Encoding from command line

From Alessandro's Wiki
(Redirected from MPLAYER MENCODER)
Jump to: navigation, search

elwood _musicali # for a in ls ./*.* ; do ffmpeg -y -i "$a" -acodec mp3 -f flv /scratch/`echo "$a"|sed -e s@' '@'_'@g`; done


Quick commands to Encode/Decode Multimedia files



Rip DVD to DivX

  1. count dvd titles, this example is using 8 of them:
  2. audio:
mencoder dvd://1 -ovc frameno -o frameno.avi -oac mp3lame -lameopts vbr=3 -alang es -chapter 1-8
  1. video.
  2. last command output will suggest a vbitrate=xxx value:
mencoder dvd://1 -ovc lavc -lavcopts vcodec=mpeg4:vhq:v4mv:vbitrate=1000:vpass=1 -oac copy -o movie.avi -chapter 1-8
mencoder dvd://1 -ovc lavc -lavcopts vcodec=mpeg4:vhq:v4mv:vbitrate=1000:vpass=2 -oac copy -o movie.avi -chapter 1-8
  1. divx will be movie.avi.

Rip DVD generic

modo I

mencoder /dev/dvd-hdd -o CINEMA_STRANGE.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4

modo II ~15 Min.

mencoder dvd://1 -ovc frameno -o frameno.avi -oac mp3lame -lameopts vbr=3 -alang es -chapter 1-8

~100 Min.

mencoder dvd://1  -ovc lavc -lavcopts vcodec=mpeg4:vhq:v4mv:vbitrate=1130:vpass=1 -oac copy -o movie.avi -chapter 1-8 
mencoder dvd://1  -ovc lavc -lavcopts vcodec=mpeg4:vhq:v4mv:vbitrate=1130:vpass=2 -oac copy -o movie.avi -chapter 1-8 

DVD 4:3

mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd -vf scale=720:576 \
-srate 48000 -af lavcresample=48000 -lavcopts vcodec=mpeg2video -ofps 25 \
-o /fileout.mpg /filein.avi

PAL Widescreen

mencoder -of mpeg -mpegopts format=dvd:vaspect=16/9:vframerate=25 -srate 48000 \
-ofps 25 -ovc lavc -oac lavc -lavcopts \
codec=mpeg2video:vrc_buf_size=1835:keyint=15:vrc_maxrate=9800:vbitrate=4900:aspect=16/9:acodec=ac3:abitrate=192 \
-o /fileout.mpeg2  /filein.mpg

PAL Fullscreen

mencoder -of mpeg -mpegopts format=dvd:vaspect=4/3:vframerate=25 -srate 48000 \
-ofps 25 -ovc lavc -oac lavc -lavcopts \
codec=mpeg2video:vrc_buf_size=1835:keyint=15:vrc_maxrate=9800:vbitrate=4900:aspect=4/3:acodec=ac3:abitrate=192 \
-o /fileout.mpeg2  /filein.mpg

Encode DVD title #2, only selected chapters:

mencoder dvd://2 -chapter 10-15 -o title2.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4

Encode DVD title #2, resizing to 640x480:

mencoder dvd://2 -vf scale=640:480 -o title2.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4

Encode DVD title #2, resizing to 512xHHH (keep aspect ratio):

mencoder dvd://2 -vf scale -zoom -xy 512 -o title2.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4

The same, but with bitrate set to 1800kbit and optimized macroblocks:

mencoder dvd://2 -o title2.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4:mbd=1:vbitrate=1800

The same, but with MJPEG compression:

mencoder dvd://2 -o title2.avi -oac copy -ovc lavc -lavcopts vcodec=mjpeg:mbd=1:vbitrate=1800
ffmpeg -i  inputfile.mov  -acodec mp2 -ar 44100 -ac 2 -ab 112000 -vcodec mjpeg -qscale 2  output.mpg


  • to add filters to ffmpeg or mencoder, use the option -vf


  • with mencoder
mencoder video.mpg -oav copy -ovc copy -vf scale=120:100 -o scaled.mpg
  • with ffmpeg
