Imprimere i sottotitoli con Mencoder

Se avete un filmato a cui volete aggiungere dei sottotitoli imprimendoli nel flusso c’è un modo semplice e veloce per farlo: utilizzare mencoder. Mencoder è un codificatore/decodificatore sviluppato unitamente al famoso player Mplayer. Mencoder è un programma da linea di comando e non ha un’interfaccia grafica. Vi assicuro però che risulta essere superiore a molti tool con interfaccia grafica che potrete trovare in giro, anche per Windows e Mac.

Può sembrare ostico a prima vista, ma se ci si mette qualche minuto per capire come esso funzioni lo si può utilizzare agevolmente per codificare o decodificare flussi video. Se volete approfondire l’utilizzo di Mencoder potete dare un’occhiata alla guida di base e a quella avanzata.

Video Hardsub

Per effettuare quello che viene chiamato hardsub dei sottotitoli bisogna ricodificare il flusso video inserendo i sottotitoli in formato srt, ass o altro. Affinché ciò avvenga in maniera desiderata bisogna specificare quale carattere utilizzare: deve essere presente il file subfont.ttf in “~/.mplayer” che definisce i caratteri da usare per i sottotitoli, mentre la grandezza dei caratteri si controlla con “-subfont-text-scale x”. Io vi invito ad utilizzare un carattere bold della famiglia Dejavu. Lo rinominate e lo posizionate nella specifica cartella.

Fatto ciò non rimane altro che ricodificare con i sottotitoli dando il comando:

mencoder FILE_ORIGINALE.avi -ovc xvid -xvidencopts fixed_quant=4 -o FILE_CON_SUB.avi -oac copy -sub -subfont-text-scale 3
  • fixed_quant=3 qualità del video (1 alta, 3 normale, 5 scarsa)
  • subfont-text-scale 4 grandezza del font da 1 a 5 (3 e 4 vanno benissimo)
  • subpos <0-100> posizione dei sottotitoli nello schermo in % rispetto altezza
  • subalign <0-2> 0 in alto, 1 al centro, 2 in basso
  • sub-bg-alpha <0-100> trasparenza dei sottotitoli, 0 massima trasparenza, 100 nessuna trasparenza. La trasparenza credo sia riferita alla banda nera su cui vengono aggiunti i sottotitoli e non ai font. Infatti non usando questa opzione non c’è nessuna banda nera (valore di default 0), mentre l’aggiunta del parametro con valore 100 (nessuna trasparenza) crea la banda nera.

Vediamo anche le opzioni del codec Xvid:

xvidenc (−xvidencopts)

There are three modes available: constant bitrate (CBR), fixed quantizer and two pass.


Specify the pass in two pass mode.

turbo (two pass only)

Dramatically speeds up pass one using faster algorithms and disabling CPU-intensive options. This will probably reduce global PSNR a little bit and change individual frame type and PSNR a little bit more.

bitrate=<value> (CBR or two pass mode)

Sets the bitrate to be used in kbits/second if <16000 or in bits/second if >16000. If <value> is negative, Xvid will use its absolute value as the target size (in kBytes) of the video and compute the associated bitrate automagically (default: 687 kbits/s).


Switch to fixed quantizer mode and specify the quantizer to be used.

zones=<zone0>[/<zone1>[/…]] (CBR or two pass mode)

User specified quality for specific parts (ending, credits, …). Each zone is <start-frame>,<mode>,<value> where <mode> may be

q Constant quantizer override, where value=<2.0−31.0> represents the quantizer value.
w Ratecontrol weight override, where value=<0.01−2.00> represents the quality correction in %.



Encodes all frames starting with frame 90000 at constant quantizer 20.


Encode frames 0−10000 at 10% bitrate, encode frames 90000 up to the end at constant quantizer 20. Note that the second zone is needed to delimit the first zone, as without it everything up until frame 89999 would be encoded at 10% bitrate.


