0

The purpose of this script is to read a file, extract the audio, and print out a transcript by running it through IBM Watson speech to text API. My problem is when I try to save the output from the subprocess into a variable and pass it into the open function, it reads as binary. What am I doing wrong? Any help would be appreciated!

import sys
import re 
import json
import requests
import subprocess 
from subprocess import Popen, PIPE

fullVideo = sys.argv[1]
title = re.findall('^([^.]*).*', fullVideo)
title = str(title[0])
output = subprocess.Popen('ffmpeg -i ' + fullVideo + ' -vn -ab 128k ' + title + '.flac', shell = True, stdin=subprocess.PIPE).communicate()[0]

sfile= open(output, "rb")
response = requests.post("https://stream.watsonplatform.net/speech-to-text/api/v1/recognize",
         auth=("USERNAME", "PASSWORD"),
         headers = {"content-type": "audio/flac"},
         data=sfile
         )

print (json.loads(response.text))
Bryan
  • 435
  • 1
  • 4
  • 11

1 Answers1

0

1.Run 'ffmpeg -i ' + fullVideo + ' -vn -ab 128k ' + title + '.flac' for ensure it is right.

2.If it is right, see the converted file existed.

3.stdin is standard input, stdout is standard output. So use stdout argument in Popen.

    output = subprocess.Popen('ffmpeg -i ' + fullVideo + ' -vn -ab 128k ' + title + '.flac', shell = True, stdout=subprocess.PIPE).communicate()[0]
sfile= open(output, "rb")
Em L
  • 328
  • 1
  • 7
  • I am still receiving this error: FileNotFoundError: [Errno 2] No such file or directory: b'' – Bryan Jul 20 '16 at 04:54
  • run `'ffmpeg -i ' + fullVideo + ' -vn -ab 128k ' + title + '.flac'` in terminal or `print output`,ensure ffmpeg output is right. – Em L Jul 20 '16 at 05:44
  • Ideally, I'd like to run it within the script and only pass in the video as the argument within the terminal When I print output to the console, it states: /bin/sh: ffmpeg: command not found b''. I believe that the process of extracting the audio from the video isn't completing in time which is why the output is just b". How can I allow it to wait until the process is completed before passing it into the open function? – Bryan Jul 20 '16 at 05:50
  • It looks like ffmpeg is not installed or not add to environment variable PATH. Run ffmpeg in terminal directly, which is okay or not? – Em L Jul 20 '16 at 06:01
  • Confirming that ffmpeg is now installed (I switched computers and forgot to homebrew it on this one). However, I am still receiving the error message: FileNotFoundError: [Errno 2] No such file or directory: b'' – Bryan Jul 20 '16 at 06:07
  • The output should be the converted video file name. For instances, example.mp4 --> example.flac – Bryan Jul 20 '16 at 17:32