Spaziba
Posts: 26
Joined: Mon Apr 30, 2018 4:31 pm

USB webcam live streaming using FFmpeg

Mon Apr 30, 2018 4:49 pm

Hi there

Let me start by saying I am a complete novice. That I managed to stumble as far as I have is nothing short of miraculous.
Anyways, I have the simple dream of being able to live stream to YouTube using my Logitech C920 connected to a Raspberry Pi 3b+. I actually managed to get some of the way. That is I seem to have installed and compiled FFmpeg with h264 on a fully updated Raspbian (2018-04-18). I also managed to stream, but I am at a complete loss to why I seem unable to adjust the tiniest things.
So I am using the following to stream (edited from another forum)

Code: Select all

ffmpeg -re -f s16le -i /dev/zero -f v4l2 -thread_queue_size 512 -codec:v h264 -s 1920x1080 -i /dev/video0 -codec:v copy -acodec aac -ab 128k -g 50 -f flv rtmp://a.rtmp.youtube.com/live2/*My stream ID*
This does work, but I feel pretty confident that this is an awful line of code - for instance some of the code is related to audio which I do not need. I have to be honest and say I am having a very hard time understanding FFmpeg, so every adjustment seems to go wrong, i.e. changing bit- and framerate.
Just to clarify, my goal is to live stream the C920 to YouTube, ideally 60 fps in 1920x1080, looking all nice. The Raspberry is dedicated to this only purpose, so all horsepower is available

Anyone who might have suggestions for the streaming options? Or is this simply a downright silly approach for livestreaming?
I tried looking into Docker solutions, but without success.

Sorry for the novel-lenght post.

- Spaziba

tropho
Posts: 58
Joined: Mon Feb 26, 2018 11:36 pm

Re: USB webcam live streaming using FFmpeg

Tue May 01, 2018 4:46 pm

No, it's not silly at all. I've built such a single-purpose device myself that live streams to YouTube or Facebook, and that can also capture video locally on a 128GB USB flash drive. I've outlined my process in a previous post, which includes how to compile ffmpeg to include x264 and ALSA (it appears you've already mastered that step), as well as some follow up replies with more information about the various command line options and how/why you can use them. There is also a note on which option to use if you don't need audio in your stream.

Here's the link to the thread: viewtopic.php?f=38&t=205181

What are you trying to adjust? I think I could provide some advice to help you balance quality vs. bandwidth.

Please note I used a Raspberry Pi camera v2 for this, rather than a USB webcam so you won't need to use raspivid.

Spaziba
Posts: 26
Joined: Mon Apr 30, 2018 4:31 pm

Re: USB webcam live streaming using FFmpeg

Thu May 03, 2018 2:01 pm

Hi there Tropho

Thanks for your reply. All to busy at the moment, so just wanted to say I will give it a look and get back to you. Balance quality vs. bandwidth seems like a no brainer to get your opinion on ;)

tropho
Posts: 58
Joined: Mon Feb 26, 2018 11:36 pm

Re: USB webcam live streaming using FFmpeg

Fri May 04, 2018 12:50 pm

I'm glad to help where I can. If you're using a display with your Pi, you could check out Open Broadcaster Software (OBS), a tool many people use to live stream to Twitch, YouTube and other services. They have Windows, Mac and Linux versions available but I've only tried the Windows version, which worked great with my Logitech C615 webcam. The live streaming camera I built would be more like a GoPro or video camera so that wouldn't work for my project, but ffmpeg+raspivid works well.

As for suggestions on the command line, when you get some free time I recommend checking out my examples on the linked thread and see if that provides what you need. Looking at your command line, there is nothing specifying video bitrate so that it something to consider adding so you can begin your quality vs. bandwidth battle. I call it a "battle" because it will likely take many tries to get it right, but to me that refinement is one of the fun parts of the process.

Finally, if you're trying to live stream to YouTube you'll need to keep using that "dummy" audio option otherwise the stream will be rejected. I'm not sure how Twitch, Facebook or any other service handles that. It adds no data to the stream so there is no bandwidth cost to use it. Here's the command line piece for that: -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero

Spaziba
Posts: 26
Joined: Mon Apr 30, 2018 4:31 pm

Re: USB webcam live streaming using FFmpeg

Mon May 07, 2018 2:31 pm

