1

I've been using a RPi2 to encode live video from the PiCam module to stream to YouTube. The stream is up and running, but of course there are issues :). First and foremost the video stream on YouTube seems to play back very fast (as seen by a timer in front of the camera which looks like it plays back at roughly 4x speed), and then I get the buffering wheel for a few seconds and repeat, ad infinitum. Here's a great example:

https://youtu.be/Wi3K_1SuqcQ?t=389

As you can see, the video is playing back WAY too fast. Here's the command I'm using for the stream which works like champ except for the speed on the YouTube end:

sudo /opt/vc/bin/raspivid -o - -t 0 -fps 30 -h 720 -w 1280 -rot 270 | /usr/src/ffmpeg/ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i - -vcodec h264 -filter:v "setpts=1.0*PTS" -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/REMOVED

Here's a typical output on the console:

sudo /opt/vc/bin/raspivid -o - -t 0 -fps 10 -h 720 -w 1280 -rot 270 | /usr/src/ffmpeg/ffmpeg -re -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/ffmpeg version N-78791-ge7345ab Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.9.2 (Raspbian 4.9.2-10)
  configuration: --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree --enable-libmp3lame --enable-libfaac
  libavutil      55. 19.100 / 55. 19.100
  libavcodec     57. 27.101 / 57. 27.101
  libavformat    57. 26.100 / 57. 26.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 37.100 /  6. 37.100
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Guessed Channel Layout for  Input Stream #0.0 : stereo
Input #0, s16le, from '/dev/zero':
  Duration: N/A, bitrate: 1411 kb/s
    Stream #0:0: Audio: pcm_s16le, 44100 Hz, 2 channels, s16, 1411 kb/s
Input #1, h264, from 'pipe:':
  Duration: N/A, bitrate: N/A
    Stream #1:0: Video: h264 (High), yuv420p, 1280x720, 25 fps, 25 tbr, 1200k tbn, 50 tbc
Output #0, flv, to 'rtmp://a.rtmp.youtube.com/live2/':
  Metadata:
    encoder         : Lavf57.26.100
    Stream #0:0: Video: h264 ([7][0][0][0] / 0x0007), yuv420p, 1280x720, q=2-31, 25 fps, 25 tbr, 1k tbn, 1200k tbc
    Stream #0:1: Audio: aac (LC) ([10][0][0][0] / 0x000A), 44100 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc57.27.101 aac
Stream mapping:
  Stream #1:0 -> #0:0 (copy)
  Stream #0:0 -> #0:1 (pcm_s16le (native) -> aac (native))
[flv @ 0x2ab3830] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
[h264 @ 0x2a525c0] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
size=  170976kB time=00:04:13.88 bitrate=5516.9kbits/s speed=0.325x

Saw a partial response on this here, and I've got mine "working" just need help with the SPEED :)>.

  • I notice you're using the setpts filter. I'm pretty sure this is normally used to do unusual things with playback speed, and shouldn't be necessary for standard recording. Can you try the stream without it? – goobering Mar 7 '16 at 10:14
  • I think your use of the '-re' flag might also be problematic. See here: 'The FFmpeg's "-re" flag means to "Read input at native frame rate. Mainly used to simulate a grab device." i.e. if you wanted to stream a video file, then you would want to use this, otherwise it might stream it too fast (it attempts to stream at line speed by default). My guess is you typically don't want to use this flag when streaming from a live device, ever.' – goobering Mar 7 '16 at 12:54
  • ...you also have two instances of '-ac 2'. Inconsequential for playback timing, but messy. – goobering Mar 7 '16 at 12:55
  • Depending on the version of ffmpeg you're using, you may be able to get rid of '-strict experimental': 'Note: -strict experimental (or -strict -2) was previously required for this encoder, but it is ​no longer experimental and these options are unnecessary since 5 December 2015.' – goobering Mar 7 '16 at 12:57
  • @goobering, thanks for the response. I've tried the setpts filter as my attempt to fix the issue, it didn't help; the fast streaming occurred even without that. I bet you're right about the -re flag. I could probably also do some cleanup and remove that second -ac 2. I don't use ffmpeg very often and the commands are so long its hard to keep track of! I'll try and fix those issues tonight and see what happens :). – Jeremy Mar 7 '16 at 14:01
2

So, I went home and tried the new command with success. The only issue now is some buffering on the YouTube side, which is out of the scope of this question. The below command works fine, replace "REMOVED" with your YouTube channel ID.

sudo /opt/vc/bin/raspivid -o - -t 0 -fps 15 -h 720 -w 1280 -rot 270 | /usr/src/ffmpeg/ffmpeg -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -i /dev/zero -f h264 -i - -vcodec copy -acodec aac -ab 128k -g 50 -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/REMOVED

Here is an example with it fixed:

https://www.youtube.com/watch?v=7N6vzGwPhQI

Jeremy

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.