This option controls the motion estimation subsystem. The higher the value, the more precise the estimation should be (default: 6). The more precise the motion estimation is, the more bits can be saved. Precision is gained at the expense of CPU time so decrease this setting if you need realtime encoding.


MPEG-4 uses a half pixel precision for its motion search by default. The standard proposes a mode where encoders are allowed to use quarter pixel precision. This option usually results in a sharper image. Unfortunately it has a great impact on bitrate and sometimes the higher bitrate use will prevent it from giving a better image quality at a fixed bitrate. It is better to test with and without this option and see whether it is worth activating.


Enable Global Motion Compensation, which makes Xvid generate special frames (GMC-frames) which are well suited for Pan/Zoom/ Rotating images. Whether or not the use of this option will save bits is highly dependent on the source material.


Trellis Quantization is a kind of adaptive quantization method that saves bits by modifying quantized coefficients to make them more compressible by the entropy encoder. Its impact on quality is good, and if VHQ uses too much CPU for you, this setting can be a good alternative to save a few bits (and gain quality at fixed bitrate) at a lesser cost than with VHQ (default: on).


Activate this if your encoded sequence is an anime/cartoon. It modifies some Xvid internal thresholds so Xvid takes better decisions on frame types and motion vectors for flat looking cartoons.


The usual motion estimation algorithm uses only the luminance information to find the best motion vector. However for some video material, using the chroma planes can help find better vectors. This setting toggles the use of chroma planes for motion estimation (default: on).


Enable a chroma optimizer prefilter. It will do some extra magic on color information to minimize the stepped-stairs effect on edges. It will improve quality at the cost of encoding speed. It reduces PSNR by nature, as the mathematical deviation to the original picture will get bigger, but the subjective image quality will raise. Since it works with color information, you might want to turn it off when encoding in grayscale.


Activates high-quality prediction of AC coefficients for intra frames from neighbor blocks (default: on).


The motion search algorithm is based on a search in the usual color domain and tries to find a motion vector that minimizes the difference between the reference frame and the encoded frame. With this setting activated, Xvid will also use the frequency domain (DCT) to search for a motion vector that minimizes not only the spatial difference but also the encoding length of the block. Fastest to slowest:

0 off
1 mode decision (inter/intra MB) (default)
2 limited search
3 medium search
4 wide search


Adaptive quantization allows the macroblock quantizers to vary inside each frame. This is a ’psychosensory’ setting that is supposed to make use of the fact that the human eye tends to notice fewer details in very bright and very dark parts of the picture. It compresses those areas more strongly than medium ones, which will save bits that can be spent again on other frames, raising overall subjective quality and possibly reducing PSNR.


Make Xvid discard chroma planes so the encoded video is grayscale only. Note that this does not speed up encoding, it just prevents chroma data from being written in the last stage of encoding.


Encode the fields of interlaced video material. Turn this option on for interlaced content.
Should you rescale the video, you would need an interlace-aware resizer, which you can activate with −vf scale=<width>:<height>:1.


minimum I-frame quantizer (default: 2)


maximum I-frame quantizer (default: 31)


minimum P-frame quantizer (default: 2)


maximum P-frame quantizer (default: 31)


minimum B-frame quantizer (default: 2)


maximum B-frame quantizer (default: 31)

min_key_interval=<value> (two pass only)

minimum interval between keyframes (default: 0)


maximum interval between keyframes (default: 10*fps)


Sets the type of quantizer to use. For high bitrates, you will find that MPEG quantization preserves more detail. For low bitrates, the smoothing of H.263 will give you less block noise. When using custom matrices, MPEG quantization must be used.


Load a custom intra matrix file. You can build such a file with xvid4conf’s matrix editor.


Load a custom inter matrix file. You can build such a file with xvid4conf’s matrix editor.

keyframe_boost=<0−1000> (two pass mode only)