OBS is very fine yes, but sadly not able to run on a Raspberry. That would have been a good solution.
Regarding bitrate you are absolutely right. I have tried fooling around with your examples, but I seem to get stuck quite early...
I am not familiar with the "hierarchy" of the commands. So where is the correct position to specify bitrate?

Code: Select all

ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f v4l2 -thread_queue_size 10240 -codec:v h264 -s 1920x1080 -i /dev/video0 -codec:v copy -codec:a copy -f flv rtmp://a.rtmp.youtube.com/"STREAM KEY"
(I altered my stream code a bit since the original post)

I keep getting pretty much this, or no result:

Code: Select all

Option b:v (video bitrate (please use -b:v)) cannot be applied to input url /dev/video0 -- you are trying to apply an input option to an output file or vice versa. Move this option before the file it belongs to.
Error parsing options for input file /dev/video0.
By the way, here is my setup.
On.jpg
On.jpg (231.11 KiB) Viewed 48731 times
Basically, power cable, webcam, and a control. Record button starts the FFmpeg stream and turns on red led to indicate normal behaviour should be observed in front of the camera - stop button, well no surprise what it does. (Note: pay very little attention to the crudely painted button symbols)
Last edited by Spaziba on Tue May 08, 2018 3:16 pm, edited 2 times in total.

tropho
Posts: 58
Joined: Mon Feb 26, 2018 11:36 pm

Re: USB webcam live streaming using FFmpeg

Mon May 07, 2018 6:50 pm

Spaziba wrote:
Mon May 07, 2018 2:31 pm
OBS is very fine yes, but sadly not able to run on a Raspberry. That would have been a good solution.
Regarding bitrate you are absolutely right. I have tried fooling around with your examples, but I seem to get stuck quite early...
I am not familiar with the "hierarchy" of the commands. So where is the correct position to specify bitrate?

Code: Select all

ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f v4l2 -thread_queue_size 10240 -codec:v h264 -s 1920x1080 -i /dev/video0 -codec:v copy -codec:a copy -f flv rtmp://a.rtmp.youtube.com/live2/*key_removed*
(I altered my straem code a bit since the original post)

I keep getting pretty much this, or no result:

Code: Select all

Option b:v (video bitrate (please use -b:v)) cannot be applied to input url /dev/video0 -- you are trying to apply an input option to an output file or vice versa. Move this option before the file it belongs to.
Error parsing options for input file /dev/video0.
By the way, here is my setup.
On.jpg
Basically, power cable, webcam, and a control. Record button starts the FFmpeg stream and turns on red led to indicate normal behaviour should be observed in front of the camera - stop button, well no surprise what it does. (Note: pay very little attention to the crudely painted button symbols)
*You may want to edit your last post as I think you left your streaming key in the command line!*

Nice job on the record/stop controller! I was lucky that there is a PiTFT model that includes buttons or else I would have had to come up with something like that for mine.

I cleaned up and slightly modified your command line; try this command line and see if it works:
ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f v4l2 -s 1920x1080 -r 30 -i /dev/video0 -codec:v h264 -g 60 -b:v 2500k -codec:a aac -ac 2 -ar 44100 -ab 128k -f flv rtmp://a.rtmp.youtube.com/live2/*your_streaming_key*

Here are explanations of the options; pardon me if you already know some of these:
1. -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero : a dummy audio device that reports a valid audio format and sample rate for YouTube
2. -f v4l2 : video format output from your camera
3. -s 1920x1080 : camera's video resolution
4. -r 30 : video framerate (I changed it from 25 to 30)
5. -i /dev/video0 : input device (your webcam)
6. -codec:v h264 : use h264 video codec
7. -g 60 : group of pictures (GOP) interval (ideally twice your framerate, so 30x2)
8. -b:v 2500k : video bitrate, in this case is 2.5Mbps, aka 2,500k, aka 2500000
9. -codec:a aac -ac 2 -ar 44100 -ab 128k : audio stream options, but this one is not "real" as you're using an empty audio stream
10. -f flv : output format (Flash video)
11. rtmp://... : the RTMP stream you're using as your output

Spaziba
Posts: 26
Joined: Mon Apr 30, 2018 4:31 pm

Re: USB webcam live streaming using FFmpeg

Tue May 08, 2018 4:09 pm

*You may want to edit your last post as I think you left your streaming key in the command line!*
Oh how embarrassing! A little disappointed no one took advantage of it.