ffmpeg -i <file> -vf scale=<x>:<y> <file-cropped>
  1. x and y are pixel values
  2. If value is 0, the input size is used
  3. If value is -1, maintains the aspect ratio
    this can brake the process having the proportional side size not divisible by 2 (x264 codecs)
  4. default is 0.
"not divisible by 2" (x264 issue)
  • trying to upscale a video from 854x480 [PAR 1:1 DAR 427:240] to -vf scale=-1:720 , got this message:

width or height not divisible by 2 (1281x720)

  • I'm running periodically a script encoding clips for a video server, using multiple formats for every clip and scaling them respectively to x:360 , x:480 , x:720 , the common " -vf scale:-1:360 " gives "not divisible by 2" error for some input/output sizes, the solution has been found with a formula:


  • in words: scale the height is proportionally to the new width plus the rest of the division by two of the proportional size. That means that, for example we take a 480 x 352 video and we scale it to 360, the new width (without my formula) would have been 491 (giving the codec an error) the formula divides it by 2 and ad adds the rest ( 1 ) to the output width.

  • these are the calculations I've made to reach the solution:
 a = 320 / 240 = 1,3333 
 1080 * 1,3333 = 1439,8   ceil = 1440
 720 * 1,3333  = 959,9999 ceil = 960
 480 * 1,3333  = 639,9    ceil = 640 
 360 * 1,3333  = 479,9    ceil = 480    
 a = 352 / 288 = 1,2222 
 1080 * 1,2222 = 1,319    ceil = 1320
 720 * 1,2222  = 879,9999 ceil = 880
 480 * 1,2222  = 586,6    ceil = 587   ERR
 360 * 1,2222  = 439,9    ceil = 440    
 a = 480 / 352 = 1,3636
 1080 * 1,3636 = 1472,6  ceil = 1473   ERR
 720 * 1,3636  = 981,7   ceil = 982
 480 * 1,3636  = 654,5   ceil = 655    ERR
 360 * 1,3636  = 490,8   ceil = 491    ERR
 a = 480 / 264 = 1,8181
 1080 * 1,8181 = 1963,5  ceil = 1964  
 720 * 1,8181  = 1309,03 ceil = 1310
 480 * 1,8181  = 872,6   ceil = 873    ERR
 360 * 1,8181  = 654,51  ceil = 655    ERR
 # final expression:


mplayer -vf crop=470:350:5:100 "$fileee"
  • tagliare un video dal secondo minuto (120) fino a 20 secondi dopo
mencoder  -ss 120 -endpos 20 video.avi -o video_tagliato.avi -ovc lavc

Tagliare solo i primi 30 min (1800 sec.)

mencoder -endpos 1800 rec-CUATRO-20070213-01:30.avi -o /mirror160/_video/__Temp/rec-CUATRO-20070213-01:30_tagliato.avi -ovc copy -oac copy


Registrare dal DVB (Digital Video Broadcasting)

  • Satellite o digitale terrestre.
mencoder dvb://CUATRO  -ovc copy -oac copy -o Film.avi


mplayer -vo mpegpes -ao mpegpes -vop lavc=5000:25,expand=688:576:-1:-1:1,scale=688:563 \
 -cache 8192 -slave -nolirc  -subpos 80 -sub-bg-color 0 -sub-bg-alpha 255 -quiet

Guardo la webcam con mplayer

  • v4l2
mplayer -cache 128 -tv driver=v4l2:width=800:height=480:outfmt=i420:device=/dev/video0 -vo xv tv://
  • v4l
mplayer tv:// -tv driver=v4l:width=640:height=480:outfmt=rgb24:device=/dev/video0:noaudio -flip

recording webcam (old Video4Linux 1) with mencoder...

  • good quality
mencoder tv:// -tv driver=v4l:width=640:height=480:outfmt=rgb24:device=/dev/video0:noaudio -flip \
-o camed.avi -ovc lavc
  • or less
mencoder  tv:// -tv driver=v4l:width=320:height=240:fps=15:outfmt=rgb24:device=/dev/video0:alsa:forceaudio \
-flip -ofps 11.44 -ovc lavc -lavcopts vcodec=rv10 -oac pcm -noskip -o prova.avi


extract a frame in JPEG

  • 1° frame
ffmpeg -i <file sorgente> -ss 00:00:00 -t 0.04 -f mjpeg <outputfile.jpg>

Generate gif89a (-gif animation) mplayer from a video

mplayer mov03331.mpg -loop 0 -vo gif89a:fps=15.0:output=caffe1.gif

create a video from image

ffmpeg -loop_input -vframes <number_of_frames> -i <input_image> <output_video>

ffmpeg -i INPUT.jpg -an -vcodec libx264 -coder 1 -flags +loop -cmp +chroma -subq 10 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -flags2 +dct8x8 -trellis 2 -partitions +parti8x8+parti4x4 -crf 24 -threads 0 -r 25 -g 25 -y OUTPUT.mp4


Save a stream

mplayer http://mplayer.hq/example.avi -dumpstream -dumpfile dumpato.avi

Grabando ,cortando y comprimiendo un stream de tv3

mplayer "mms://directe.3alacarta.tvcatalunya.com/reflector:60738?aifp=fhgt&WMCache=0" -dumpstream -dumpfile dumpato.avi
mencoder  -ss 826.5  dumpato.avi -o dumpato_tagliato.avi -ovc copy -oac copy

mencoder  -endpos 108  dumpato_tagliato.avi -o dumpato_montato.avi
mencoder  dumpato_montato.avi -o dumpato_copresv.avi  -oac copy  -ovc lavc -of mpeg -lavcopts vcodec=mpeg2video -ofps 1

Fare il dumpo di un stream (TV da internet per esempio) in XVID

mencoder mms://sream
-ovc xvid  -xvidencopts bitrate=1150 -vf scale=640:480 -oac mp3lame -o fileout.avi

...In DV

mencoder timegousbyconloli.avi -ovc libdv -vf scale=720:576 -oac pcm -o timegousbyconlolippp.avi

Ascoltare l'audio di mplayer in un host remoto: Vedi ESOUND

  • faccio partire il server audio di rete su elwood:
[xunil@elwood ~]$ esd -tcp -port 9876 -public &
[1] 3849
- accepting connections on port 9876
  • Apro il video (uno stream in questo caso) specificando host e porta del server audio
[xunil@zombie ~]$ mplayer -ao esd:elwood:9876 mms://rntlivewm.rai.it/_rn24_live_
  • così vedo il vido su zombie e sento l'audio su elwood, cioè vedo dalla cucina e sento dalla camera da letto, non proprio comodo:
  • allora trorno in camera, da elwood, e faccio:
[xunil@elwood ~]$ ssh -X zombie mplayer -ao esd:elwood:9876 mms://rntlivewm.rai.it/_rn24_live_
  • adesso la situazione è strana, una persona normale, sana non noterebbe differenza tra "fare doppio click" sul video o

Mandare l'audio su un altro host (elwood)

mplayer -ao esd:elwood:9876  r24-24luglio.avi

Motion JPEG audio PCM

il "2> /dev/null" serve per 'buttare' lo standard error, nel caso qualcosa non funzioni, rimuoverlo dal comando.

mencoder video.avi -oac pcm -ovc lavc -lavcopts vcodec=mjpeg -o MJPEG_video.avi 2> /dev/null

Motion PNG (MPNG)

mencoder mf://*.png -mf w=800:h=600:fps=25:type=png -ovc copy -oac copy -o output.avi

Motion TGA (MTGA)

mencoder mf://*.tga -mf w=800:h=600:fps=25:type=tga -ovc copy -oac copy -o output.avi


  • /usr/share/ffmpeg/libvpx-720p.ffpreset
  • ignored unless using -pass 2

ffmpeg -i input.mp4 -s 1280x720 -vpre libvpx-720p -b 3900k -pass 1 -an -f webm -y output.webm
ffmpeg -i input.mp4 -s 1280x720 -vpre libvpx-720p -b 3900k -pass 2 -acodec libvorbis -ab 128k -f webm -y output.webm

