Showing posts with label video encoding. Show all posts
Showing posts with label video encoding. Show all posts

Monday, March 23, 2015

Few filters from ffmpeg filter library

If you are using WinFF with Display Cmd Line option, then from DVD preset you already know how scaling look like. For example

-vf scale=720:480

That would be width and height of output. With Additional Options activated WinFF will allow you to specify crop values. Unfortunately it will place them in wrong order if you are doing crop and scale. Placing crop in front of scale is preferable.

-vf crop=iw-50-10:ih-20-20:50:20,scale=720:480

Crop parameters are width, height and start point. It calculates it from input with, height and parameters which you have entered. BTW if you need to crop and scale inside complex filter that looks like this:

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


Filter expressions are separated with coma as in non complex case.
Beside size and crop, commonly used filter is unsharp. With unsharp filter output may be sharper or softer, blured. It accepts six parameters and those are default values:

-vf unsharp=5:5:1.0:5:5:0.0

Parameters are, in order of aperance, luma matrix size, horizontal and vertical, odd integers between 3 and 63. Next we have luma effect strength which is float. Then we have the same but for chroma. Positive values for effect strength will sharpen image, negative will blur it and zero will disable effect. For example scale and make it sharper:

-vf scale=960:540,unsharp=9:9:0.75:9:9:0.75

If we want to blur image we will make floats negative.
Another filter which we may want to use is curves. Those are the same ones which we know from GIMP, Curves Tool under Colors. While describing curves may be somewhat complicated, there is number of presets available and they are:

none
color_negative
cross_process
darker
increase_contrast
lighter
linear_contrast
medium_contrast
negative
strong_contrast
vintage


We can even chain them if required. So, if we want lighter output image:

-vf scale=960:540,curves=lighter

More ambitious users will take a look at man pages and maybe decide to create their own presets. According to documentation ffmpeg can import curves from photoshop but I can not confirm that since I am not using it.

Sunday, March 22, 2015

Join two videos using ffmpeg and fade filter

Imagine that you retrieved your video from your camera, tablet, mobile phone, Internet and converted it to mpeg. After resizing and encoding of 43.8 Mbps 1920x1080 input video produced by Canon camera we can end up with some more modest resolution. Using Avidemux we can precisely find and cut segments from one keyframe to another. You can append those segments like this:

ffmpeg -i sa0.mpg -i sa1.mpg -filter_complex \
 "[0:0] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1 [v] [a]" \
 -map '[v]' -map '[a]' -c:v mpeg2video -q:v 2 out.mpg


We want video quality 2, we want mpeg video codecs, and we want both video and audio to be in output. Audio will be default mp2 to match video, since we didn't specify any. ffmpeg will print to standard output how it “understands” our instructions:

Output #0, mpeg, to 'out.mpg':
  Metadata:
    encoder         : Lavf55.3.100
    Stream #0:0: Video: mpeg2video, yuv420p, 960x540 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 90k tbn, 29.97 tbc
    Stream #0:1: Audio: mp2, 44100 Hz, stereo, s16, 128 kb/s


Whatever we didn't specify ffmpeg will or pick up from input video or adjust to match output container. I assume here that both videos are from the same original video and for that reason I didn't resize or resample videos, if that is required one can do it as preprocessing. That will append our two videos but it will be rather abrupt and not very smooth event. Luckily ffmpeg got rich library of filters and we can fade out and in to make smooth transition between two segments. We can fade just video or we can fade both, video and audio.

ffmpeg -i sa0.mpg\
 -vf fade=out:8928:36 -af afade=t=out:st=297.5:d=1.5\
 -c:v mpeg2video -q:v 2 output1.mpg


This is the first segment and we want to fade out last 36 frames and also last 1,5 seconds of audio. Video fade parameters are expressed in frames, start at frame and fade duration in frames, audio afade is about the same but in seconds. If we do not want to fade audio track we will omit audio filter story. Now we want to fade in next segment:

ffmpeg -i sa1.mpg\
 -vf fade=in:0:36 -af afade=t=in:st=0:d=1.5\
 -vcodec mpeg2video -q:v 2 output2.mpg


Starting from zero we fade in 36 frames of video and 1,5 seconds of audio signal. After we join those two segments we will have smooth fade out and in transition:

ffmpeg -i output1.mpg -i output2.mpg -filter_complex \
 "[0:0] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1 [v] [a]" \
 -map '[v]' -map '[a]' -c:v mpeg2video -q:v 2 out.mpg


The same as we did without fading.

Monday, December 31, 2012

Download YouTube video and convert it for Android

For downloading videos from YouTube I am using YouTubeDownloader2 and you can find it here http://sourceforge.net/projects/ytd2/. Why I am using that and not using Firefox add-ons? YouTubeDownloader2 is standalone program written in Java which is not storing my browsing habits in some remote DB. How do I know that? That is OSS, downloaded code, checked what it does, compiled it. If you don’t do Java, trust others who do ;-)
Once video is on file system we can encode it. It should be easy on Linux unless you are have common repo FFmpeg which is compiled without support for H.264, mp3 and few others codecs. If you are experienced Linux user you will visit FFmpeg wiki where is detailed instruction https://ffmpeg.org/trac/ffmpeg/wiki/UbuntuCompilationGuide how to configure and build FFmpeg. I am on Mint so that is valid set of instructions for me, people using other distro will look for their set of instructions. If you are not experienced user maybe mencoder which comes by default with everything enabled is better option. So, install libavcodec-extra-53 or maybe 54 and mencoder. We can ask mencoder what is supported if in terminal we execute:

~ $ mencoder -ovc help
MEncoder svn r34540 (Ubuntu), built with gcc-4.6 (C) 2000-2012 MPlayer Team

Available codecs:
copy     - frame copy, without re-encoding. Doesn't work with filters.
frameno  - special audio-only file for 3-pass encoding, see DOCS.
raw      - uncompressed video. Use fourcc option to set format explicitly.
nuv      - nuppel video
lavc     - libavcodec codecs - best quality!
xvid     - XviD encoding
x264     - H.264 encoding


and it produces video codecs list. For audio, query looks like this:

~ $ mencoder -oac help
MEncoder svn r34540 (Ubuntu), built with gcc-4.6 (C) 2000-2012 MPlayer Team

Available codecs:
   copy     - frame copy, without re-encoding (useful for AC3)
   pcm      - uncompressed PCM audio
   mp3lame  - cbr/abr/vbr MP3 using libmp3lame
   lavc     - FFmpeg audio encoder (MP2, AC3, ...)
   faac     - FAAC AAC audio encoder


There is small problem with mencoder, there is no WinFF for it and one have to do quite a bit of reading to assemble proper set of parameters. To save you from learning, here is what I do:

~ $ mencoder -of lavf -lavfopts format=mp4 -oac lavc -ovc lavc -lavcopts vbitrate=480:aglobal=1:vglobal=1:acodec=libfaac:abitrate=64:vcodec=mpeg4:keyint=25 -ofps 15 -af lavcresample=44100 -vf harddup,scale=480:320 -mc 0 "/home/yourlogin/in.flv" -o "/home/yourlogin/out.mp4"

I want H.263 video and AAC audio in MP4 container. Video should have bitrate up to 480kb/s and must be resized to 480x320 pixels, also frame rate is 15 frames per second.  For audio requested was resampling at 44.1 KHz and bit rate up to 64kb/s. It is not requirement that input video must be FLV, it should work for any container which you can find at YouTube. If your screen is different from 480x320, you may want to resize video differently. For example for 320x240 screen, vbitrate=256 and scale=320:240 should be good choice.
Not so nice as WinFF but not very complicated either.