Thank you for your compliments. You PiTFT model is pretty nice yes.
I cleaned up and slightly modified your command line; try this command line and see if it works:
Very cool, but no cigar. I get 3 fps and basically no stream. Fooling a little around with output format and video codec did however seem to have some effect. The use of h264 was merely because the webcam has onboard H264 encoding, and I thought that might save some processing power somehow.
Here are explanations of the options; pardon me if you already know some of these:
Fantastic explanations! That's so neat!




I'm wondering if, just to be safe, I should start over with FFmpeg compilation...

Oh, and if there should be any doubt: I am very grateful for all your help, and almost feel bad for you considering my FFmpeg ignorance. But things feel like they are beginning to shape up.

tropho
Posts: 58
Joined: Mon Feb 26, 2018 11:36 pm

Re: USB webcam live streaming using FFmpeg

Tue May 08, 2018 8:05 pm

Spaziba wrote:
Tue May 08, 2018 4:09 pm
*You may want to edit your last post as I think you left your streaming key in the command line!*
Oh how embarrassing! A little disappointed no one took advantage of it.

Thank you for your compliments. You PiTFT model is pretty nice yes.
I cleaned up and slightly modified your command line; try this command line and see if it works:
Very cool, but no cigar. I get 3 fps and basically no stream. Fooling a little around with output format and video codec did however seem to have some effect. The use of h264 was merely because the webcam has onboard H264 encoding, and I thought that might save some processing power somehow.
Here are explanations of the options; pardon me if you already know some of these:
Fantastic explanations! That's so neat!




I'm wondering if, just to be safe, I should start over with FFmpeg compilation...

Oh, and if there should be any doubt: I am very grateful for all your help, and almost feel bad for you considering my FFmpeg ignorance. But things feel like they are beginning to shape up.

You're welcome; I enjoy helping out.

I did some web searches to find others who have tried streaming to YouTube with a C920 webcam, and one preson got it working using the following line, which also has the "dummy" or blank audio stream:
ffmpeg \ -ar 44100 -acodec pcm_s16le \ -f s16le -ac 2 -channel_layout 2.1 -i /dev/null \ -f v4l2 -codec:v h264 -framerate 30 -video_size 1920x1080 -i /dev/video0 \ -c:v copy \ -c:a libmp3lame \ -f flv rtmp://a.rtmp.youtube.com/live2/your_stream_key

They also said they set the C920's resolution (before executing the above line) with this:
v4l2-ctl --device=/dev/video0 --set-fmt-video=width=1920,height=1080

I'm not sure the v4l2-ctl command is necessary but it can't hurt to try it to see if it makes a difference. Also the command line options are different than what I use but ffmpeg accepts both long and short versions of most options.

Spaziba
Posts: 26
Joined: Mon Apr 30, 2018 4:31 pm

Re: USB webcam live streaming using FFmpeg

Thu May 10, 2018 9:30 am

You're welcome; I enjoy helping out.

I did some web searches to find others who have tried streaming to YouTube with a C920 webcam, and one preson got it working using the following line, which also has the "dummy" or blank audio stream:
ffmpeg \ -ar 44100 -acodec pcm_s16le \ -f s16le -ac 2 -channel_layout 2.1 -i /dev/null \ -f v4l2 -codec:v h264 -framerate 30 -video_size 1920x1080 -i /dev/video0 \ -c:v copy \ -c:a libmp3lame \ -f flv rtmp://a.rtmp.youtube.com/live2/your_stream_key

They also said they set the C920's resolution (before executing the above line) with this:
v4l2-ctl --device=/dev/video0 --set-fmt-video=width=1920,height=1080

I'm not sure the v4l2-ctl command is necessary but it can't hurt to try it to see if it makes a difference. Also the command line options are different than what I use but ffmpeg accepts both long and short versions of most options.
Interesting. The codec:v copy really seems to do all the difference. As soon as I try some "manual" entries instead, I lose all fps.
Best result as of yet are with the following code:

Code: Select all

ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f v4l2 -codec:v h264 -framerate 30 -video_size 1920x1080 -i /dev/video0 -codec:v copy -f flv rtmp://a.rtmp.youtube.com/live2/Stream Code
which yields me: frame= 473 fps= 31 q=-1.0 size= 6539kB time=00:00:15.55 bitrate=3443.2kbits/s speed=1.0 and perhaps that is good enough...

