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

Community
  • 1
  • 1
Kendall Weihe
  • 2,021
  • 4
  • 27
  • 53

1 Answers1

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)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • 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)` ? – Kendall Weihe Apr 01 '16 at 20:10
  • 1
    @KendallWeihe, try adding `stderr=sp.STDOUT` – Padraic Cunningham Apr 01 '16 at 20:11
  • To `Popen`? Now it just spins with this as the last output `` – Kendall Weihe Apr 01 '16 at 20:12
  • @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? – Padraic Cunningham Apr 01 '16 at 20:18
  • my goal is to print the raw data the ffmpeg reads so I can confirm it is actually reading something – Kendall Weihe Apr 01 '16 at 20:21
  • I forgot to add stdin so run it one more time – Padraic Cunningham Apr 01 '16 at 20:22
  • I see what you mean, and you are correct I was printing the pipe. but before `Popen()` was printing to stdout, and then when I added `stderr` it stopped. this might be no problem, in which case, are you saying that I would have to wait for the entire directory to finish processing before I could print `raw_audio`? – Kendall Weihe Apr 01 '16 at 20:23
  • @KendallWeihe, it blocks for each call until completion, you could write to stdin directly and read in real time but that can have side effects. – Padraic Cunningham Apr 01 '16 at 20:24
  • BEAUTIFUL! Thanks a bunch. One more thing, it returns a two element array. What's up with two elements instead of one? – Kendall Weihe Apr 01 '16 at 20:24
  • left=stdout, right = stderr, because we have redirected stderr we only need the left element, if you want you can `pipe.stdin.write(str(88200*4)))` `for line in iter(pipe.stdout.readline,""):print line`, that will output data as it comes, if you are using python3 you just need to iterate over stdout `for line pipe.stdout` – Padraic Cunningham Apr 01 '16 at 20:25