How to remove an audio track from an avi file

If you want to remove an unwanted audio track from a dual audio avi movie file, you can use the following command with Mencoder:

mencoder original_dual_audio_file.avi -o target_file.avi -oac copy -ovc copy -aid 2

or with Ffmpeg:

ffmpeg -i original_dual_audio_file.avi -vcodec copy -map 0:0 -acodec copy -map 0:2 target_file.avi

These will create a file named target_file.avi retaining only the second audio track (aid 2).

FFprobe how to

This one line command will search for all avi video files in a volume (dvd) with find, analyzes the specs with ffprobe, and filters the output through grep

find /media/cdrom/ -name '*.avi' -exec ffprobe {} \; 2>&1 | grep -F -f patternfile

Notes:

2>&1 is necessary because the ffprobe output is in stderr, not stdout

patternfile contains all the patterns to be matched by grep, one in each line.

How to remove annoying tags from every srt subtitle file

Sites like opensubtitles.org have the annoying habit of tagging their files with subtitles like “Tip for download: Open Subtitles MKV Player” and “Downloaded from http://www.opensubtitles.org”.

Here is a fast single command to get rid of all that on all subrip (.srt) files, using find, sed, and a bit of regex:

find . -name \*.srt -execdir sed -i '/pen.*ubtitles/d' {} +

I did it for dozens of files in one command ;)

find” searches all subdirectories below the current one (.) for every file with the srt extension (-name \*.srt) and executes the sed command that deletes on those files ({} +) the lines matching the regex pattern (pen.*ubtitles).

 

x264 presets/tunes and HandBrake

Taken from the handbrake.fr forum:

This information is up-to-date for HandBrake 0.9.5. It may no longer apply to future versions.
HandBrake 0.9.5 does not natively support x264 presets and tunes. However, nothing prevents you from re-creating them manually.
You can specify most x264 options (nearly all options parseable by libx264’s x264_param_parse function) via HandBrake’s advanced panel:

* –option value becomes option=value

* Options are separated by a colon.

* Options that do not require a parameter can be specified as either option or option=1
The MacGUI’s advanced panel will automatically add =1 if you don’t specify a value.

* In most cases, no-<something>=1 and <something>=0 are equivalent.

This isn’t true for deblock – to disable it, you must use no-deblock=1; deblock=0 is the same as deblock=0,0 (the default)

A list of all available x264 options can be found on MeWiki.

PRESETS

Formatted for HandBrake’s advanced panel (getting rid of redundant options; e.g. –bframes 0 implies –b-adapt 0), we get:

ultrafast

ref=1:bframes=0:cabac=0:8x8dct=0:weightp=0:me=dia:subq=0:rc-lookahead=0:analyse=none:trellis=0:aq-mode=0:no-deblock=1:scenecut=0:mbtree=0

superfast

ref=1:mixed-refs=0:weightp=1:subq=1:rc-lookahead=0:analyse=i8x8,i4x4:trellis=0:mbtree=0

veryfast

ref=1:mixed-refs=0:weightp=1:subq=2:rc-lookahead=10:trellis=0

faster

ref=2:mixed-refs=0:weightp=1:subq=4:rc-lookahead=20

fast

ref=2:weightp=1:subq=6:rc-lookahead=30

slow

ref=5:b-adapt=2:direct=auto:me=umh:subq=8:rc-lookahead=50

slower

ref=8:b-adapt=2:direct=auto:me=umh:subq=9:rc-lookahead=60:analyse=all:trellis=2

veryslow

ref=16:bframes=8:b-adapt=2:direct=auto:me=umh:merange=24:subq=10:rc-lookahead=60:analyse=all:trellis=2

placebo

ref=16:bframes=16:b-adapt=2:direct=auto:me=tesa:merange=24:subq=10:rc-lookahead=60:analyse=all:trellis=2:no-fast-pskip=1

–slow-firstpass can be set by disabling “Turbo First Pass” in the Video panel.

The official x264 presets are meant to be good speed / compression efficiency tradeoffs. The options that are most useful to compression efficiency are enabled first; the less useful options are enabled in the slowest presets (i.e. once all the most useful options are already enabled).

If you’re using settings slower than the placebo preset, you’re doing it wrong (the difference won’t be noticeable).

Note: if you’re starting with an official HandBrake preset for your device, be sure to add these settings before the preset’s options (so that the HandBrake options override the x264 preset – the other way around will likely break compatibility with your device).

Some settings should be safe to override with the x264 preset values (no guarantee, however):

* b-adapt (Adaptive B-Frames)
* direct (Adaptive Direct Mode)
* me (Motion Estimation Method)
* subme/subq (Subpixel ME & Mode Decision)
* merange (Motion Estimation Range)
* aq-strength (Adaptive Quantization)
* psy-rd (Psychovisual Rate Distorsion, Psychovisual Trellis)
* partitions/analyse (Partition Types)
* trellis (Trellis)
* deblock (Deblocking)*
* rc-lookahead
* no-fast-pskip

* Some very weak devices may require that deblocking be disabled

The following settings are NOT safe to override:

