Wednesday, June 30, 2010

FFMPEG - Transcoding Commands

Getting information from a video file
ffmpeg -i video.avi

Turn X images to a video sequence
ffmpeg -f image2 -i image%d.jpg video.mpg
This command will transform all the images from the current directory (named image1.jpg, image2.jpg, etc…) to a video file named video.mpg.

So, if your image name is image-100 - image 199, your command will be 

ffmpeg -f image2 -i image-1%02d.jpg mp4

%02 means use 2 digit starting from 00 for the input file name

Turn a video to X images
ffmpeg -i video.mpg image%d.jpg
This command will generate the files named image1.jpg, image2.jpg, …
The following image formats are also availables : PGM, PPM, PAM, PGMYUV, JPEG, GIF, PNG, TIFF, SGI.

Encode a video sequence for the iPpod/iPhone
ffmpeg -i source_video.avi -acodec aac -ab 128 -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4
Explanations :
Source : source_video.avi
Audio codec : aac
Audio bitrate : 128kb/s
Video codec : mpeg4
Video bitrate : 1200kb/s
Video size : 320px par 180px
Generated video : final_video.mp4

Encode video for the PSP
ffmpeg -i source_video.avi -b 300 -s 320x240 -vcodec xvid -ab 32 -ar 24000 -acodec aac final_video.mp4
Explanations :
Source : source_video.avi
Audio codec : aac
Audio bitrate : 32kb/s
Video codec : xvid
Video bitrate : 1200kb/s
Video size : 320px par 180px
Generated video : final_video.mp4

Extracting sound from a video, and save it as Mp3
ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 sound.mp3
Explanations :
Source video : source_video.avi
Audio bitrate : 192kb/s
output format : mp3
Generated sound : sound.mp3

Convert a wav file to Mp3
ffmpeg -i son_origine.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 son_final.mp3

Convert .avi video to .mpg
ffmpeg -i video_origine.avi video_finale.mpg

Convert .mpg to .avi
ffmpeg -i video_origine.mpg video_finale.avi

Convert .avi to animated gif(uncompressed)
ffmpeg -i video_origine.avi gif_anime.gif

Mix a video with a sound file
ffmpeg -i son.wav -i video_origine.avi video_finale.mpg

Convert .avi to .flv
ffmpeg -i video_origine.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv video_finale.flv

Convert .avi to dv
ffmpeg -i video_origine.avi -s pal -r pal -aspect 4:3 -ar 48000 -ac 2 video_finale.dv
ffmpeg -i video_origine.avi -target pal-dv video_finale.dv

Convert .avi to mpeg for dvd players
ffmpeg -i source_video.avi -target pal-dvd -ps 2000000000 -aspect 16:9 finale_video.mpeg
Explanations :
target pal-dvd : Output format
ps 2000000000 maximum size for the output file, in bits (here, 2 Gb)
aspect 16:9 : Widescreen

Compress .avi to divx
ffmpeg -i video_origine.avi -s 320x240 -vcodec msmpeg4v2 video_finale.avi

Compress Ogg Theora to Mpeg dvd
ffmpeg -i film_sortie_cinelerra.ogm -s 720x576 -vcodec mpeg2video -acodec mp3 film_terminée.mpg

Compress .avi to SVCD mpeg2
NTSC format:
ffmpeg -i video_origine.avi -target ntsc-svcd video_finale.mpg
PAL format:
ffmpeg -i video_origine.avi -target pal-svcd video_finale.mpg

Compress .avi to VCD mpeg2
NTSC format:
ffmpeg -i video_origine.avi -target ntsc-vcd video_finale.mpg
PAL format:
ffmpeg -i video_origine.avi -target pal-vcd video_finale.mpg

Multi-pass encoding with ffmpeg
ffmpeg -i fichierentree -pass 2 -passlogfile ffmpeg2pass fichiersortie-2

Copy AV stream without re-encoding
ffmpeg -i video.mp4 -vcodec copy -acodec copy output.mp4
copy attribute will tell the multiplexer that the stream does not need to re-encode.
If the stream is not compatible for the output format, the stream will be ignore

Copy only video stream to output file
ffmpeg -i video.mp4 -vcodec copy -an output.mp4
-an tells ffmpeg not to include audio stream. You can use -vn for not including video stream

Rescale input video 

ffmpeg -i input.mp4 -vcodec libx264 -s 160x120 output.mp4

This will convert input.mp4 to H.264 with rescaling to 160x120 image size

Convert mp4 to MPEG-TS

ffmpeg -i input.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec libvo_aacenc output.ts


-vbsf h264_mp4toannexb is a filter on each frame. MPEG-TS requires start code in H.264 frames and not all H.264 frames in mp4 contains a start code. This filter will look at each frame and add necessary information to the frame prior putting into the MPEG-TS container

Tuesday, June 29, 2010

FFMPEG - Compiling with Cygwin Window

You need the following to compile FFMPEG on window. Cygwin seems to be an easier choice than MingW

1. Install your Cygwin with all the "Base" packages, plus the following "Devel" ones: binutils, gcc4-core, make, subversion, mingw-runtime, texi2html, diffutils

2. Load Cygwin and navigate to the ffmpeg source

3. type dos2unix configure to convert the file to dos compatible text

4. Type make distclean to clean the past distribution if any

5. Then run ./configure --enable-static --disable-shared to make a static build.

5. The current gcc4-core package is buggy and needs this flag to build shared libraries: ./configure --enable-shared --disable-static --extra-cflags=-fno-reorder-functions

6. Then perform make and make install

7. You should have all your builds at \Cygwin\usr\local folder

Alternatively, you can download nightly build from

Wednesday, June 16, 2010

C# - Timer Precision Issue

Timer precision issue doesn't only affect Java, it should affect most programming language. It is because when you pause a thread, it gives away its right and allow the OS to schedule their job.

Thus, Thead.Sleep/Monitor.Wait does not guarantee the sleep time.

To archive high resolution, you can try using multimedia timer such as QueryPerformanceCounter . This is good for perform elapse calculations.

Or you can use timeBeginPeriod to requests a minimum resolution for periodic timers.

For example, to reduce the resolution to 1 ms

internal static extern uint timeBeginPeriod(uint period);
internal static extern uint timeEndPeriod(uint period);

Thread.Sleep(1); // will sleep 1ms every time

Java - Timer Precision Issue

When you have more experience with Thread.sleep/Object.wait, you will soon realize that this method often sleep more than you want.
Java does not guarantee the resolution archived. Java uses underlying OS interrupt to trigger timed event and this interrupt are often in the resolution of 10ms. For example, System.currentTimeMillis return the clock time in millisecond. However, the update of this clock reference is often same as time interrupt (10ms). 
This issue is a problem for application that requires high timer resolution. 
So, Java provides a switch -XX:ForceTimeHighResolution to force the OS time interrupt to 1ms interval. It will set OS interrupt to 1ms when VM starts and reset to original value when VM exit.
But, it seems that -XX:ForceTimeHighResolution is not perfect and is a use at your own risk switch ( Also, setting OS interrupt to 1ms may have big effect on OS performance. 
In general, you can use System.currentTimeMillis for getting absolute time. If you are calculating elapse time, you should use System.nanoTime
To see your Win OS interrupt/s, do the following:
You can see what interrupt period is being used in Windows by running the perfmon tool. After you bring it up you'll need to add a new item to watch (click the + icon above the graph - even if it appears grayed/disabled). Select the interrupts/sec items and add it. Then right click on interrupts/sec under the graph and edit its properties. On the "data" tab, change the "scale" to 1 and on the graph tab, the vertical max to be 1000. Let the system settle for a few seconds and you should see the graph drawing a steady line. If you have a 10ms interrupt then it will be 100, for 1ms it will be 1000, for 15ms it will be 66.6, etc. Note: on a multiprocessor system show the interrupts/sec for each processor individually, not the total - one processor will be fielding the timer interrupts.

Wednesday, June 9, 2010

MS Outlook and AVAST 5.0 Hangs

If you encounter MS Outlook busy writing to disk and hang, please check if you have Avast 5.0 and above installed. If Avast is installed, it has a very high chance that Avast is not working well with MS Outlook together.

The best solution is to uninstall Avast and stop the hangs.

On top of that, you should consider repairing your Outlook after you have uninstalled Avast.

1. Go to :\Program Files\Microsoft Office\Office12\SCANPST.exe to repair your PST folder. Your pst file should be contained at C:\Documents and Settings\\Local Settings\Application Data\Microsoft\Outlook\Outlook.pst. Repairing PST takes several minutes.

2. Next, run Microsoft Office Diagnostics to repair Office issues. You can find this application at Microsoft Office Tools start menu.

3. Run MS Outlook, you should see that your outlook will not grind your harddisk after these operations.

Wednesday, June 2, 2010

ADTS - Audio Data Transport Stream Frame

ADTS is an audio interchange format for storage and transporting audio object.

A ADTS structure contains a fixed header, variable header and a error check

Most importantly, ADTS contains decoding information for decoder. If you wants to initialize a decoded with specific information, you will requires information in the fixed header

Below is the layout for fixed header in

Syncword 12 bits
ID 1 bits
Layer 2 bits
protection_absent 1 bits
Profile_ObjectType 2 bits
sampling_frequency_index 4 bits
private_bit 1 bits
channel_configuration 3 bits
original/copy 1 bits
home 1 bits
Emphasis 2 bits

Full information can be located at ISO/IEC 14496-3 Annex 1.A