将子进程的输出指派给变量

2024-09-19 23:35:34 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个由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))

Tags: importrejsonoutputtitleresponsesysopen
1条回答
网友
1楼 · 发布于 2024-09-19 23:35:34

1.运行“ffmpeg-i”+fullVideo+'-vn-ab 128k'+title+'.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")

相关问题 更多 >