Shift some bits from the pool for other frame types to intra frames, thus improving keyframe quality. This amount is an extra percentage, so a value of 10 will give your keyframes 10% more bits than normal (default: 0).

kfthreshold=<value> (two pass mode only)

Works together with kfreduction. Determines the minimum distance below which you consider that two frames are considered consecutive and treated differently according to kfreduction (default: 10).

kfreduction=<0−100> (two pass mode only)

The above two settings can be used to adjust the size of keyframes that you consider too close to the first (in a row). kfthreshold sets the range in which keyframes are reduced, and kfreduction determines the bitrate reduction they get. The last I-frame will get treated normally (default: 30).


Maximum number of B-frames to put between I/P-frames (default: 2).


quantizer ratio between B- and non-B-frames, 150=1.50 (default: 150)


quantizer offset between B- and non-B-frames, 100=1.00 (default: 100)


This setting allows you to specify what priority to place on the use of B-frames. The higher the value, the higher the probability of B-frames being used (default: 0). Do not forget that B-frames usually have a higher quantizer, and therefore aggressive production of B-frames may cause worse visual quality.


This option tells Xvid to close every GOP (Group Of Pictures bounded by two I-frames), which makes GOPs independent from each other. This just implies that the last frame of the GOP is either a P-frame or a N-frame but not a B-frame. It is usually a good idea to turn this option on (default: on).


This option is meant to solve frame-order issues when encoding to container formats like AVI that cannot cope with out-of-order frames. In practice, most decoders (both software and hardware) are able to deal with frame-order themselves, and may get confused when this option is turned on, so you can safely leave if off, unless you really know what you are doing.
This will generate an illegal bitstream, and will not be decodable by ISO-MPEG-4 decoders except DivX/libavcodec/ Xvid.
This will also store a fake DivX version in the file so the bug autodetection of some decoders might be confused.

frame_drop_ratio=<0−100> (max_bframes=0 only)

This setting allows the creation of variable framerate video streams. The value of the setting specifies a threshold under which, if the difference of the following frame to the previous frame is below or equal to this threshold, a frame gets not coded (a so called n-vop is placed in the stream). On playback, when reaching an n-vop the previous frame will be displayed.
Playing with this setting may result in a jerky video, so use it at your own risks!


This parameter controls the number of frames the CBR rate controller will wait before reacting to bitrate changes and compensating for them to obtain a constant bitrate over an averaging range of frames.


Real CBR is hard to achieve. Depending on the video material, bitrate can be variable, and hard to predict. Therefore Xvid uses an averaging period for which it guarantees a given amount of bits (minus a small variation). This settings expresses the “number of frames” for which Xvid averages bitrate and tries to achieve CBR.


size of the rate control buffer


This setting allows Xvid to take a certain percentage of bits away from high bitrate scenes and give them back to the bit reservoir. You could also use this if you have a clip with so many bits allocated to high-bitrate scenes that the low(er)-bitrate scenes start to look bad (default: 0).


This setting allows Xvid to give a certain percentage of extra bits to the low bitrate scenes, taking a few bits from the entire clip. This might come in handy if you have a few low-bitrate scenes that are still blocky (default: 0).


During pass one of two pass encoding, a scaled bitrate curve is computed. The difference between that expected curve and the result obtained during encoding is called overflow. Obviously, the two pass rate controller tries to compensate for that overflow, distributing it over the next frames. This setting controls how much of the overflow is distributed every time there is a new frame. Low values allow lazy overflow control, big rate bursts are compensated for more slowly (could lead to lack of precision for small clips). Higher values will make changes in bit redistribution more abrupt, possibly too abrupt if you set it too high, creating artifacts (default: 5).
This setting impacts quality a lot, play with it carefully!


During the frame bit allocation, overflow control may increase the frame size. This parameter specifies the maximum percentage by which the overflow control is allowed to increase the frame size, compared to the ideal curve allocation (default: 5).


