User avatar
HermannSW
Posts: 1392
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Howto upload raspivid high framerate videos to youtube

Sun Apr 01, 2018 7:45 pm

The video output format of raspivid is .h264 by default. If you upload such a video directly to youtube, it will allow for that (youtube does not allow to upload .mkv Matroska videos). But the video is played at constant framerate of 25fps, with no indication of the real timestamps.

In this posting I will show how to take high framerate raspivid videos (90fps with v1 camera, 120fps with older Raspbian or 180fps with newest Stretch with v2 camera), add readable millisecond precision time overlay to the video, cut out relevant part of it, convert it to 30fps video (the maximum youtube allows for in case video is less than 720p, which mode 7 VGA resolution is).

In posting viewtopic.php?f=43&t=206047&p=1294557#p1294557 I described the steps needed after raspivid video capture to associate each frame in video with its recorded timestamp.

Here I will show complete steps doing that, and finally upload to youtube as 30fps video. For the 180fps video I took that will result in 6x slower play speed than real, but the time overlay gives you exact information on time (youtube does not show sub second time).

  1. record 180fps VGA video and generate timestamp file:

    Code: Select all

    $ raspivid -md 7 -w 640 -h 480 -t 8000 -fps 180 -pts t.pts -o t.h264
  2. combine generated t.h264 and t.pts into Matroska video with timestamps:

    Code: Select all

    $ mkvmerge -o t.mkv --timecodes 0:t.pts t.h264
  3. post process that video for 90° rotation and (millisecond resolution) timeoverlay addition with gstreamer pipeline:

    Code: Select all

    $ gst-launch-1.0 -v filesrc location=t.mkv ! matroskademux ! h264parse ! avdec_h264 ! videoflip method=clockwise ! timeoverlay time-mode="buffer-time" shaded-background=true ! x264enc ! matroskamux ! filesink location=tt.mkv
  4. cut out the interesting part of the video generated sofar:

    Code: Select all

    $ ffmpeg -ss 0.1 -i tt.mkv -vframes 117 t2.mkv
  5. convert that video to 30fps fixed framerate video for youtube upload (you can take 25fps if you like as well):

    Code: Select all

    $ mkvmerge -o t30.mkv --default-duration 0:30fps t2.mkv
  6. youtube cannot upload Matroska video, convert to .avi for upload

    Code: Select all

    $ ffmpeg -i t30.mkv -c:v copy -c:a copy t30.avi
The video cut out ranges from 0.1s until 0.75s of original video (180*0.65=117). As you can see in this frame the millisecond resolution time overlay at top has this correlation to the time bar shown by youtube:

Code: Select all

0.1 + 0.481*6 = 2.986  ==>  "0:02" in youtube time bar
https://www.youtube.com/watch?v=RlsvHZV ... e=youtu.be
Image


Reminder:
After starting the video, stop it.
With right clicking you can select "Loop", that keeps the video playing again and again.
Instead of starting the video again, you can alternatively single step fore/back with keys "."/",".
Keeping the "," key pressed allows you to see video played backwards.
Clicking on "Settings" you can select playback speed in range 0.25 to 2.
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://gitlab.freedesktop.org/HermannSW/gst-template
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7138
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Howto upload raspivid high framerate videos to youtube

Sun Apr 01, 2018 8:30 pm

As I'm sure you're aware, the decompress and recompress will be lossy.
I don't know whether ffmpeg supports it, but all Android phones and iPhones will set the transformation matrix in the MP4 container to reflect the orientation, leaving the image in the native orientation of the sensor. You may be able to do the same trick here and avoid the lossy step. Then again you want to add your overlay, so unless you can do that with a subtitle track or similar, I guess you need access to the raw pixels.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
HermannSW
Posts: 1392
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Howto upload raspivid high framerate videos to youtube

Mon Apr 02, 2018 7:18 pm

I tried the lossless rotation part first.
While .mp4 might be rotated that way, .mp4 created from .mkv does not rotate if below commands are correct :(

Code: Select all

$ ffmpeg -i t.mkv  -codec copy t.mp4
$ ffmpeg -i t.mp4 -metadata:s:v rotate="90" -codec copy tr.mp4
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://gitlab.freedesktop.org/HermannSW/gst-template
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

User avatar
HermannSW
Posts: 1392
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Howto upload raspivid high framerate videos to youtube

Mon Apr 02, 2018 7:26 pm

This is the diff:

Code: Select all

$ od -vtx1 t.mp4 > t
$ od -vtx1 tr.mp4 > tr
$ diff t tr
180804,180805c180804,180805
< 13022060 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00
< 13022100 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00
---
> 13022060 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00
> 13022100 ff ff 00 00 00 00 00 00 00 00 00 00 01 e0 00 00
$ 

I trusted totem video player probably too much, since HTML5 video players can play .mp4 I opened tr.mp4 in Chrome browser and it is rotated compared to t.mp4 ...

P.S:
Just uploaded tr.mp4 test wise to youtube, it shows rotated there as well.
Next step is to see whether/how time overlay can be added lossless.
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://gitlab.freedesktop.org/HermannSW/gst-template
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7138
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Howto upload raspivid high framerate videos to youtube

Mon Apr 02, 2018 7:50 pm

I haven't studied the details, but that looks plausible based on the Android code for setting the transformation matrix
https://android.googlesource.com/platfo ... er.cpp#921
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
HermannSW
Posts: 1392
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Howto upload raspivid high framerate videos to youtube

Tue Apr 03, 2018 10:44 pm

6by9 wrote:
Sun Apr 01, 2018 8:30 pm
... Then again you want to add your overlay, so unless you can do that with a subtitle track or similar, I guess you need access to the raw pixels.
Just to confirm that I am not running into the wrong way.
  • I was able to rotate and upload to youtube with only changing metadata (lossless)
  • I found "lossless .h264 endoing"
    https://trac.ffmpeg.org/wiki/Encode/H.264#LosslessH.264
  • Although it states
    Note that lossless output files will likely be huge, and most non-FFmpeg based players will not be able to decode lossless. Therefore, if compatibility or file size are an issue, you should not use lossless
    I was able to upload such huge video to youtube successfully
  • I found how to add timeoverlay in ffmpeg, no need for gstreamer

So the question is, if I add rotation via metadata, decode for adding timeoverlay, but then encode to lossless .h264 (that takes a HUGE amount of time (and space)), and then upload that big file to youtube, am I right that these steps alltogether will result in a lossless upload of rotated video with timeoverlay to youtube?
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://gitlab.freedesktop.org/HermannSW/gst-template
https://github.com/Hermann-SW/fork-raspiraw
https://twitter.com/HermannSW

Return to “Camera board”