tropho
Posts: 58
Joined: Mon Feb 26, 2018 11:36 pm

Re: USB webcam live streaming using FFmpeg

Thu May 10, 2018 11:59 am

Spaziba wrote:
Thu May 10, 2018 9:30 am
You're welcome; I enjoy helping out.

I did some web searches to find others who have tried streaming to YouTube with a C920 webcam, and one preson got it working using the following line, which also has the "dummy" or blank audio stream:
ffmpeg \ -ar 44100 -acodec pcm_s16le \ -f s16le -ac 2 -channel_layout 2.1 -i /dev/null \ -f v4l2 -codec:v h264 -framerate 30 -video_size 1920x1080 -i /dev/video0 \ -c:v copy \ -c:a libmp3lame \ -f flv rtmp://a.rtmp.youtube.com/live2/your_stream_key

They also said they set the C920's resolution (before executing the above line) with this:
v4l2-ctl --device=/dev/video0 --set-fmt-video=width=1920,height=1080

I'm not sure the v4l2-ctl command is necessary but it can't hurt to try it to see if it makes a difference. Also the command line options are different than what I use but ffmpeg accepts both long and short versions of most options.
Interesting. The codec:v copy really seems to do all the difference. As soon as I try some "manual" entries instead, I lose all fps.
Best result as of yet are with the following code:

Code: Select all

ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f v4l2 -codec:v h264 -framerate 30 -video_size 1920x1080 -i /dev/video0 -codec:v copy -f flv rtmp://a.rtmp.youtube.com/live2/Stream Code
which yields me: frame= 473 fps= 31 q=-1.0 size= 6539kB time=00:00:15.55 bitrate=3443.2kbits/s speed=1.0 and perhaps that is good enough...
If you're satisfied with the quality of the stream and it's not too much data for your network connection, then that sounds good to me. The final bitrate is over 3Mbps (good), the fps is 30 (great) and the speed is stable at 1.0 (excellent; less than 1 means it's falling behind and the stream will pause until the data catches up, not a good thing for viewers). If you're just using WiFi and don't worry about the cost of data it appears you have a good solution.

I'm happy to read that you've come to a solution that seems to work!

Spaziba
Posts: 26
Joined: Mon Apr 30, 2018 4:31 pm

Re: USB webcam live streaming using FFmpeg

Fri May 11, 2018 11:36 am

If you're satisfied with the quality of the stream and it's not too much data for your network connection, then that sounds good to me. The final bitrate is over 3Mbps (good), the fps is 30 (great) and the speed is stable at 1.0 (excellent; less than 1 means it's falling behind and the stream will pause until the data catches up, not a good thing for viewers). If you're just using WiFi and don't worry about the cost of data it appears you have a good solution.

I'm happy to read that you've come to a solution that seems to work!
Well the 3 Mbps might be overkill, but it consumes surprisingly little upload bandwidth so I think I'll stick with it. (plus it sounds better than "I think I'm going to use the only thing that I can get working ;))

Anyways, once again thanks for all the help tropho. I understand for more about FFmpeg than before. Thank god for forums - hopefully it will be a while before I need to bother someone again.

LlamaFluff
Posts: 1
Joined: Thu Jul 19, 2018 11:46 pm

Re: USB webcam live streaming using FFmpeg

Thu Jul 19, 2018 11:49 pm

Thank you all so much!

I know this thread is between you two but I wanted to do the exact same thing as you, a C920 and a Pi livestreaming onto YouTube. The only question is, how do I achieve the button setup?

redlac
Posts: 1
Joined: Wed Nov 06, 2019 7:38 pm

Re: USB webcam live streaming using FFmpeg

Wed Nov 06, 2019 7:46 pm

Hello, I am trying to do something very similar with a C920 and a Pi 4. I would like to stream to twitch and I'm using the following command

Code: Select all

ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f v4l2 -codec:v h264 -framerate 30 -video_size 1280x720 -i /dev/video0 -codec:v copy -f flv rtmp://live-lhr.twitch.tv/app/{stream_key} 
This command works but I don't get any audio on stream.
From what I understand this uses a blank or dummy audio stream but how would I go about streaming the audio from the mic on the C920?

Return to “Graphics, sound and multimedia”