Video Tools - Tipps and Tricks |
Here you can find some maybe helpful C++ example programs and bash commands to manipulate (cut, resize, convert, ...) videos under linux.
FFmpeg C/C++ Example Code |
You can download my C/C++ source code example software ffmpegReader (v2009-11-11 GPL) which uses the current SVN ffmpeg version to read, seek, and process single frames from a videofile. The source code can be used as an example if you want to write your own interface. You need to download and install first FFmpeg SVN version and libargtable2. It also contains a README and INSTALL instruction file.
To try the compiled example program just run the ffmpeg info option:
- ffmpeg_use inputfile.mpg -I
This will return only some maybe usefull information about the length, the streams, frame numbers, fps, etc. Output example:
Input #0, mpegvideo, from 'inputfile.mpg': Duration: 00:00:03.0, start: 0.000000, bitrate: 1228 kb/s Stream #0.0: Video: mpeg1video, yuv420p, 336x312, 1228 kb/s, 30.00 fps(r) Full Duration: 3.019600 Total Frames: 90
Bash Scripts |
- mplayer / mencoder:
- play your USB webcam raw modus or resized and mirrored:
- mplayer -rawvideo on:fps=15:w=640:h=480 /dev/video1
- mplayer tv:// -tv driver=v4l:device=/dev/video1:width=640:height=480:noaudio -x 800 -y 600 -vf-add pp=hb:a/vb:a,hqdn3d,mirror
- play your video cam (plugged into the S-VHS entry of a TV-card)
- mplayer tv:// -tv driver=v4l:device=/dev/video1:input=2:noaudio:width=352:height=288:fps=25:normid=0
- often searched and seldom used:
- quite and in background: mplayer [options] inputfile &> /dev/null &
- slow motion: mplayer -speed 0.3 inputfile
- slow motion can also be used as a workaround for xv -wait sec with sec smaller than 1.0. Just write a simple bash script like mplayer mf://${FRAMES} -speed ${SPEED} in combination with a noglob alias, e.g. alias xvwait='noglob /path/to/the/simple/script', and then try e.g. xvwait images/*/*.png 0.3
- record from your webcam or video cam (plugged into the S-VHS entry of a TV-card)
- mencoder -rawvideo on:fps=15:w=640:h=480 /dev/video1 -ovc lavc -ofps 15 -o outputfile.avi
- mencoder tv:// -tv device=/dev/video1:driver=v4l2:input=2:noaudio:width=352:height=288:fps=25:normid=0 -ovc lavc -ofps 25 -o outputfile.avi
- create movies from image sequences
- JPEG to AVI: mencoder -ovc lavc -mf fps=25:type=jpg 'mf://*.jpg' -vf harddup -ofps 25 -noskip -o outputfile.avi
- PNG to MPEG2 (framewise seekable as keyinterval set to 1): mencoder -ovc lavc -oac copy -mf fps=25 'mf://*.png' -of avi -lavcopts vcodec=mpeg2video:keyint=1:mbd=1:vqmin=2:vqmax=10:autoaspect -vf harddup -ofps 25 -noskip -o outputfile.avi
- PNG to XVID (can be used e.g. for PPT 2007): mencoder -ovc xvid -xvidencopts pass=2:bitrate=15999:max_bframes=0 -oac copy -mf fps=25:type=png 'mf://*.png' -vf harddup -ofps 25 -noskip -of avi -o outputfile.avi
- convert a video file of arbitrary format to a sequence of images
- JPEG: mplayer -nosound -vo jpeg inputfile
- PNG: mplayer -nosound -vo png:z=[0-9] inputfile
- Note that you can also split an animated GIF into its image components, i.e. mplayer -nosound -vo png animated.gif
- create a flash video (.flv) file for web streaming
- From a video source: mencoder inputfile -ffourcc FLV1 -of lavf -ovc lavc -lavcopts vcodec=flv:acodec=mp3:abitrate=56 -srate 22050 -o outputfile.avi
- From a sequence of images: mencoder mf://*.png -mf w=360:h=240:fps=12:type=png -of lavf -ffourcc FLV1 -ovc lavc -lavcopts vcodec=flv -o outputfile.avi
(or if you're using jpegs, use '*.jpeg' and 'type=jpeg')
- convert a video file of arbitrary format into another
- to a DIVX video (w/ sound, w/ exact frame seeking): mencoder inputfile -ffourcc DX50 -oac mp3lame -lameopts preset=standard -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=$BITRATE:autoaspect:keyint=$KEYINT:mbd=1:scplx_mask=0.1:vqmin=2:vqmax=10 -vf pp=lb,scale=$WIDTH:$HEIGHT -of avi -o outputfile.avi
(with e.g. BITRATE=1200, KEYINT=1, FPS=25, WIDTH=720, HEIGHT=576) - to a MPEG2 video (w/o sound, w/o exact frame seeking): mencoder inputfile -nosound -ovc lavc -lavcopts vcodec=mpeg2video:vbitrate=$BITRATE:autoaspect:keyint=$KEYINT:mbd=1:vqmin=2:vqmax=10 -vf scale=$WIDTH:$HEIGHT -of mpeg -o outputfile.avi
(with e.g. BITRATE=800, KEYINT=128, FPS=25, WIDTH=720, HEIGHT=576) - same format, but rotated video and a little bit brighter (e.g. when recorded with your digicam in a wrong way): mencoder inputfile.avi -oac pcm -ovc lavc -vf rotate=1,eq=20:20 -o outputfile-rotated.avi
- to a DIVX video (w/ sound, w/ exact frame seeking): mencoder inputfile -ffourcc DX50 -oac mp3lame -lameopts preset=standard -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=$BITRATE:autoaspect:keyint=$KEYINT:mbd=1:scplx_mask=0.1:vqmin=2:vqmax=10 -vf pp=lb,scale=$WIDTH:$HEIGHT -of avi -o outputfile.avi
- cut a video file (with 25 fps) using start/end time in seconds
- Extract frames: mplayer -ss starttime inputfile -frames `python -c "print int((endtime - starttime) * 25)"` -vo png
- Cut video: mencoder -oac copy -ovc copy -ss starttime -endpos endtime -o outputfile inputfile
- concatenate several video files of the same format
- mencoder -oac copy -ovc copy -o outputfile inputfiles*
- crop a video file using mplayer's cropdetect
- First autodetect the area to be cropped: mplayer -vf cropdetect inputfile
- Mplayer will output the most probably area to be cropped, e.g. [CROP] Crop area: X: 0..319 Y: 28..209 (-vf crop=320:176:0:32).0
- Then crop the movie using this inforamtion: mplayer inputfile -vf crop=320:176:0:32
- convert a quicktime mov video file into a mpeg1 video file using 2-pass encoding (w/o sound, w/ exact frame seeking)
- 1. first pass: mencoder inputfile -of avi -nosound -ovc frameno -vf harddup -o frameno.avi
- 2. second pass: mencoder inputfile -of avi -nosound -ofps 25 -ovc lavc -lavcopts vcodec=mpeg1video:keyint=1:autoaspect -vf harddup -o outputfile.avi
- after processing a video with your tools (i.e. usually a demuxing occurs), rebuild the video with sound (i.e. mux again)
- 1. process your video, ususally your tool will write each processed frame separately (e.g. as JPG files)
- 2. dump (demux) the audio stream from the original video: mplayer -ao pcm:file=audio.wav:fast -vo null inputfile
- 3. mux the single frames and the original audio stream together: mencoder "mf://*.jpg" -mf type=jpg:fps=25 -vf harddup -ofps 25 -noskip -ovc lavc -lavcopts vcodec=mpeg4 -audiofile audio.wav -o outputfile.avi
- play your USB webcam raw modus or resized and mirrored:
- FFmpeg:
- crop a video file which will have the same quality as the input file
- ffmpeg -i inputfile.avi -croptop 30 -cropbottom 30 -cropleft 0 -cropright 0 -sameq cropped.avi
- Cut a video file with arbritrary input format using ffmpeg (cutted video will have the same quality as the input file)
- ffmpeg -i inputfile -ss starttime -t duration -sameq outputfile
- Create a flash video (.flv) file for web streaming from a video source using ffmpeg
- ffmpeg -i inputfile -ab 56 -ar 22050 -b 500 -r 15 -s 320×240 outputfile.flv
- Create a snaphshot of a flash video (.flv) file for web streaming from a video source using ffmpeg
- ffmpeg -i inputfile -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320×240 snapshot.jpg
- Create a jpeg snapshot every 12.5 seconds (1/0.08) from a video source using ffmpeg
- ffmpeg -i inputfile -r 0.08 -s 320x240 f%06d.jpg
- Create a single jpeg snapshot from a video source using ffmpeg
- ffmpeg -i inputfile -f singlejpeg -t 0.001 -s 320x240 snapshot.jpg
- Create an MP4 video file from multiple jpeg source files using ffmpeg
- ffmpeg -r 25 -b 800k -i %03d.jpg outputfile.mp4 (at 800k/s compression)
- ffmpeg -r 25 -b 800k -i %03d.pgm outputfile.mp4 (also works with PGM files, opposed to mencoder)
- ffmpeg -r 25 -sameq -i %03d.jpg outputfile.mp4 (no compression)
- After exporting an iMovie project to an avi file, it might
happen that all video clips (e.g. avi, mov, xvid, ...)
imported to the iMovie application do not appear in the final
output. To be sure that everythings works fine, you should
convert your clips to the Sony DV format before importing
them to the iMovie application. This can be easily done by using
ffmpeg
- ffmpeg -i inputfile -target dv -aspect 16:9 outputfilename.dv
- If you have to work with panavision video clips
(e.g. 640x272 video files with an 2.35 aspect ratio), you have to add
black borders (letterboxing) before converting them to DV
format, as ffmpeg can either convert the videos to 4:3 or 16:9 aspect ratio in DV format.
- 1. padding: ffmpeg -i inputfile -padtop 44 -padbottom 44 -sameq paddedoutput
- 2. converting: ffmpeg -i paddedoutput -target dv -aspect 16:9 outputfilename.dv
- Another FFmpeg HowTo Page
- overlay an image over a video using ffmpeg
- ffmpeg -i inputfile.avi -sameq -vhook "/usr/lib/vhook/imlib2.so -x 10 -y 10 -i ./logo.png" -y outputfile.avi
- crop a video file which will have the same quality as the input file
- Other:
- play your webcam using xawtv
- xawtv -geometry 640x480-0+0 -device /dev/video1
- convert a sequence of 16-bit/color PNG images to 8-bit/color PNG images
- if you use montage to concatenate two images, e.g. montage -background black -geometry +4+4 left.png right.png result.png, where the two imput images were in 8-bit/color PNG format, the resulting PNG image will be in 16-bit/color image format. This might be a problem if you want to do this for a sequence of images in order to create a concatenated video, as mencoder cannot handle these 16-bit/color PNG images.
- 16-bit/color PNG to PNM to 8-bit/color PNG: for i in *.png ; do pngtopnm $i > a.pnm ; pnmtopng a.pnm > $i ; done
- demux VOB files
-
#!/bin/bash for file in *.VOB; do echo "demuxing " ${file} # video dump echo "dumping video ..." mplayer ${file} -dumpvideo &> /dev/null; mv stream.dump `echo ${file} | sed -e "s/VOB/m2v/"` ; # audio dump channel 1 echo "dumping audio channel 1 ..." mplayer ${file} -aid 128 -dumpaudio &> /dev/null; mv stream.dump `echo ${file} | sed -e "s/VOB/80.ac3/"` ; # audio dump channel 2 echo "dumping audio channel 2 ..." mplayer ${file} -aid 129 -dumpaudio &> /dev/null; mv stream.dump `echo ${file} | sed -e "s/VOB/81.ac3/"` ; done
-
- rip a single DVD chapter
- mplayer -dvd-device /dev/cdrom dvd://1 -chapter $CHAPTER -dumpstream; mv stream.dump outputfile.avi
- play your webcam using xawtv
Misc |
- You can download my xorg.conf which configures a
Fujitsu-Siemens ScaleoView W19-1 Widescreen 19'' flat panel at 1400x900 (or
1440x900) and the nvidia TV output. This config was tested on ubuntu dapper
6.06 release. You might also have a look at this
ubuntu forum page.
The nvidia driver must be installed before and should already work. Of
course you need root privileges to do all this and you should always do a
backup of your xorg.conf. e.g:
user@localhost:~$ sudo cp /etc/X11/xorg.conf /etc/X11/xorg.conf.bak
user@localhost:~$ gtf 1400 900 60 -x # 1400x900 @ 60.00 Hz (GTF) hsync: 55.92 kHz; pclk: 104.23 MHz Modeline "1400x900_60.00" 104.23 1400 1480 1632 1864 900 901 904 932 -HSync +Vsync
user@localhost:~$ sudo cp /etc/X11/xorg.conf.bak /etc/X11/xorg.conf
- If you don't know how to create a fancy LaTeX poster you should have a look at my beamerposter page.
- If you ever encounter compiler problems, floating point problems, etc. which you can not solve using e.g. valgrind, then you should have a look at the really great CompilerTricks page.
Philippe Dreuw Last modified: Thu May 19 17:47:24 CEST 2011 Disclaimer. Created Wed Dec 22 18:04:32 CET 2004