ffmpeg -pass 1 -passlogfile pr7.webm -qmin 5 -qmax 51 -i master/879.m2t -vcodec libvpx -b:v 104800 -s 320x200 -an -f webm -y NUL
ffmpeg -pass 2 -passlogfile pr7.webm -qmin 5 -qmax 51 -i master/879.m2t -vcodec libvpx -b:v 104800 -s 320x200 -acodec libvorbis -ac 1 -b:a 64k -y  video/pr7.webm
ffmpeg  -i master/743.MTS -s 360x240  -vpre libvpx-360p -pass 1 -passlogfile  log743_7.webm -qmin 5 -qmax 51  -b:v 500k -an -f webm -y video/743_7.webm
ffmpeg  -i master/743.MTS -s 360x240  -vpre libvpx-360p -pass 1 -passlogfile  log743_7.webm -qmin 5 -qmax 51  -b:v 500k -acodec libvorbis -ac 1 -b:a 96k -f webm -y video/743_7.webm


Convert TO theora

  • Examples: (from ffmpeg2theora help)
ffmpeg2theora videoclip.avi (will write output to videoclip.ogv)
ffmpeg2theora videoclip.avi --subtitles subtitles.srt (same, with subtitles)
cat something.dv | ffmpeg2theora -f dv -o output.ogv -
  • Encode a series of images:
ffmpeg2theora frame%06d.png -o output.ogv
  • Live streaming from V4L Device:
ffmpeg2theora /dev/video0 -f video4linux --inputfps 15 -x 160 -y 128 -o - \
| oggfwd icast2server 8000 password /theora.ogv
  • Live encoding from a DV camcorder (needs a fast machine):
dvgrab - | ffmpeg2theora -f dv -x 352 -y 288 -o output.ogv -
  • Live encoding and streaming to icecast server:
 dvgrab --format raw - \
  | ffmpeg2theora -f dv -x 160 -y 128 -o /dev/stdout - \
  | oggfwd icast2server 8000 password /theora.ogv

Informazioni mandando in Play da MPLAYER

Eseguendo il comando che segue , le opzioni '-v -identify' ci posso dare delle informazioni molto utili del video che mandiamo in PLAY:

 mplayer -v -identify -aspect 4:3 /root/Desktop/Prova03.avi  

EXAMPLES OF MENCODER USAGE (from ~$ man mencoder)

  • Encode all *.jpg files in the current directory:
mencoder "mf://*.jpg" -mf fps=25 -o output.avi -ovc lavc -lavcopts vcodec=mpeg4
  • Encode from a tuner (specify a format with -vf format):
mencoder -tv driver=v4l:width=640:height=480 tv:// -o tv.avi -ovc rawnuv


http://www.mplayerhq.hu/DOCS/HTML/en/index.html http://forum.doom9.org/archive/index.php/t-110761.html

  • mencoder


-pvr <opzione1:opzione2:...> (solo PVR)
Questa  opzione  imposta varie proprietà di codifica per il modulo di cattura PVR.  Deve essere usata con schede di codifica MPEG via hardware supp-
portate dal driver V4L2.  Le schede Hauppauge WinTV PVR-150/250/350/500 e tutte quelle basate su IVTV sono conosciute come schede  di  cattura  PVR.
Attenzione che solamente il kernel Linux 2.6.18 o maggiore è capace di gestire il flusso MPEG attraverso lo strato V4L2.  Per la cattura hardware di
un flusso MPEG e la visione attraverso MPlayer/MEncoder, usa ’pvr://’ come URL del filmato.
 Seleziona un formato MPEG per la codifica:
  ps:    MPEG-2 Program Stream (default)
  ts:    MPEG-2 Transport Stream
  mpeg1: MPEG-1 System Stream
  vcd:   flusso compatibile Video CD
  svcd:  flusso compatibile Super Video CD
  dvd:   flusso compatibile DVD

TS (Transport Stream)

mencoder -of lavf -lavfopts format=mpegts \
-lavcopts vcodec=mpeg2video:acodec=mp2:abitrate=224:vbitrate=3200 -o test2_mpegts_1109.m2t \
-ovc lavc -oac lavc /export/data/mediaroot/video/mid/1109.mp4

PES (Packetized Elementary Stream)


From Mpeg DVD (vob)

ffmpeg -i /mnt/win/linux/dvd_da_convertire/kidd-pivot-01.MPG -target dv /media/LACIE/temp/kidd-pivot-01.dv

3pg Mobile Phones

  • Nokia XpressMusic, display:320x240

mpeg4 libamr_nb

