将子流程的输出分配给变量

时间:2016-07-20 04:20:29

标签: python ffmpeg subprocess

此脚本的目的是读取文件,提取音频,并通过IBM Watson语音到文本API运行脚本来打印出脚本。我的问题是当我尝试将子进程的输出保存到变量并将其传递给open函数时,它读取为二进制。我究竟做错了什么?任何帮助,将不胜感激!

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))

1 个答案:

答案 0 :(得分:0)

1.Run' ffmpeg -i' + fullVideo +' -vn -ab 128k' +标题+' .flac'确保它是正确的。

2.如果正确,请参阅已转换的文件。

3.stdin是标准输入,stdout是标准输出。所以在Popen中使用stdout参数。

    output = subprocess.Popen('ffmpeg -i ' + fullVideo + ' -vn -ab 128k ' + title + '.flac', shell = True, stdout=subprocess.PIPE).communicate()[0]
sfile= open(output, "rb")
相关问题