Wednesday, October 13, 2010

ffmpeg commands

FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video. It includes libavcodec – the leading audio/video codec library. FFmpeg is free software and is licensed under the LGPL or GPL depending on your choice of configuration options.
FFmpeg supports most of the popular formats, we don’t need to worry a lot about that. Formats supported by FFmpeg include MPEG, MPEG-4 (Divx), ASF, AVI, Real Audio/Video and Quicktime. To see a list of all the codecs/formats supported by FFmpeg, run the following command:

ffmpeg -formats

1. X11 grabbing

FFmpeg can grab the X11 display.
ffmpeg -f x11grab -s cif -i :0.0 /tmp/out.mpg
0.0 is display.screen number of your X11 server, same as the DISPLAY environment variable.
ffmpeg -f x11grab -s cif -i :0.0+10,20 /tmp/out.mpg
0.0 is display.screen number of your X11 server, same as the DISPLAY environment variable. 10 is the x-offset and 20 the y-offset for the grabbing.
ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/outputFile.mpg

2. Convert Pictures To Movie

First, rename your pictures to follow a numerical sequence. For example, img1.jpg, img2.jpg, img3.jpg,… Then you may run:
ffmpeg -f image2 -i img%d.jpg /tmp/a.mpg
Notice that `%d’ is replaced by the image number.
`img%03d.jpg' means the sequence `img001.jpg', `img002.jpg', etc…
If you have large number of pictures to rename, you can use the following command to ease the burden. The command, using the bourne shell syntax, symbolically links all files in the current directory that match *jpg to the `/tmp' directory in the sequence of `img001.jpg', `img002.jpg' and so on.
x=1; for i in *jpg; do counter=$(printf %03d $x); ln "$i" /tmp/img"$counter".jpg; x=$(($x+1)); done
If you want to sequence them by oldest modified first, substitute $(ls -r -t *jpg) in place of *jpg.
Then run:
ffmpeg -f image2 -i /tmp/img%03d.jpg /tmp/a.mpg
The same logic is used for any image format that ffmpeg reads.

3. Video Conversions

Quick and dirty convert to flv
ffmpeg -i inputfile.mp4 outputfile.flv
This converts any media ffmpeg handles to flash. It would actually convert anything to anything, it’s based on the file extension. It doesn’t do ANY quality control, sizing, etc, it just does what it thinks is best.
Convert .flv to .3gp
ffmpeg -i file.flv -r 15 -b 128k -s qcif -acodec amr_nb -ar 8000 -ac 1 -ab 13 -f 3gp -y out.3gp
Download YouTube videos as .flv and convert them to .3gp for your mobile phone.
Convert AVI to iPhone MP4
ffmpeg -i [source].avi -f mp4 -vcodec mpeg4 -b 250000 -s 480?320 -acodec aac -ar 24000 -ab 64 -ac 2 [destination].mp4
for 4:3 aspect:
ffmpeg -i source-xvid.avi -s 480x320 -aspect 4:3 -b 768k -ab 64k -ar 22050 -r 30000/1001 OUT.mp4
for 16:9:
ffmpeg -i source-xvid.avi -s 480x320 -aspect 16:9 -b 768k -ab 64k -ar 22050 -r 30000/1001 OUT.mp4
Create a video that is supported by youtube:
ffmpeg -i mymovie.mpg -ar 22050 -acodec libmp3lame -ab 32K -r 25 -s 320x240 -vcodec flv
mytarget.flv
Takes an mpeg video and coverts it to a youtube compatible flv file.
The -r 25 sets the frame rate for PAL, for NTSC use 29.97

4. Audio Conversion

Convert RM file to mp3
ffmpeg -i input.rm -acodec libmp3lame -ab 96k output.mp3
Adjust the bitrate (-ab) as necessary. If omitted FFmpeg will use a default of 64 kb/s.
Converting WMV to MP3 using FFMPEG
ffmpeg -i audio1.wmv audio1.mp3
This will convert audio1.wmv file to audio1.mp3
Converting WMV to FLV using FFMPEG

ffmpeg -i audio1.wmv audio1.flv
This will convert audio1.wmv file to audio1.flv, this will generate only audio content
Converting AMR to MP3 using FFMPEG

ffmpeg -i audio1.amr -ar 22050 audio1.mp3
This will convert audio1.amr file to audio1.mp3 having audio rate 22.05 Khz
Converting aac to mp3 using FFMPEG

ffmpeg -i audio1.aac -ar 22050 -ab 32 audio1.mp3
This will convert audio1.aac to audio1.mp3 having audio rate 22.05 Khz and Audio BitRate 32Khz
Converting aac to mp3 using FFMPEG with MetaData

ffmpeg -i audio1.aac -ar 22050 -ab 32 -map_meta_data audio1.mp3:audio1.aac audio1.mp3
This will convert audio1.aac to audio1.mp3 having audio rate 22.05 Khz and Audio BitRate 32Khz and will copy the meta data from .aac file to .mp3 file

5. Audio Extraction

ffmpeg -i video.avi -f mp3 audio.mp3
Dumping Audio stream from flv (using ffmpeg)
ffmpeg -i input.flv -f mp3 -vn -acodec copy ouput.mp3

6. Record Audio and Video from webcam

To record video run ffmpeg with arguments such as these:
ffmpeg -f video4linux2 -s 320x240 -i /dev/video0 out.mpg
To record both audio and video run ffmpeg with arguments such as these:
ffmpeg -f oss -i /dev/dsp -f video4linux2 -s 320x240 -i /dev/video0 out.mpg

7. Copy Only A Part Of Video

Cut out a piece of film from a file. Choose an arbitrary length and starting time.
ffmpeg -vcodec copy -acodec copy -i orginalfile -ss 00:01:30 -t 0:0:20 newfile
-vcodec, you choose what video codec the new file should be encoded with. Run ffmpeg -formats E to list all available video and audio encoders and file formats.
copy, you choose the video encoder that just copies the file.
-acodec, you choose what audio codec the new file should be encoded with.
copy, you choose the audio encoder that just copies the file.
-i originalfile, you provide the filename of the original file to ffmpeg
-ss 00:01:30, you choose the starting time on the original file in this case 1 min and 30 seconds into the film
-t 0:0:20, you choose the length of the new film
newfile, you choose the name of the file created.

8. Join Multiple Video Files

A few multimedia containers (MPEG-1, MPEG-2 PS, DV) allow to join video files by merely concatenating them.
Hence you may concatenate your multimedia files by first transcoding them to these privileged formats, then using the humble cat command (or the equally humble copy under Windows), and finally transcoding back to your format of choice.
mkfifo orig1.mpg
mkfifo orig2.mpg
ffmpeg -i input1.avi -sameq -y orig1.mpg
ffmpeg -i input2.avi -sameq -y orig2.mpg
Merge files
cat orig1.mpg orig2.mpg | ffmpeg -f mpeg -i - -vcodec copy -acodec copy merged.mpg
Merge and convert to avi
cat orig1.mpg orig2.mpg | ffmpeg -f mpeg -i - -sameq -vcodec mpeg4 -acodec libmp3lame merged.avi
Notice that you should either use -sameq or set a reasonably high bitrate for your intermediate and output files, if you want to preserve video quality.
Also notice that you may avoid the huge intermediate files by taking advantage of named pipes, should your platform support it:

9. Removing Synchronization Problems Between Audio and Video

ffmpeg -i source_audio.mp3 -itsoffset 00:00:10.2 -i source_video.m2v target_video.flv
This assumes that there is a 10.2 sec delay between the video and the audio (delayed).
To extract the original video into a audio and video composites look at the command on extracting audio and video from a movie
Here is more information of how to use ffmpeg:
http://www.ffmpeg.org/ffmpeg-doc.html


ffmpeg is a multiplatform, open-source library for video and audio files. Useful and amazing commands covering almost all needs: video conversion, sound extraction, encoding file for iPod or PSP, and more.

Getting infos 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.
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 input -acodec aac -ab 128kb -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
Or:
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
Find a webhost with ffmpeg enabled

No comments: