Showing posts with label map. Show all posts
Showing posts with label map. Show all posts

Sunday, December 08, 2013

lossless encoding from H264 to MPEGTS

For editing purposes, I needed a good way to losslessly encode video from my Canon 5D Mark II's H264 source to a format that allows me to concatenate multiple video files into one large one.  Only a few video formats have the ability to concatenate multiple files together: mainly mpg and mpegts.  FFmpeg is always getting better (thank you, guys!), and has a nice formula for doing this here:
https://trac.ffmpeg.org/wiki/How%20to%20concatenate%20(join,%20merge)%20media%20files

The formula relies on a specification from the H264 standard known as AnnexB.  Here are a couple articles about Annex B, though both are above my head:
http://aviadr1.blogspot.com/2010/05/h264-extradata-partially-explained-for.html
http://wiki.multimedia.cx/index.php?title=H.264

The H.264 spec itself:
http://www.itu.int/rec/T-REC-H.264-201304-I

My goal was to losslessly extract the video and audio from the individual videos I shot using the Canon and concatenate them all into one long-length video.

Here's what the Canon video file container looked like to FFprobe:
ffprobe $VIDEO 2>&1 | grep Stream
    Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuvj420p(pc, smpte170m), 1920x1080, 45694 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 60k tbc (default)
    Stream #0:1(eng): Audio: pcm_s16le (sowt / 0x74776F73), 48000 Hz, stereo, s16, 1536 kb/s (default)

Note the video stream is H264 and the audio stream is waveform PCM.

Extract the Video
I did this with an FFmpeg command line:
ffmpeg -i $VIDEO -c copy -bsf:v h264_mp4toannexb -f mpegts $NEWVIDEO

Note that you'll need the latest and greatest FFmpeg.  Using an older version of FFmpeg from RPM Fusion's repo )FFmpeg 1.0.8), I got a bunch of these errors when I tried to concatenate individual MPEGTS files.
[aac @ 0x6727e0] channel element 1.10 is not allocated
[aac @ 0x6727e0] More than one AAC RDB per ADTS frame is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
[aac @ 0x6727e0] channel element 3.15 is not allocated
[aac @ 0x6727e0] Error decoding AAC frame header.
[aac @ 0x6727e0] Reserved bit set.
[aac @ 0x6727e0] channel element 1.8 is not allocated
[aac @ 0x6727e0] channel element 2.0 is not allocated
[aac @ 0x6727e0] Sample rate index in program config element does not match the sample rate index configured by the container.
[aac @ 0x6727e0] Reserved bit set.
    Last message repeated 1 times
[aac @ 0x6727e0] Number of bands (4) exceeds limit (2).
[aac @ 0x6727e0] SSR not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
[mpegts @ 0x676640] decoding for stream 1 failed
[mpegts @ 0x676640] Could not find codec parameters for stream 1 (Audio: aac ([6][0][0][0] / 0x0006), 8 channels (FL+FR+FC+LFE+BL+BR+FLC+FRC), s16, 548 kb/s): unspecified sample rate
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, mpegts, from 'concat:test.ts|test2.ts':
  Duration: 00:03:14.17, start: 1.400000, bitrate: 131495 kb/s
  Program 1 
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
    Stream #0:0[0x100]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuvj420p, 1920x1088, 29.97 tbr, 90k tbn, 180k tbc
    Stream #0:1[0x101](eng): Audio: aac ([6][0][0][0] / 0x0006), 8 channels (FL+FR+FC+LFE+BL+BR+FLC+FRC), s16, 548 kb/s
[mpegts @ 0x672c20] sample rate not set

Once I downloaded the latest FFmpeg from FFmpeg's git repo

the errors disappeared.  Hooray for updated code!

Extract the Audio
My second task was to yank the PCM audio track from the Canon and save that off to a file for concatenation later on.  I did so using this command to extract only the audio stream (-c:a) from the source video file the Canon outputs:
ffmpeg -i $VIDEO -c:a copy $NEWAUDIO

Concatenate the Individual Video Files
Next, I combined the extracted video streams that were saved to MPEGTS format into one large file.  That command looked like this:
ffmpeg -i "concat:file1.ts|file2.ts|file3.ts" -c copy $FINAL.ts

