How to livestream a webcam to YouTube with FFmpeg?

廉价感情. 提交于 2020-05-16 21:42:08

问题


I want to send the livestream of my webcam to YouTube. I can follow YouTube's guide up to step 8. "Stream Connection" tells me there is "No data" and the button "Go Live" remains unclickable. A screenshot of this situation can be seen at

image

As encoding software, I was planning on using FFmpeg because it can run from the target platform, a Raspberry Pi with Raspbian. A USB webcam supported by video4linux2 is used.

FFmpeg's wiki shows that streaming a file can be done with the following:

ffmpeg -re -i input.mkv \
-c:v libx264 -preset veryfast -maxrate 3000k \
-bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 \
-ar 44100 -f flv rtmp://live.twitch.tv/app/<stream key>

I modified this command in the following ways: 1. It takes the video stream from the webcam with -f v4l2 -i /dev/video0. 2. It does not broadcast any audio with -an. 3. It broadcasts to YouTube's RTMP server, rtmp://a.rtmp.youtube.com/live2/<stream key>

The final version of the command is now:

RTMP_URL="rtmp://a.rtmp.youtube.com/live2"
STREAM_KEY="secr-etse-cret-secr"
OUTPUT=$RTMP_URL/$STREAM_KEY
ffmpeg -re -f v4l2 -i /dev/video0 \
-c:v libx264 -preset veryfast -maxrate 3000k \
-bufsize 6000k -pix_fmt yuv420p -g 50 -an \
-f flv $OUTPUT

When I run this command, I would expect that "Stream connection" would change to something else than "No data" after a few seconds, but that does not happen.

I have tried recording the stream to a local file with:

ffmpeg -re -f v4l2 -i /dev/video0 \
-c:v libx264 -preset veryfast -maxrate 3000k \
-bufsize 6000k -pix_fmt yuv420p -g 50 -an \
-f flv test.flv

This worked fine. That demonstrates to me that the issue is with getting the video stream accepted by YouTube.


回答1:


The code below works very well using Windows, you may adjust to fit for your need

ffmpeg -loglevel debug -threads:v 2 -threads:a 8 -filter_threads 2 -thread_queue_size 512  -f dshow -i video="HP Wide Vision HD" -f dshow -i audio="Microphone Array (Realtek Audio)" -pix_fmt yuv420p -c:v libx264 -qp:v 19 -profile:v high -rc:v cbr_ld_hq -level:v 4.2 -r:v 60 -g:v 120 -bf:v 3 -refs:v 16 -f flv rtmp://youtube_stream_url/stream_key


来源:https://stackoverflow.com/questions/57540004/how-to-livestream-a-webcam-to-youtube-with-ffmpeg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!