* ref (Reference Frames)
* bframes (Maximum B-Frames)
* cabac (CABAC Entropy Coding)
* 8x8dct (8×8 Transform)
* weightp (Weighted P-Frames)
* b-pyramid (Pyramidal B-Frames)
* weightb
* mixed-refs
* vbv-bufsize and vbv-maxrate

(meaning that you should not enable them if they are disabled in the HandBrake preset, or increase the value)

The official x264 tunes for r1834 are:

–tune                  Tune the settings for a particular type of source or situation

Overridden by user settings.

Multiple tunings are separated by commas.
Only one psy tuning can be used at a time.

TUNES

In HandBrake’s advanced panel:

* film: optimizes settings for film sources

Set Deblocking to -1,-1
Set Psychovisual Trellis to 0.15 (leaving Psychovisual Rate Distorsion untouched)

* animation: optimizes settings for cartoons and other animated content

Set Deblocking to 1,1
Set Psychovisual rate Distorsion to 0.4 (leaving Psychovisual Trellis untouched)
Set Adaptive Quantization to 0.6
Optional: if B-Frames are enabled, add 2 B-Frames to Maximum B-Frames*
Optional: if using more than one Reference Frame(s), double the number of Reference Frames*

* While these changes are part of the tune, they’re likely to break device compatibility
Leave B-Frames and Reference Frames untouched unless you know what you’re doing

* grain: optimizes settings to preserve grain

Set Deblocking to -2,-2
Set Psychovisual Trellis to 0.25 (leaving Psychovisual Rate Distorsion untouched)
Enable No DCT Decimation
Add the following options to the option string:

deadzone-inter=6:deadzone-intra=6:ipratio=1.1:pbratio=1.1:qcomp=0.8

* stillimage: optimizes settings for videos made of multiple still frames lasting several seconds

Set Deblocking to -3,-3
Set Adaptive Quantization to 1.2
Set Psychovisual Rate Distorsion to 2.0 and Psychovisual Trellis to 0.7

* psnr: optimizes the settings for best PSNR (hurts perceived quality, not useful for encodes you intend to watch

Add the following options to the option string:

aq-mode=0:psy=0

* ssim: optimizes the settings for best SSIM (hurts perceived quality, not useful for encodes you intend to watch)

Add the following options to the option string:

aq-mode=2:psy=0

* fastdecode: optimizes settings for faster decoding on weak devices

Disable CABAC Entropy Coding
Disable Weighted P-Frames
Add the following options to the option string:

weightb=0:no-deblock=1

* zerotatency: minimizes encoding latency (useful for live streaming, which HandBrake doesn’t do)

Set Maximum B-Frames to 0
Add the following options to the option string:

mbtree=0:rc-lookahead=0:sync-lookahead=0:sliced-threads=1

CFR (Constant Framerate) is set automatically by HandBrake when required
Using –force-cfr will likely cause audio/video sync issues

Join multiple AVI or MPEG videos into a single file with mencoder

I have some videos I wish to concatenate into a single one, without re-encoding.

It’s very easy with mencoder.

If they are avi:

mencoder -forceidx -ovc copy -oac copy part1.avi part2.avi part3.avi -o total.avi

-forceidx forces to rebuild the index, for seeking.

But if they are MPG files i should use the MPEG container (mencoder uses avi as default, regardless of file extension)

mencoder -forceidx -of mpeg -noskip -mc 0 -ovc copy -oac copy part1.mpg part2.mpg part3.mpg -o total.mpg

The mencoder manual advises the use of -noskip -mc 0 when muxing mpeg files.
For MPEG1 format this has to be specified, because MPEG2 is the default:

mencoder -forceidx -of mpeg -noskip -mc 0 -mpegopts format=mpeg1 -ovc copy -oac copy part1.mpg part2.mpg part3.mpg -o -o total.mpg

Rip a DVD and encode as Xvid with mencoder

  • Determine what is the longest title in the dvd. Usualy it’s the longest:
lsdvd -d /dev/dvd | grep Longest
  • Determine the croping to be applied to avoid encoding black bars:
mplayer dvd://[longest_title] -chapter 3 -vf cropdetect
  • Based on the correct aspect ratio, decide what will be the desired final resolution. Here are some examples:

480:368 gives AR=1.30:1
656:368 gives AR=1.78:1
720:304 gives AR=2.37:1
704:304 gives AR=2.32:1

  • I’m using 2-pass encoding and converting the sound to mp3. Here are the mencoder commands:
mencoder \

dvd://[title_to_encode] \

-vf crop=[cropping],scale=[final_resolution] \

-ovc xvid -xvidencopts chroma_opt:quant_type=mpeg:bitrate=[video_bitrate|-desired_size_in_kB]:pass=1 \

-oac copy \

-o /dev/null

mencoder \

dvd://[title_to_encode] \

-vf crop=[cropping],scale=[final_resolution] \

-ovc xvid -xvidencopts chroma_opt:quant_type=mpeg:bitrate=[video_bitrate|-desired_size_in_kB]:pass=2 \

-oac  mp3lame -lameopts preset=[audio_bitrate] \

-o [filename].avi