1

I'm working on a tensorflow project that learns from an audio stream. I'm using the subprocess module (with Popen) and FFMPEG to read in the audio data from an mp3. I successfully open the audio file with Popen() and I can print the output through stdout. However, I can't seem to capture it.

I have tried both read() and communicate()

I'm following a tutorial here

read() simply returns nothing and communicate() throws the error: AttributeError: 'file' object has no attribute 'communicate'

Here's my code:

for image_index, image in enumerate(image_files):
  count += 1
  image_file = os.path.join(folder, image)
  try:
    output_files = "output/output" + str(count) + ".png"
    if image_file != 'train/rock/.DS_Store':
        command = [FFMPEG_BIN,
            '-i', image_file,
            '-f', 's16le',
            '-acodec', 'pcm_s16le',
            '-ar', '44100',
            '-ac', '2',
            output_files]
        pipe = sp.Popen(command, stdout=sp.PIPE)
        print (pipe)
        raw_audio = pipe.stdout.communicate(88200*4)

I've tried everything here and here

0

1 Answer 1

6

The Popen object has communicate not stdout:

pipe.communicate(str(88200*4))

To also capture stderr through stdout:

 pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE)
 raw_audio, _  = pipe.communicate(str(88200*4).encode())
 print(raw_audio)
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you! I was banging my head against a wall, and of course it's something simple. Is there a reason why the print(raw_audio) would be printing (' ', None) ?
@KendallWeihe, try adding stderr=sp.STDOUT
To Popen? Now it just spins with this as the last output <subprocess.Popen object at 0x10f3cdf50>
@KendallWeihe, you know communicate will block until the process returns? The only way you would see subprocess.Popen object at 0x10f3cdf50> would be if you printed pipe and not raw_audio, is your end goal to write to files?
my goal is to print the raw data the ffmpeg reads so I can confirm it is actually reading something
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.