Yet another thing ffmpeg can do
Streaming a camera to YouTube came up in chat the other day. It’s not something I’ve ever had cause to do, but it seemed interesting so I thought I’d try it out.
Prerequisites
Streaming to YouTube Live from a phone or tablet is possible from the YT app, but apparently you need 1000 subscribers to do it directly. Instead, we can grab a camera stream and use that.
You’re going to need:
- a phone (or tablet) with camera
- an app to stream the camera with — I used IP Webcam but others should work as long as they provide an MJPEG stream
- ffmpeg
- a YouTube account
If you are using IP camera, you can grab the stream over wifi, using the device’s IP address. By default IP Webcam listens on port 8080, so the web interface is available on https://deviceIP:8080/
. It’s a good idea to open this in a browser to test that the connection an video work.
The quality will depend on what options are set, but with default options I got this:
Perfectly usable. For streaming, we want the video stream, which is located at https://deviceIP:8080/video
We can feed this to ffmpeg
, and add in a null audio stream, as an audio stream of some sort is required by YouTube Live.
Also needed is the stream key, from the live stream Studio, accessible via ‘Go Live’:
then you can get the stream key via ‘Stream Settings’:
and click the eye () to show the key.
This lets us give the following invocation of ffmpeg:
$ ffmpeg -i http://phoneIP:8080/video -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -vcodec libx264 -pix_fmt yuv420p -b:v 2500k -preset faster -f flv rtmp://a.rtmp.youtube.com/live2/aaa-bbbb-cccc-dddd-eee
A quick recap of the various parts:
-i http://phoneIP:8080/video
– use our device’s video stream as an input-f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100
– use the anullsrc audio filter to provide a dummy audio stream-vcodec libx264
encode using the x264 encoder in a well-supported pixel format (-pix_fmt yuv420p
) at a reasonable bitrate (-b:v 2500k
— feel free to increase this value if your upload speed supports it) using the ‘faster’ preset (-preset faster
)-f flv
use an output format YouTube understandsrtmp://a.rtmp.youtube.com/live2/aaa-bbbb-cccc-dddd-eee
– send the output via RTMP to YouTube’s ingress URL (rtmp://a.rtmp.youtube.com/live2/
) using the stream key (aaa-bbbb-cccc-dddd-eee
)
And you should get ffmpeg
output along the lines of:
and the YouTube studio page should change:
?
and you should see a preview of your stream. Once you’re happy, click ‘GO LIVE’ to go live!
Troubleshooting
ffmpeg outputs to RTMP correctly, but the Studio page still says “No Data”
This happens if you have no audio present. Add your own audio stream or use the anullsrc
filter for a dummy audio stream described above.
I want something easier than ffmpeg!
OBS can use MJPEG resources as a ‘media source‘:
You may have to toggle the source to refresh it once you start the server in the app.
I want to use this as a virtual webcam / video device
That’s a bit out of the scope, but briefly: ffmpeg
can create a virtual video device if the kernel module v4l2loopback
is installed. Instead of using FLV format and the rtmp://
URL as output, use -f v4l2 /dev/video0
(or video1
if another video device is already present).
There is an initiative to get OBS to output to a virtual webcam, but at the moment it cannot do that natively. It is possible to do so using plug-ins:
https://github.com/CatxFish/obs-virtual-cam for windows
https://github.com/CatxFish/obs-v4l2sink for linux