During the frame bit allocation, overflow control may decrease the frame size. This parameter specifies the maximum percentage by which the overflow control is allowed to decrease the frame size, compared to the ideal curve allocation (default: 5).


Specifies a frame average overhead per frame, in bytes. Most of the time users express their target bitrate for video w/o taking care of the video container overhead. This small but (mostly) constant overhead can cause the target file size to be exceeded. Xvid allows users to set the amount of overhead per frame the container generates (give only an average per frame). 0 has a special meaning, it lets Xvid use its own default values (default: 24 − AVI average overhead).


Restricts options and VBV (peak bitrate over a short period) according to the Simple, Advanced Simple and DivX profiles. The resulting videos should be playable on standalone players adhering to these profile specifications.


no restrictions (default)


simple profile at level 0


simple profile at level 1


simple profile at level 2


simple profile at level 3


advanced simple profile at level 0


advanced simple profile at level 1


advanced simple profile at level 2


advanced simple profile at level 3


advanced simple profile at level 4


advanced simple profile at level 5


DXN handheld profile


DXN portable NTSC profile


DXN portable PAL profile


DXN home theater NTSC profile


DXN home theater PAL profile


DXN HDTV profile

NOTE: These profiles should be used in conjunction with an appropriate −ffourcc. Generally DX50 is applicable, as some players do not recognize Xvid but most recognize DivX.


Specifies the Pixel Aspect Ratio mode (not to be confused with DAR, the Display Aspect Ratio). PAR is the ratio of the width and height of a single pixel. So both are related like this: DAR = PAR * (width/height).
MPEG-4 defines 5 pixel aspect ratios and one extended one, giving the opportunity to specify a specific pixel aspect ratio. 5 standard modes can be specified:


It is the usual PAR for PC content. Pixels are a square unit.


PAL standard 4:3 PAR. Pixels are rectangles.


same as above


same as above


same as above (Do not forget to give the exact ratio.)


Allows you to specify your own pixel aspect ratio with par_width and par_height.

NOTE: In general, setting aspect and autoaspect options is enough.

par_width=<1−255> (par=ext only)

Specifies the width of the custom pixel aspect ratio.

par_height=<1−255> (par=ext only)

Specifies the height of the custom pixel aspect ratio.

aspect=<x/y | f (float value)>

Store movie aspect internally, just like MPEG files. Much nicer solution than rescaling, because quality is not decreased. MPlayer and a few others players will play these files correctly, others will display them with the wrong aspect. The aspect parameter can be given as a ratio or a floating point number.


Same as the aspect option, but automatically computes aspect, taking into account all the adjustments (crop/expand/scale/ etc.) made in the filter chain.


Print the PSNR (peak signal to noise ratio) for the whole video after encoding and store the per frame PSNR in a file with a name like ’psnr_hhmmss.log’ in the current directory. Returned values are in dB (decibel), the higher the better.


Save per-frame statistics in ./xvid.dbg. (This is not the two pass control file.)

The following option is only available in Xvid 1.1.x.


This setting allows vector candidates for B-frames to be used for the encoding chosen using a rate distortion optimized operator, which is what is done for P-frames by the vhq option. This produces nicer-looking B-frames while incurring almost no performance penalty (default: 1).

The following option is only available in the 1.2.x version of Xvid.


Create n threads to run the motion estimation (default: 0). The maximum number of threads that can be used is the picture height divided by 16.

La documentazione sui codec potete trovarla alla pagina del man di mplayer. Per utilizzare al meglio una CPU multicore basta aggiungere come opzione threads=NUMERO_CORE. Nel nostro caso bisognerebbe modificare il codice come segue:

mencoder FILE_ORIGINALE.avi -ovc xvid -xvidencopts fixed_quant=4:threads=NUMERO_CORE -o FILE_CON_SUB.avi -oac copy -sub -subfont-text-scale 3

tags Tags:
categorieCategorie: Linux, Multimedia

Forge Of Empires Italia

Comments are closed.