Concatenate the Individual Audio Files
I did the same for the audio:
ffmpeg -i "concat:file1.wav|file2.wav|file3.wav" -c copy $FINAL.wav

Edit to Taste
Knowing my goal was to have as close as lossless video and audio, I did some audio sweetening in Audacity and reencoded the $FINAL.wav as an MP2 audio file.  This is lossy, but
1) the MPEGTS container prefers MPEG audio codecs.
2) FFmpeg doesn't yet have an encoder for MPEG-4 ALS lossless just yet.

Combine the Long-length Video and Audio Streams
Lastly, I combined both lossless video and lossy audio streams using this command into a:
ffmpeg -i $FINAL.mp2 -i $FINAL.ts -c:a copy -c:v copy -map 0:0 -map 0:1 $COMBINED.ts

The -map feature in FFmpeg is the best thing since sliced bread:
http://ffmpeg.org/ffmpeg.html#Advanced-options

You can take an audio or video stream from one file and combine it with an audio or video stream from another file to save into a new file.  This assumes the container format supports those codecs.  Sweet!

Sync Issue Averted
I noticed that if I combined the individual audio and video streams into files that were greater than an hour and a half long, FFmpeg would introduce audio-visual sync issues towards the end of that long duration video.  My solution was to combine the audio and video streams into smaller transport stream files first and then combine those smaller, complete files into one big file.  This way, I had no a/v sync issue.

Anyway, that's my latest adventure into the world of lossless video editing using my Canon and FFmpeg.

Enjoy.
CM

Sunday, April 05, 2009

animated route in Cinelerra

This was fun. I spent the day perfecting a way to automate a line on a map in Cinelerra. You might think that was a somewhat pedantic exercise, but I think the image I used was very pretty and that the moving line, ala Raiders of the Lost Ark, came out great. What would make it even better would be to use an ancient map of some sort.

Here it is:

a line on a map from crazed mule on Vimeo.

Update 2009/04/07
For some reason, this video is not playing as embedded on this page. Please visit my crazed mule profile on Vimeo to view.

Thanks!
*** end update ***

Using Gimp to Spice Things Up
I created the graphics in Gimp:
-the line representing the route and its shadow
-the circle representing the route's start
-the star representing the route's end

The circle and the star were created using Gfig, the Gimp add-on utility that let's you create geometric shapes. Also note that the shadow of the line matches the position of the light source in the photo of the globe.

Note that the circle and the star are not flat, 2D creations, but they look like stickers pasted on the side of the globe. I acheived that effect by using Gimp's Perspective and Shear tools. Here's a resource that discusses Perspective in Gimp:
http://gimp-university.blogspot.com/2008/03/perspective-and-layers.html

I created four images to import in Cinelerra:
1) globe with no Gimp object overlays
2) globe with just the circle as start of route
3) globe with the circle and the line
4) globe with the circle, line and star representing the full trip

Assembling the Images in Cinelerra
The tracks in Cinelerra looked like this:
Top Video Track
image 1 (plain globe) at beginning of timeline and image 4 (all objects) at end of timeline
Bottom Video Track
image 2 (globe and circle) and image 3 (globe, circle and line)


Gradient Created for Line's Movement
The key to the movement of the route was a screen wipe that travelled from the upper left corner of the screen to the lower right, mimicking the direction of the line's travel. Since Cinelerra does not have a built in wipe that moves in this direction, I had to create my own gradient using Gimp and plop it in /usr/local/lib/cinelerra/shapewipe. I then used that gradient in the Shape Wipe video transition tool:


In the timeline picture above, you can see the Shape Wipe transition effect that I used between the image of the map with the circle and the image of the map with the circle and the line.

Here are some resources on wipes and making your own wipe in Cinelerra:
http://cvs.cinelerra.org/transitions.php
http://www.mail-archive.com/cinelerra@skolelinux.no/msg05664.html
http://akiradproject.net/your_own_transition
http://cvs.cinelerra.org/images/

I love the way this turned out, because it looks a s*1tload better than most of the other animated routes I've seen out there. In fact, it blows away the lame route create with Photoshop and After Effects that I read about in VideoMaker magazine this month.
http://www.videomaker.com/article/14206/

enjoy,
the mule