2

I'm trying to use ffmpeg to stream output from a c920 webcam to YouTube live on a Raspberry Pi.

Unfortunately, YouTube Live returns this error when I start the stream:

Please use a keyframe frequency of four seconds or less. Currently, keyframes are not being sent often enough, which will cause buffering. The current keyframe frequency is 9.9 seconds.

For what it's worth, the stream works on Twitch, but buffers pretty frequently.

I am looking for advice to make the stream acceptable to YouTube Live by adjusting the keyframe frequency.

The command I am using is this:

ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le \
-i /dev/null -ac 2 -f v4l2 -codec:v h264 -framerate 30 -video_size 1920x1080 \
-itsoffset 0.5 -i /dev/video0 -copyinkf -codec:v copy -codec:a aac -ab 128k -g 10 \
-f flv -strict -2 rtmp://a.rtmp.youtube.com/live2/STREAM_KEY

When I run the command, I get output like:

ffmpeg version N-60774-gc427b2b Copyright (c) 2000-2014 the FFmpeg developers
  built on Feb 20 2014 16:29:13 with gcc 4.7.1 (crosstool-NG 1.15.2) 20120402 (prerelease)
  configuration: --enable-cross-compile --cross-prefix=/opt/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi- --arch=armel --target-os=linux --prefix=/home/jon/arm --enable-gpl --enable-libx264 --enable-nonfree --extra-cflags=-I/home/jon/arm/include --extra-ldflags=-L/home/jon/arm/lib --extra-libs=-ldl
  libavutil      52. 65.100 / 52. 65.100
  libavcodec     55. 52.102 / 55. 52.102
  libavformat    55. 33.100 / 55. 33.100
  libavdevice    55. 10.100 / 55. 10.100
  libavfilter     4.  1.103 /  4.  1.103
  libswscale      2.  5.101 /  2.  5.101
  libswresample   0. 17.104 /  0. 17.104
  libpostproc    52.  3.100 / 52.  3.100
Guessed Channel Layout for  Input Stream #0.0 : stereo
Input #0, s16le, from '/dev/null':
  Duration: N/A, bitrate: 1411 kb/s
    Stream #0:0: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s
Input #1, video4linux2,v4l2, from '/dev/video0':
  Duration: N/A, start: 80888.938908, bitrate: N/A
    Stream #1:0: Video: h264 (Constrained Baseline), yuvj420p(pc), 1920x1080 [SAR 1:1 DAR 16:9], -5 kb/s, 30 fps, 30 tbr, 1000k tbn, 60 tbc
Output #0, flv, to 'rtmp://a.rtmp.youtube.com/live2/vktg-95zk-96fm-133h':
  Metadata:
    encoder         : Lavf55.33.100
    Stream #0:0: Video: h264 ([7][0][0][0] / 0x0007), yuvj420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, -5 kb/s, 30 fps, 1k tbn, 1000k tbc
    Stream #0:1: Audio: aac ([10][0][0][0] / 0x000A), 44100 Hz, stereo, fltp, 128 kb/s
Stream mapping:
  Stream #1:0 -> #0:0 (copy)
  Stream #0:0 -> #0:1 (pcm_s16le -> aac)
Press [q] to stop, [?] for help
[flv @ 0x2c0b3d0] Non-monotonous DTS in output stream 0:0; previous: 500, current: 333; changing to 500. This may result in incorrect timestamps in the output file.
[flv @ 0x2c0b3d0] Non-monotonous DTS in output stream 0:0; previous: 500, current: 369; changing to 500. This may result in incorrect timestamps in the output file.
[flv @ 0x2c0b3d0] Non-monotonous DTS in output stream 0:0; previous: 500, current: 401; changing to 500. This may result in incorrect timestamps in the output file.
[flv @ 0x2c0b3d0] Non-monotonous DTS in output stream 0:0; previous: 500, current: 433; changing to 500. This may result in incorrect timestamps in the output file.
[flv @ 0x2c0b3d0] Non-monotonous DTS in output stream 0:0; previous: 500, current: 469; changing to 500. This may result in incorrect timestamps in the output file.
frame=  202 fps= 18 q=-1.0 size=     760kB time=00:00:13.94 bitrate= 446.4kbits/s    

This github issue suggests there's a control in v4l2-ctl to adjust the keyframe rate, but I don't see it available:

$ v4l2-ctl --list-ctrls
                     brightness (int)    : min=0 max=255 step=1 default=-8193 value=128
                       contrast (int)    : min=0 max=255 step=1 default=57343 value=128
                     saturation (int)    : min=0 max=255 step=1 default=57343 value=128
 white_balance_temperature_auto (bool)   : default=1 value=1
                           gain (int)    : min=0 max=255 step=1 default=57343 value=255
           power_line_frequency (menu)   : min=0 max=2 default=2 value=2
      white_balance_temperature (int)    : min=2000 max=6500 step=1 default=57343 value=2294 flags=inactive
                      sharpness (int)    : min=0 max=255 step=1 default=57343 value=128
         backlight_compensation (int)    : min=0 max=1 step=1 default=57343 value=0
                  exposure_auto (menu)   : min=0 max=3 default=0 value=3
              exposure_absolute (int)    : min=3 max=2047 step=1 default=250 value=666 flags=inactive
         exposure_auto_priority (bool)   : default=0 value=1
                   pan_absolute (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                  tilt_absolute (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                 focus_absolute (int)    : min=0 max=250 step=5 default=8189 value=0 flags=inactive
                     focus_auto (bool)   : default=1 value=1
                  zoom_absolute (int)    : min=100 max=500 step=1 default=57343 value=100
                     brightness (int)    : min=0 max=255 step=1 default=-8193 value=128
                       contrast (int)    : min=0 max=255 step=1 default=57343 value=128
                     saturation (int)    : min=0 max=255 step=1 default=57343 value=128
 white_balance_temperature_auto (bool)   : default=1 value=1
                           gain (int)    : min=0 max=255 step=1 default=57343 value=255
           power_line_frequency (menu)   : min=0 max=2 default=2 value=2
      white_balance_temperature (int)    : min=2000 max=6500 step=1 default=57343 value=2294 flags=inactive
                      sharpness (int)    : min=0 max=255 step=1 default=57343 value=128
         backlight_compensation (int)    : min=0 max=1 step=1 default=57343 value=0
2

Here is how I pipe my HIK IP camera to youtube. This does NOT include audio, and is NOT a USB camera, but perhaps it helps [NOTE: Youtube requires an audio stream, even with a video only stream]. (this is an old script, I have better BASH coding standards today).

#! /bin/bash
YOUTUBE_URL="rtmp://a.rtmp.youtube.com/live2"   # Server URL
KEY="USERNAME.xxxx-xxxx-xxxx-xxxx"          # Stream name/key

ffmpeg \
    -hide_banner \
    -ar 44100 -acodec pcm_s16le -f s16le -ac 2 -channel_layout 2.1 -i /dev/zero \
    -i rtsp://ZMUser:ZMPassword@192.168.1.100:554/Streaming/Channels/2?tcp \
    -rtsp_transport tcp \
    -ss 2 \
    -c:v copy \
    -c:a libmp3lame \
    -shortest \
    -f flv "$YOUTUBE_URL/$KEY" 
  • Here is a video I shared with the above Script (youtube.com/watch?v=nzfi3l0Bsc4) – Eric Kirchner Jan 24 '17 at 7:08
  • Simplify your script, first attempt to rip your camera to a file, then try modifying the ffmpeg commandine to pipe to youtube. Always go back to the Basics First. – Eric Kirchner Jan 24 '17 at 7:09
  • For what it's worth, the simplest command I can get running is ./ffmpeg -f v4l2 -input_format h264 -i /dev/video0 -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -i /dev/null -copyinkf -codec:v copy -codec:a aac -f flv -strict -2 rtmp://a.rtmp.youtube.com/live2/ – Dan Jan 25 '17 at 5:35
  • the command above shows the same error on YouTube, but works on Twitch. – Dan Jan 25 '17 at 6:41
2

I managed to get it to work on YouTube Live with the following command:

ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero \
       -f v4l2 -input_format h264 -i /dev/video0 -codec:v copy \
       -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/REMOVED

I can set the resolution by doing:

v4l2-ctl --device=/dev/video0 --set-fmt-video=width=1280,height=720

The ffmpeg output looks like this:

Stream #1:0: Video: h264 (Constrained Baseline), yuvj420p(pc), 320x240 [SAR 1:1 DAR 4:3], -5 kb/s, 30 fps, 30 tbr, 1000k tbn, 60 tbc

Eventually YouTube complains about keyframes, but it manages to maintain a decent stream at 1080p.

0
ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le \
-i /dev/null -ac 2 -f v4l2 -codec:v h264 -framerate 30 -video_size 1920x1080 \
-itsoffset 0.5 -i /dev/video0 -copyinkf -codec:v copy -codec:a aac -ab 128k -g 10 \
-f flv -strict -2 rtmp://a.rtmp.youtube.com/live2/STREAM_KEY

Try reducing your complexity to:

ffmpeg \ -ar 44100 -acodec pcm_s16le \ -f s16le -ac 2 -channel_layout 2.1 -i /dev/null \ -f v4l2 -codec:v h264 -framerate 6 -video_size 1920x1080 -i /dev/video0 \ -c:v copy \ -c:a libmp3lame \ -f flv rtmp://a.rtmp.youtube.com/live2/$STREAM_KEY

The above will have a BLANK audio stream.

As I edit the above, either you mean STREAM_KEY to substitute your STREAM_KEY or you are missing a $STREAM_KEY, in a part of your command you didn't post.

0

Functional Script, no audio, under Dell Latitude and Integral Webcam

The valid formats for this camera are:

Raw : yuyv422 : # YUYV 4:2:2 : 640x480 160x120 176x144 320x240 352x288 1280x720 1920x1080

Compressed: # mjpeg : Motion-JPEG : 640x480 160x120 176x144 320x240 352x288 1280x720 1920x1080

ffmpeg \
    -hide_banner \
    -ar 44100 \
    -acodec pcm_s16le \
    -f s16le \
    -ac 2 \
    -channel_layout 2.1 \
    -i /dev/zero \
    -f v4l2 \
    -framerate 24 \
    -video_size 320x240 \
    -i /dev/video0 \
    -c:v libx264 \
    -c:a libmp3lame \
    -f flv "$YOUTUBE_URL/$KEY"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.