ffmpeg -i "$video" -s 320x240 -r 14.99 -vcodec mpeg4 -ac 1 -ar 8000 -acodec libamr_nb -b 80k -ab 10.2k -y $out

mpeg4 libamr_wb (va a scatti l'audio)

ffmpeg -i "$video" -s 320x240 -r 14.99 -vcodec mpeg4 -ac 1 -ar 8000 -acodec libamr_nb -b 224k -ab 12.2k 

mpeg4 libamr_wb (va a scatti l'audio)

ffmpeg -i "$video" -s 320x240 -r 14.99 -vcodec mpeg4 -ac 1 -ar 16000 -acodec libamr_wb -b 100k -ab 18.25k -y  
ffmpeg -i "$video" -s 320x240 -r 14.99 -vcodec mpeg4 -ac 1 -ar 16000 -acodec libamr_wb -b 80k -ab 12.65k -y 

h263p mp3 (non letto da cell Nokia per via dell'audio mp3)

ffmpeg -i "$video" -s qcif    -r 20    -vcodec h263p -acodec mp3 -ar 22050 -b 128k -ab 32k -y $out

H.264 (x264)




Very high quality

subq=6:partitions=all:8x8dct:me=umh:frameref=5:bframes=3:b_pyramid:weight_b	6fps	0dB

High quality

subq=5:8x8dct:frameref=2:bframes=3:b_pyramid:weight_b	13fps	-0.89dB


subq=4:bframes=2:b_pyramid:weight_b	17fps	-1.48dB
mencoder -v -o video.264
mencoder $ifile -of avi -vf scale=352:288 -ovc x264 \
-x264encopts bitrate=1000:subq=6:partitions=all:8x8dct:me=umh:frameref=5:bframes=3:b_pyramid:weight_b:threads=auto \
-o Desktop/test2641.avi

mencoder $ifile -of avi -ovc x264 -x264encopts bitrate=3000:subq=6:partitions=all:8x8dct:me=umh:frameref=5:bframes=3:b_pyramid:weight_b:threads=auto \
-o Desktop/test2641.avi
mencoder $ifile -of avi -ovc x264 \
-x264encopts bitrate=500:subq=5:8x8dct:frameref=2:bframes=3:b_pyramid:weight_b:threads=auto \
-o Desktop/test2641.avi


ffmpeg -i ../hi/1906.dv -vcodec libx264 -b 4096000 -t 10 -subq 6 -acodec libfaac -ar 48000 -ab 128000 -ac 2 -f mp4  1906_ffmpeg_Afaac_Vx264.mp4
ffmpeg -i ../hi/1906.dv -vcodec libx264 -b 4096000 -t 10 -subq 6 -acodec libfaac -ac 2 -f mp4  1906_ffmpeg_Afaac_Vx264.mp4

Working with PIPEs

  • esempio da omf file di avid
mkfifo videopipe

Aprire nuova shell:

cat videopipe | ffmpeg -i - -target dv output.dv

Toranre alla vecchia shell:

mencoder <filevideo.omf> -ovc copy -oac pcm -o videopipe

Quando i due hanno terminato il play e l'encoding:

unlink videopipe 
  • reccare e encodare DV
dvgrab --autosplit --format raw -|ffmpeg -f dv -i - -target vcd f.mpg
  • Encode from a pipe:
rar p test-SVCD.rar | mencoder -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=800 -ofps 24 -

Flash FLV

encode in flv (avi to flv) (mpg to flv) ffmpeg

ffmpeg -y -i dumpato2.avi -acodec mp3 -f flv dumpato2.flv
video_directory/ # for clip in ls ./*.* ; do ffmpeg -y -i "$clip" -acodec mp3 -f flv /scratch/`echo "$clip"|sed -e s@' '@'_'@g`; done


all aiff in a directory to mp3

for file in *.aiff ; do ffmpeg -i "$file" -f mp3 -ab 320000 -ar 48000 "$file.mp3" ; done
rename .aiff.mp3 .mp3 *.aiff.mp3

Problem -> Solution


  • convertendo in x264 con RedHat 5.2 il video in uscita risulta corrotto
    • is the x264 library that updatig casuses the problem

bit rate

  • values can be specified using 1M or 1024K or 1 048 576
    • -b 1024K
    • -minrate 800K
    • -maxrate 1400K
    • -bufsize 120K


cannot open shared object file: No such file or directory

ldd `which ffmpeg`
       libavdevice.so.52 => not found
       libavfilter.so.1 => not found
       libavformat.so.52 => not found
       libavcodec.so.52 => not found
       libswscale.so.0 => not found
       libavcore.so.0 => not found
       libavutil.so.50 => not found
       libm.so.6 => /lib/libm.so.6 (0x006c3000)
       libpthread.so.0 => /lib/libpthread.so.0 (0x007e9000)
       libc.so.6 => /lib/libc.so.6 (0x00575000)
       /lib/ld-linux.so.2 (0x00557000)
find /usr/local/lib/ | grep -E "libavdevice.so.52|libavfilter.so.1|libavcodec.so.52|libavcore.so.0"

FFmpeg Math expresisons

  • very powerful "under the hood" part of this software:

from ffmpeg documentation

  • item E, PI, PHI
    • the corresponding mathematical approximated values for e (euler number), pi (greek PI), phi (golden ratio)
  • in_w, in_h the input width and heigth
  • iw, ih same as @var{in_w} and @var{in_h}
  • out_w, out_h the output (cropped) width and heigth
    • ow, oh same as @var{out_w} and @var{out_h}
  • a input display aspect ratio, same as @var{iw} / @var{ih}
  • hsub, vsub horizontal and vertical chroma subsample values.
    • For example for the pixel format "yuv422p" @var{hsub} is 2 and @var{vsub} is 1.
  • If the input image format is different from the format requested by the next filter,
    • the scale filter will convert the input to the ratio of the input image.
  • default value of @var{width} and @var{height} is 0.
  • 'Some examples follow:
  • scale the input to 2x

same as

  • scale the input to half size
  • increase the width, and set the height to the same size
  • seek for Greek harmony

  • increase the height, and set the width to 3/2 of the height
  • increase the size, but make the size a multiple of the chroma
  • increase the width to a maximum of 500 pixels, keep the same input aspect ratio
scale='min(500\, iw*3/2):-1'


When evaluating an arithmetic expression, FFmpeg uses an internal formula evaluator, implemented through the `libavutil/eval.h' interface.

An expression may contain unary, binary operators, constants, and functions.

Two expressions expr1 and expr2 can be combined to form another expression "expr1;expr2".

expr1 and expr2 are evaluated in turn, and the new expression evaluates to the value of expr2.

The following binary operators are available: +, -, *, /, ^.

The following unary operators are available: +, -.

The following functions are available:

    Return 1.0 if x is NAN, 0.0 otherwise. 
`mod(x, y)'
`max(x, y)'
`min(x, y)'
`eq(x, y)'
`gte(x, y)'
`gt(x, y)'
`lte(x, y)'
`lt(x, y)'
  • `st(var, expr)'
    • Allow to store the value of the expression expr in an internal variable. var specifies the number of the variable where to store the value, and it is a value ranging from 0 to 9. The function returns the value stored in the internal variable.
  • `ld(var)'
    • Allow to load the value of the internal variable with number var, which was previosly stored with st(var, expr). The function returns the loaded value.
  • `while(cond, expr)'
    • Evaluate expression expr while the expression cond is non-zero, and returns the value of the last expr evaluation, or NAN if cond was always false.
  • `ceil(expr)'
    • Round the value of expression expr upwards to the nearest integer. For example, "ceil(1.5)" is "2.0".
  • `floor(expr)'
    • Round the value of expression expr downwards to the nearest integer. For example, "floor(-1.5)" is "-2.0".
  • `trunc(expr)'
    • Round the value of expression expr towards zero to the nearest integer. For example, "trunc(-1.5)" is "-1.0".
  • `sqrt(expr)'
    • Compute the square root of expr. This is equivalent to "(expr)^.5".
  • `not(expr)'
    • Return 1.0 if expr is zero, 0.0 otherwise.
  • `pow(x, y)'
    • Compute the power of x elevated y, it is equivalent to "(x)^(y)".
* works like AND
+ works like OR
if A then B else C

is equivalent to

A*B + not(A)*C