Saturday, March 21, 2015

Encoding with ffmpeg

Join two videos

Unless one have production ambitions, encoding on Linux is rather simple and user friendly. Install ffmpeg, WinFF and get presets for WinFF. Thanks to WinFF menu option Display Cmd Line one can see what arguments are passed to ffmpeg to achieve this or that. For example you want 30 seconds from one and 30 seconds from another place in video.

ffmpeg -i input.mp4 -filter_complex \
"[0:v]trim=500:530,setpts=PTS-STARTPTS[v0]; \
 [0:a]atrim=500:530,asetpts=PTS-STARTPTS[a0]; \
 [0:v]trim=900:930,setpts=PTS-STARTPTS[v1]; \
 [0:a]atrim=900:930,asetpts=PTS-STARTPTS[a1]; \
 [v0][a0][v1][a1]concat=n=2:v=1:a=1" \
 -c:v libx264 output.mkv

 Start at 500 seconds, use 30 seconds of video and audio and then from 900 seconds do the same. Concat output and encode using lib264. Filters trim and atrim will help you with ref frames, the first is video and the second one is audio version. Next we have setpts and asetpts, again video and audio version. We want PTS Presentation Time Stamp to start from zero. Finally we have concat filter where we specify two parts to be concatenated into one video and one audio. What are square brackets? Those at the start of line are saying take video or audio from input array at position zero. Since we have only one video all left tags are zero. Right tags are our friendly names to segments, we use them at the end to say how to concat segments to output.
If you see something like this, when you try concating two segments:

[AVFilterGraph @ 0x2e9be00] No such filter: 'trim'
Error configuring filters.


Then you download source, build and install ffmpeg. For more precise editing one may want to use Avidemux and for that video files must be converted to mpeg. WinFF have DVD settings which you are going to use. It will not specify audio conversion parameters, if you do not want default ones you just append this to what WinFF says:

-acodec mp2 -ar 44100 -ab 128k -ac 2

That is use mp2 codec, sampling 44.1KHz, bit rate 128Kbps and number of channels. With Avidemux you can do frame by frame, variable speed searching and so on.

Video in video

In existing video you want to insert some kind of detail in upper right corner.

ffmpeg -i input.mpg -i detail.mpg -filter_complex \
  "[0:v] setpts=PTS-STARTPTS, scale=800x640 [big];\
  [1:v] setpts=PTS-STARTPTS, scale=240x192 [detail];\
  [big][detail] overlay=shortest=1:x=560" -c:v libx264 out.mkv


Here we resize both videos, tag them with friendly names and for overlay specify where we want detail to start. Since we didn't specify y it will be zero. That shortest=1 means when end of shortest input is reached, stop encoding. Audio signal will be copied from the first video input.

Mosaic of several videos

This one is nicely explained at Create a mosaic out of several input videos at ffmpeg site. I will not bother you with explanation which you can read there but I am going to give you command to create 6x2 mosaic:

ffmpeg -i a.mpg -i b.mpg -i c.mpg -i d.mpg -i e.mpg -i f.mpg -filter_complex\
 "nullsrc=size=1920x960 [base];\
 [0:v] setpts=PTS-STARTPTS, scale=640x480 [upperleft];\
 [1:v] setpts=PTS-STARTPTS, scale=640x480 [uppermiddle];\
 [2:v] setpts=PTS-STARTPTS, scale=640x480 [upperright];\
 [3:v] setpts=PTS-STARTPTS, scale=640x480 [lowerleft];\
 [4:v] setpts=PTS-STARTPTS, scale=640x480 [lowermiddle];\
 [5:v] setpts=PTS-STARTPTS, scale=640x480 [lowerright];\
 [base][upperleft] overlay=shortest=1 [tmp1];\
 [tmp1][uppermiddle] overlay=shortest=1:x=640 [tmp2];
 [tmp2][upperright] overlay=shortest=1:x=1280 [tmp3];\
 [tmp3][lowerleft] overlay=shortest=1:y=480 [tmp4];\
 [tmp4][lowermiddle] overlay=shortest=1:x=640:y=480 [tmp5];\
 [tmp5][lowerright] overlay=shortest=1:x=1280:y=480"\
 -c:v libx264 output.mkv


You will need large monitor to enjoy your mosaic. Again you will have audio from the first video in input. If combining audio makes sense, I would rather extract audio and mix tracks in Audacity where levels can be adjusted. Now with example at ffmpeg wiki and this one I guess one can easily create different overlays like large video left and two half size ones on the right and similar.

No comments:

Post a Comment