FFmpeg直播

时间:2018-11-28 23:25:16

标签: python youtube-data-api live-streaming

如何在python中使用此代码,请详细指导我是全新的。

ffmpeg \
-f alsa -ac 2 -i hw:1,0 \
-f v4l2 -r 10 -i /dev/video0 \
-c:v libx264 -pix_fmt yuv420p -preset ultrafast -g 20 -b:v 2500k \
-c:a aac -ar 44100 \
-threads 0 -bufsize 512k \
-f flv rtmp://a.rtmp.youtube.com/live2/YOURSTREAM &> stream.log

我尝试用pycharm编写

  1. 当我刚粘贴到编辑器中时,它像往常一样给了我错误

  2. 然后我尝试像这样在os.system()和subprocess.call()中编写

os.system

os.system(ffmpeg \)
os.system(-f alsa -ac 2 -i hw:1,0 \)
os.system(-f v4l2 -r 10 -i /dev/video0 \)
os.system(-c:v libx264 -pix_fmt yuv420p -preset ultrafast -g 20 -b:v 2500k\)
os.system(-c:a aac -ar 44100 \)
os.system(-threads 0 -bufsize 512k \)
os.system(-f flv rtmp://a.rtmp.youtube.com/live2/YOURSTREAM &> stream.log)

subprocess.call()

subprocess.call(ffmpeg \)
subprocess.call(-f alsa -ac 2 -i hw:1,0 \)
subprocess.call(-f v4l2 -r 10 -i /dev/video0 \)
subprocess.call(-c:v libx264 -pix_fmt yuv420p -preset ultrafast -g 20 -b:v 2500k\)
subprocess.call(-c:a aac -ar 44100 \)
subprocess.call(-threads 0 -bufsize 512k \)
subprocess.call(-f flv rtmp://a.rtmp.youtube.com/live2/YOURSTREAM &> stream.log)

这两次都给了我类似

的错误
'-i' is not recognized as an internal or external command,
operable program or batch file.
'-vcodec' is not recognized as an internal or external command,
operable program or batch file.
'-acodec' is not recognized as an internal or external command,
operable program or batch file.
'-f' is not recognized as an internal or external command,
operable program or batch file.

请指导我,如果还有其他通过python将视频上传到youtube实时流的方法,请告诉我

1 个答案:

答案 0 :(得分:0)

您可以尝试将整个ffmpeg命令包装到单个subprocess.call中,将参数分成一个列表。

此命令(略作修改)对我来说有效,将剪辑流式传输到我的Facebook页面:

subprocess.call(["ffmpeg", "-ac", "2", "-i", "/home/ubuntu/clip.mp4", "-r", "10", "-i", "/home/ubuntu/clip.mp4", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-preset", "ultrafast", "-g", "20", "-b:v", "2500k", "-c:a", "aac", "-ar", "44100", "-threads", "0", "-bufsize", "512k", "-f", "flv", "rtmp://rtmp-api-dev.facebook.com:80/rtmp/10156686554299774?<my stream key>"])