Difference between revisions of "FFmpeg DVD"

From Indie IT Wiki
Line 759: Line 759:
  
 
  mkvpropedit movie.mkv --edit info --set "title=The movie" --edit track:a1 --set language=fre --edit track:a2 --set language=ita
 
  mkvpropedit movie.mkv --edit info --set "title=The movie" --edit track:a1 --set language=fre --edit track:a2 --set language=ita
 +
 +
https://mkvtoolnix.download/doc/mkvpropedit.html#mkvpropedit.examples
  
 
== Extract The Subtitles From a VOB File ==
 
== Extract The Subtitles From a VOB File ==

Revision as of 10:11, 1 December 2021

Introduction

This page will show how to create a DVD and converting different popular video file formats.

Guidelines for Encoding

The guidelines for encoding questionably sourced video are called "scene guides". They are signed by various release groups who will make sure their videos comply to the guide, LOL being one of these groups.

The main web site for this information is Scene Rules and a few examples are:-

How can I get high quality/low size MP4s like the LOL release group?

Stream Mapping

Single Input

Copy the first video track and the second audio track from the INPUT to the OUTPUT...

ffmpeg -y -i 'input.mkv' -map 0:v:0 -map 0:a:1 -vcodec copy -acodec copy 'output.mkv'

Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:2 -> #0:1 (copy)

Copy the first video track and the second audio track and the third subtitle track from the INPUT to the OUTPUT...

ffmpeg -y -i 'input.mkv' -map 0:v:0 -map 0:a:1 -map 0:s:2 -vcodec copy -acodec copy -scodec copy 'output.mkv'

Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:2 -> #0:1 (copy)
  Stream #0:5 -> #0:2 (copy)

Multiple Inputs

Ffmpeg 2 inputs.png

Copy the video track from the first INPUT and the audio track from the second INPUT to the OUTPUT...

ffmpeg -y -i 'input_0.mp4' -i 'input_1.mp4' -map 0:v:0 -map 1:a:0 -vcodec copy -acodec copy 'output_0.mp4'

https://stackoverflow.com/questions/12938581/ffmpeg-mux-video-and-audio-from-another-video-mapping-issue

https://trac.ffmpeg.org/wiki/Map

The 4 Movie File Formats

  1. SD 480p XviD MPEG-4 AAC AVI
  2. HD 720p AVC X264 AC3 MP4
  3. FHD 1080p HEVC X265 DTS MKV
  4. UHD 2160p HEVC HDR 10 X265 True HD Dolby Atmos MKV

Sample Videos

Audio Formats

  1. AAC = Advanced Audio Codec = 2 channels @ 192 kb/s
  2. AC3 = Audio Codec 3 = 6 channels @ 384 kb/s
  3. E-AC3 = Enhanced Audio Codec 3 = 6 channels @ 640 kb/s
  4. DTS = Digital Theater Systems = 6 channels @ 1509 kb/s
  5. DTS-MA = Digital Theater Systems Master Audio = 8 channels @ 3813 kb/s
  6. A_TRUEHD = Dolby TrueHD with Dolby Atmos = 16 channels @ 5022 kb/s

Audio Tests - Best Sounding Dolby Atmos Movies

  1. Baby Driver
  2. Blade Runner 2049
  3. Doctor Sleep
  4. It
  5. Men in Black: International
  6. Mission: Impossible - Ghost Protocol
  7. Resident Evil: The Final Chapter
  8. Spider-Man: Into the Spider-Verse
  9. Terminator: Dark Fate
  10. The Cloverfield Paradox

https://www.avsforum.com/wordpress/10-best-sounding-dolby-atmos-movies-uhd-blu-ray/

HDR

Latest notes: 2021-10-26

FastFlix

FastFlix is a simple and friendly GUI for encoding videos.

Download latest release from Github

FastFlix keeps HDR10 metadata for x265, NVEncC HEVC, and VCEEncC HEVC, which will also be expanded to AV1 libraries when available.

It needs FFmpeg (version 4.3 or greater) under the hood for the heavy lifting, and can work with a variety of encoders.

Other

Notes: 2020-03-26

Reading HDR Values

In order to pass the correct settings to ffmpeg to create your smaller MKV file, you need to analyse the source MKV file for the HDR values...

ffprobe -read_intervals "%+#2" -show_frames -show_entries side_data "input.mkv"

...this will then give you a result...

[SIDE_DATA]
side_data_type=Mastering display metadata
red_x=34000/50000
red_y=16000/50000
green_x=13250/50000
green_y=34500/50000
blue_x=7500/50000
blue_y=3000/50000
white_point_x=15635/50000
white_point_y=16450/50000
min_luminance=1/10000
max_luminance=10000000/10000
[/SIDE_DATA]

...which is translated as...

master-display='G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,1)'

https://x265.readthedocs.io/en/default/cli.html#cmdoption-master-display

...which is used in the ffmpeg command (which also now includes key_frame scanning)...

ffmpeg -y -i "input.mkv" -t 00:05:00 -hide_banner -stats -loglevel quiet -analyzeduration 100M -probesize 100M -max_muxing_queue_size 9999 -map 0:v:0 -map 0:a:0 -map 0:a:0 -pix_fmt yuv420p10le -vcodec libx265 -preset slow -x265-params "keyint=60:bframes=3:vbv-bufsize=75000:vbv-maxrate=75000:colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc:master-display='G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,1)'" -crf 22 -force_key_frames "expr:gte(t,n_forced*2)" -c:a:0 copy -c:a:1 dca -ab 1509k -map_metadata -1 -metadata title="${TITLE}" -metadata:s:a:0 language=eng -metadata:s:a:1 language=eng -map_chapters -1 -sn -strict -2 "output.mkv"

MakeMKV Forum Post "LINUX - ffmpeg - 4K HDR HEVC X265 - Plex"


SUCCESS!

Reducing the size of a 80Gb UHD Blu-ray video down to a more manageable 15Gb, whilst keeping a stunning 4K picture with 10-bit colour and HDR.

To prepare for future audio requirements, I have kept the original TrueHD 7.1 Dolby Atmos 16 Channels audio track and added a second Dolby DTS 5.1 audio track for my current set up.

20 minutes of movie took 12 hours to transcode, which means a 3 hour film will take 4 days to transcode!

Multiple audio...

ffmpeg -y -i "massive_4k_input_file.mkv" -t 00:20:00 -analyzeduration 100M -probesize 100M -max_muxing_queue_size 9999 -map 0:v:0 -map 0:a:0 -map 0:a:0 -pix_fmt yuv420p10le -vcodec libx265 -preset slow -x265-params "crf=26:keyint=60:bframes=3:vbv-bufsize=75000:vbv-maxrate=75000:colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc:master-display='G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,500)'" -c:a:0 copy -c:a:1 dca -ab 1509k -map_metadata -1 -metadata title="Movie Name (2017)" -metadata:s:a:0 language=eng -metadata:s:a:1 language=eng -map_chapters -1 -sn -strict -2 "Movie Name (2017) 4K ffmpeg 2 Audio.mkv"

Single audio...

ffmpeg -y -i "massive_4k_input_file.mkv" -t 00:00:30 -analyzeduration 100M -probesize 100M -max_muxing_queue_size 9999 -map 0:v:0 -map 0:a:1 -pix_fmt yuv420p10le -vcodec libx265 -preset medium -x265-params "keyint=60:bframes=3:vbv-bufsize=75000:vbv-maxrate=75000:colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc:master-display='G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,500)'" -acodec ac3 -ab 384k -ac 6 -map_metadata -1 -metadata title="Movie Name (2018)" -metadata:s:a:0 language=eng -map_chapters -1 -sn "Movie Name (2018) 4K ffmpeg.mkv"

Black Magic Forum - Formatting for HDR10

Official test video file at 60 FPS...

$ mediainfo 'Downloads/HDR/Samsung 4K HDR Demo - Chasing The Light.ts'

General
ID                                       : 0 (0x0)
Complete name                            : Downloads/HDR/Samsung 4K HDR Demo - Chasing The Light.ts
Format                                   : MPEG-TS
File size                                : 762 MiB
Duration                                 : 1 min 35 s
Overall bit rate mode                    : Constant
Overall bit rate                         : 66.9 Mb/s

Video
ID                                       : 257 (0x101)
Menu ID                                  : 1 (0x1)
Format                                   : HEVC
Format/Info                              : High Efficiency Video Coding
Commercial name                          : HDR10
Format profile                           : Main 10@L5.1@High
Codec ID                                 : 36
Duration                                 : 1 min 34 s
Width                                    : 3 840 pixels
Height                                   : 2 160 pixels
Display aspect ratio                     : 16:9
Frame rate                               : 59.940 (60000/1001) FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 10 bits
Writing library                          : ATEME Titan File 3.7.3 (4.7.3.1002)
Color range                              : Limited
Color primaries                          : BT.2020
Transfer characteristics                 : PQ
Matrix coefficients                      : BT.2020 non-constant
Mastering display color primaries        : Display P3
Mastering display luminance              : min: 0.0500 cd/m2, max: 1000 cd/m2

Audio
ID                                       : 258 (0x102)
Menu ID                                  : 1 (0x1)
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Format version                           : Version 4
Muxing mode                              : ADTS
Codec ID                                 : 15-2
Duration                                 : 1 min 34 s
Bit rate mode                            : Variable
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy

Official test video at 30 FPS...

$ mediainfo 'Movies/Samsung Wonderland Two HDR UHD 4K Demo.ts'

General
ID                                       : 0 (0x0)
Complete name                            : Movies/Samsung Wonderland Two HDR UHD 4K Demo.ts
Format                                   : MPEG-TS
File size                                : 802 MiB
Duration                                 : 2 min 10 s
Overall bit rate mode                    : Constant
Overall bit rate                         : 51.4 Mb/s

Video
ID                                       : 257 (0x101)
Menu ID                                  : 1 (0x1)
Format                                   : HEVC
Format/Info                              : High Efficiency Video Coding
Commercial name                          : HDR10
Format profile                           : Main 10@L5.1@High
Codec ID                                 : 36
Duration                                 : 2 min 9 s
Width                                    : 3 840 pixels
Height                                   : 2 160 pixels
Display aspect ratio                     : 16:9
Frame rate                               : 23.976 (24000/1001) FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 10 bits
Writing library                          : ATEME Titan File 3.7.9 (4.7.9.0)
Color range                              : Limited
Color primaries                          : BT.2020
Transfer characteristics                 : PQ
Matrix coefficients                      : BT.2020 non-constant
Mastering display color primaries        : Display P3
Mastering display luminance              : min: 0.0500 cd/m2, max: 1000 cd/m2

Audio
ID                                       : 258 (0x102)
Menu ID                                  : 1 (0x1)
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Format version                           : Version 4
Muxing mode                              : ADTS
Codec ID                                 : 15-2
Duration                                 : 2 min 9 s
Bit rate mode                            : Variable
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy

4K UHD movie extracted using MakeMKV...

Video
ID                                       : 1
ID in the original source medium         : 4113 (0x1011)
Format                                   : HEVC
Format/Info                              : High Efficiency Video Coding
Commercial name                          : HDR10
Format profile                           : Main 10@L5.1@High
Codec ID                                 : V_MPEGH/ISO/HEVC
Duration                                 : 2 h 43 min
Bit rate                                 : 49.5 Mb/s
Width                                    : 3 840 pixels
Height                                   : 2 160 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 23.976 (24000/1001) FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0 (Type 2)
Bit depth                                : 10 bits
Bits/(Pixel*Frame)                       : 0.249
Stream size                              : 56.5 GiB (78%)
Language                                 : English
Default                                  : No
Forced                                   : No
Color range                              : Limited
Color primaries                          : BT.2020
Transfer characteristics                 : PQ
Matrix coefficients                      : BT.2020 non-constant
Mastering display color primaries        : Display P3
Mastering display luminance              : min: 0.0050 cd/m2, max: 4000 cd/m2
Maximum Content Light Level              : 457 cd/m2
Maximum Frame-Average Light Level        : 179 cd/m2
Original source medium                   : Blu-ray

https://stevens.li/guides/video/converting-hdr-to-sdr-with-ffmpeg/

-color_trc smpte2084

https://forum.videohelp.com/threads/385404-HDR-encoding

ffmpeg -y -i "in.mkv" -analyzeduration 100M -probesize 100M -max_muxing_queue_size 9999 -map 0:v -map 0:a -map 0:s? -c:s copy -c:a copy -pix_fmt yuv420p10le -vf scale=out_color_matrix=bt2020:out_h_chr_pos=0:out_v_chr_pos=0,format=yuv420p10 -c:v libx265 -preset medium -x265-params "high-tier=1:level-idc=5.1:open-gop=1:crf=22:crf-min=5:crf-max=40:aq-mode=1:colorprim=bt2020:colormatrix=bt2020nc:transfer=smpte2084:colormatrix=bt2020nc:hdr=1:hdr=1:info=1:repeat-headers=1:max-cll=0,0:master-display=G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(12000000,200)" "out.mkv"

Roku HDR

BBC and HDR with FFMPEG

X265 Log Level Quiet No Output

-x265-params log-level=error

Force Encoding In 8-Bit

-pix_fmt yuv420p

Combine Video Filters

To crop and deinterlace...

-vf "crop=704:416:8:80,yadif"

Convert 3D To 2D

ffmpeg -y -i "movie.mp4" -vf "stereo3d=sbsl:ml" -metadata:s:v:0 stereo_mode="mono" -aspect 16:9 "output.mp4"

Thanks

Ffmpeg Documentation - Steroscopic

Ffmpeg Filters - Stereo3d

Detect Top Bottom Widescreen Black Bars

1. Detect with cropdetect

The filter cropdetect can be used to provide the parameters for the crop filter. In this example the first 90 seconds is skipped and 10 frames are processed...

ffmpeg -ss 90 -i input.mp4 -vframes 10 -vf cropdetect -f null -
[Parsed_cropdetect_0 @ 0x220cdc0] x1:0 x2:1279 y1:0 y2:719 w:1280 h:720 x:0 y:0 pts:215 t:0.215000 crop=1280:720:0:0
[Parsed_cropdetect_0 @ 0x220cdc0] x1:0 x2:1279 y1:0 y2:719 w:1280 h:720 x:0 y:0 pts:257 t:0.257000 crop=1280:720:0:0
[Parsed_cropdetect_0 @ 0x220cdc0] x1:0 x2:1279 y1:0 y2:719 w:1280 h:720 x:0 y:0 pts:299 t:0.299000 crop=1280:720:0:0

...so according to cropdetect we can use crop=1280:720:0:0

2. Preview with ffplay

ffplay -vf crop=1280:720:0:0 input.mp4

3. Reencode using the crop filter

ffmpeg -i input.mp4 -vf crop=1280:720:0:0 -c:a copy output.mp4

Explanation

  • "crop=W:H:X:Y" means we're using the "crop" video filter, with 4 values:
  • w is the width of the output video (so the width of the cropped region)
  • h is the height of the output video (the height of the cropped region)
  • x is the horizontal position from where to begin cropping, starting from the left (with the absolute left margin being 0)
  • y is the vertical position from where to begin cropping, starting from the top of the video (the absolute top being 0)

Script Integration

CROP=$( ffmpeg -ss 90 -i "$1" -vframes 10 -vf cropdetect -f null - &>/tmp/ffmpeg_cropdetect.txt && grep 'Parsed_cropdetect_0' /tmp/ffmpeg_cropdetect.txt | tail -n1 | awk '{ print $14 }' )

"${FFMPEG}" -y -i "$1" -hide_banner -stats -loglevel quiet ${TEST} -map 0:v:0 -map 0:a:${AUDIO} -vf "${CROP}"

4K UHD Crop Detect

Display aspect ratio = 2.40:1 --> crop=3840:1600:0:280

1080 Full HD Crop Detect

Display aspect ratio = 2.40:1 --> crop=1920:800:0:140
Display aspect ratio = 2.39:1 --> crop=1920:816:0:132
Display aspect ratio = 2.35:1 --> crop=1920:816:0:130

576 SD Crop Detect

Display aspect ratio = 2.40:1 --> crop=720:416:0:80

Detect Interlaced Video

ffmpeg -i "input.mkv" -filter:v idet -frames:v 200 -an -f rawvideo -y /dev/null

http://www.aktau.be/2013/09/22/detecting-interlaced-video-with-ffmpeg/

Remove Interlacing Combing

ffmpeg -i input.vob -vf yadif -c:v libx264 -preset slow -crf 19 -c:a aac -b:a 256k output.mp4

Desaturate Colour To Monochrome And Increase Contrast

-vf "eq=saturation=0:contrast=1"

Denoise

  • atadenoise: very fast, temporal only with no motion compensation
  • hqdn3d: fast, both spatial and temporal, does basically lowpass by destroying high frequencies, blurs with extreme settings
  • nlmeans: very slow, currently implemented as spatial only, algorithm considered as one of the state of art denoisers
  • bm3d: very very slow, currently implemented as spatial only, algorithm considered as one of the state of art denoisers
  • vaguedenoiser: slow, spatial only, pretty good, wavelet
  • dctdnoiz: very very slow: spatial only, blurs too much
  • fftdnoiz: slow, spatial and limited temporal, using Fast Fourier Transform
  • owdenoise: very very very slow, spatial only, wavelet
  • removegrain: fast, spatial only, limited usecase

Cut Video Based On Start And End Time

ss = start time
t = length

This is 5 minutes...

ffmpeg -ss 00:00:00 -t 00:05:00 -i movie.mp4 -acodec copy -vcodec copy -async 1 cut.mp4

Find Total Time Of Video Files In Folder

NEW

#!/bin/sh
# convert_seconds.sh
# Convert seconds to h:m:s format

[ -z ${1} ] && echo "Usage: $(basename $0) <seconds>" && exit||secs=${1}

printf ""%dh:%dm:%ds"\n" $(($secs/3600)) $(($secs%3600/60)) $(($secs%60))

exit
~/Bin/convert_seconds.sh $( find . -maxdepth 1 -iname '*.mkv' -exec ffprobe -v quiet -of csv=p=0 -show_entries format=duration {} \; | paste -sd+ -| bc | awk '{print int($1)}' )

OLD

find . -maxdepth 1 -iname '*.mp4' -exec ffprobe -v quiet -of csv=p=0 -show_entries format=duration {} \; | paste -sd+ -| bc

Scale Video Keeping Correct Aspect Ratio

Use the scale filter to have ffmpeg work out the corresponding height or width. So, for videos which have already been cropped top and bottom, you would specify the correct width and let it work out the new height to keep the cropping and correct aspect ratio.

256x144, 426x240, 640x360, 854x480, 960x540, 1024x576, 1280x720, 1920x1080, 2560x1440, 3840x2160

e.g. a video that would have been specified as 'hd480' (854x480) is...

-vf "scale=854:-2"

...for 'hd720' it is...

-vf "scale=1280:-2"

...and for 'hd1080' it is...

-vf "scale=1920:-2"

Complete examples below for the 3 file types and resolutions...

LATEST METHODS JULY 2019

Now including CROP of top and bottom black bars. It has to crop _then_ scale so the order of the video filter is important :-)

#
# 480p mpeg4 aac avi
#
"${FFMPEG}" -y -i "${INPUT}" -hide_banner -stats -loglevel quiet ${TEST} -map 0:v:0 -map 0:a:$AUDIO -f avi -vf "${CROP},scale=854:-2" -vcodec mpeg4 -vtag xvid -qscale:v 8 -mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -g 300 -acodec aac -ab 128k -ac 2 -ar 44100 -threads 1 -map_metadata -1 -metadata title="${TITLE}" -metadata:s:a:0 language=eng -map_chapters -1 -sn "${OUTPUT}"

#
# 720p x264 ac3 mp4
#
"${FFMPEG}" -y -i "${INPUT}" -hide_banner -stats -loglevel quiet ${TEST} -map 0:v:0 -map 0:a:$AUDIO -f mp4 -vf "${CROP},scale=1280:-2" -vcodec libx264 -crf 23 -preset veryslow -tune "${X264TUNE}" -profile:v high -level 4.1 -acodec ac3 -ab 384k -ac 6 -map_metadata -1 -metadata title="${TITLE}" -metadata:s:a:0 language=eng -map_chapters -1 -sn "${OUTPUT}"

#
# 1080p x265 dts mkv
#
"${FFMPEG}" -y -i "${INPUT}" -hide_banner -stats -loglevel quiet ${TEST} -map 0:v:0 -map 0:a:$AUDIO -f matroska -vf "${CROP},scale=1920:-2" -vcodec libx265 -crf 26 -preset slow -x265-params log-level=quiet -acodec dca -ab 1509k -map_metadata -1 -metadata title="${TITLE}" -metadata:s:a:0 language=eng -map_chapters -1 -sn -strict -2 "${OUTPUT}"


LATEST METHODS JUNE 2019

#
# 480p mpeg4 aac avi
#
"${FFMPEG}" -y -i "${INPUT}" -hide_banner -stats -loglevel quiet ${TEST} -map 0:v:0 -map 0:a:$AUDIO -f avi -vf scale=854:-2 -vcodec mpeg4 -vtag xvid -qscale:v 8 -mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -g 300 -acodec aac -ab 128k -ac 2 -ar 44100 -threads 1 -map_metadata -1 -metadata title="${TITLE}" -metadata:s:a:0 language=eng -map_chapters -1 -sn "${OUTPUT}"

#
# 720p x264 ac3 mp4
#
"${FFMPEG}" -y -i "${INPUT}" -hide_banner -stats -loglevel quiet ${TEST} -map 0:v:0 -map 0:a:$AUDIO -f mp4 -vf scale=1280:-2 -vcodec libx264 -crf 23 -preset veryslow -tune "${X264TUNE}" -profile:v high -level 4.1 -acodec ac3 -ab 384k -ac 6 -map_metadata -1 -metadata title="${TITLE}" -metadata:s:a:0 language=eng -map_chapters -1 -sn "${OUTPUT}"

#
# 1080p x265 dts mkv
#
"${FFMPEG}" -y -i "${INPUT}" -hide_banner -stats -loglevel quiet ${TEST} -map 0:v:0 -map 0:a:$AUDIO -f matroska -vf scale=1920:-2 -vcodec libx265 -crf 26 -preset slow -x265-params log-level=quiet -acodec dca -ab 1509k -map_metadata -1 -metadata title="${TITLE}" -metadata:s:a:0 language=eng -map_chapters -1 -sn -strict -2 "${OUTPUT}"

Screen Resolutions

'hd480' = 852x480
'hd720' = 1280x720
'hd1080' = 1920x1080
'uhd2160' = 3840x2160

https://ffmpeg.org/ffmpeg-utils.html#Video-size

ffmpeg -s hd720 ...

16:9 aspect ratio resolutions: 640×360, 720x404, 852×480, 1024×576, 1152×648, 1280×720, 1366×768, 1600×900, 1920×1080, 2560×1440 and 3840×2160.

Video Resolutions

Aspect Ratio Cheat Sheet

Old, now using scale filter.

Aspect Ratios

16:9 = 1.78:1

Video Track Copy And Encode Multiple Audio Tracks

From this...

Stream #0.0(und): Video: h264, yuv420p, 720x424, 911 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc
Stream #0.1(eng): Audio: ac3, 48000 Hz, 5.1, fltp, 448 kb/s

To this...

Stream #0.0(und): Video: h264, yuv420p, 720x424, 911 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc
Stream #0.1(eng): Audio: aac, 48000 Hz, stereo, fltp, 160 kb/s
Stream #0.2(eng): Audio: ac3, 48000 Hz, 5.1, fltp, 448 kb/s

Using this...

ffmpeg -i INPUT -map 0:0 -map 0:1 -map 0:1 -codec:0 copy -codec:1 libfdk_aac -b:1 160k -ar:1 48k -ac:1 2 -codec:2 copy OUTPUT

Many thanks - https://forum.handbrake.fr/viewtopic.php?f=10&t=34844&p=163056#p163060

Converting File Formats

ffmpeg -i film.avi -target pal-dvd dvd.mpg

DVD Authoring

Make dvd files without menu in a dvd/ folder...

dvdauthor -o dvd/ -t dvd.mpg

Create the table of contents...

dvdauthor -o dvd/ -T

DVD Burning

Now burn the DVD...

growisofs -Z /dev/dvd -dvd-video dvd/

Blu-ray Copying Transcoding

Usual Routine

Check which track to rip (look for correct duration and chapters)...

makemkvcon --robot --minlength=1000 info disc:0

Make the file...

makemkvcon --minlength=1000 mkv disc:0 0 /tmp/

Transcode the file...

HandBrake bla bla bla

Tricky Disc

If you have a disc that will not complete the 'usual routine' above, then you have to take a backup of the entire disc, get information on the disc, join the correct files together in the correct order, extract specific tracks and then create the mkv file ready for HandBrake or ffmpeg transcoding...

  1. makemkv disc backup
  2. makemkv info save to text file
  3. cat join tracks
  4. ffmpeg copy specific tracks
  5. ffmpeg x265

Backup Disc

Create a directory for the disc contents...

mkdir -p ~/Videos/Movie

Backup the entire disc to that directory...

makemkvcon backup disc:0 ~/Videos/Movie

You will end up with the following folders...

|-- BDMV
|   |-- AUXDATA
|   |-- BACKUP
|   |   |-- BDJO
|   |   |-- CLIPINF
|   |   |-- JAR
|   |   `-- PLAYLIST
|   |-- BDJO
|   |-- CLIPINF
|   |-- JAR
|   |   |-- 00010
|   |   |-- 00011
|   |   |-- 00012
|   |   |-- 04518
|   |   |-- 09709
|   |   |-- 15631
|   |   `-- 18912
|   |-- META
|   |   `-- DL
|   |-- PLAYLIST
|   `-- STREAM
|-- CERTIFICATE
|   `-- BACKUP
`-- MAKEMKV
    |-- AACS
    |   `-- DUPLICATE
    `-- CMAP

Get Information

Find out which tracks are used and what files are used for those tracks...

 makemkvcon --robot --minlength=1000 info disc:0 > ~/Videos/Movie/makemkv_info.txt

Join Correct Tracks

Find the correct order for the files for a track...

grep '26,0' ~/Videos/Movie/makemkv_info.txt
TINFO:1,26,0,"1,4,9,10,11,13,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37"

Change to the directory which stores the many video files...

cd ~/Videos/Movie/BDMV/STREAM

Now join the files together (tab is your friend here) using the list order above, into one big M2TS file...

cat 00001.m2ts 00004.m2ts 00009.m2ts ... 00036.m2ts 00037.m2ts > allparts.m2ts

Create MKV File

There will be about 10 tracks in that big file, and we only want 2 - one video track and one audio (preferably DTS) track.

Use ffmpeg to list the tracks...

ffmpeg -i allparts.m2ts

Input #0, mpegts, from 'allparts.m2ts':
 Duration: 02:27:59.44, start: 4198.000000, bitrate: 31651 kb/s
 Program 1 
   Stream #0:0[0x1011]: Video: vc1 (Advanced) (VC-1 / 0x312D4356), yuv420p(bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 90k tbn, 47.95 tbc
   Stream #0:1[0x1100]: Audio: dts (DTS-HD MA) ([134][0][0][0] / 0x0086), 48000 Hz, 5.1(side), s32p (24 bit)
   Stream #0:2[0x1101]: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, 5.1(side), fltp, 640 kb/s
   Stream #0:3[0x1102]: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, 5.1(side), fltp, 640 kb/s
   Stream #0:4[0x1103]: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, 5.1(side), fltp, 640 kb/s
   Stream #0:5[0x1200]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090)
   Stream #0:6[0x1201]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090)
   Stream #0:7[0x1202]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090), 1920x1080
   Stream #0:8[0x1203]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090)
   Stream #0:9[0x1204]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090)
   Stream #0:10[0x1205]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090)
   Stream #0:11[0x1206]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090)
   Stream #0:12[0x1207]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090)
   Stream #0:13[0x1208]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090)
   Stream #0:14[0x1209]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090)

Use ffmpeg's copy track feature to create an MKV file with only 2 tracks.

This command uses the first video track with the first audio track and removes subtitle tracks...

ffmpeg -y -i allparts.m2ts -map 0:v:0 -map 0:a:0 -acodec copy -vcodec copy -sn allparts.mkv

Transcode

1080p X265 DTS MKV...

ffmpeg -y -i "allparts.mkv" -hide_banner -nostats -loglevel quiet -map 0:v:0 -map 0:a:0 -vcodec libx265 -crf 26 -preset slow -acodec dca -b:a 1509k -map_metadata -1 -metadata title="Movie" -metadata:s:a:0 language=eng -map_chapters -1 -sn -strict -2 "Movie.mkv"

720p X264 AC3 MP4...

ffmpeg -y -i "allparts.mkv" -hide_banner -nostats -loglevel quiet -map 0:v:0 -map 0:a:0 -s hd720 -aspect 16:9 -vcodec libx264 -crf 23 -preset veryslow -tune film -profile:v high -level 4.1 -acodec ac3 -ab 384k -ac 6 -map_metadata -1 -metadata title="Movie" -metadata:s:a:0 language=eng -map_chapters -1 -sn "Movie.mp4"

Copy to Plex

cp -av "Movie.mp4" "Movie.mkv" /path/to/Plex/Movies/

Enjoy

Turn the lights down, turn the surround sound up and enjoy your movie in Plex - make sure you choose "Play Version" :-)

Converting From One Format To Another

HVC X265 - https://trac.ffmpeg.org/wiki/Encode/H.265

Test file, 5 minutes long, CFR of 25, audio of DTS surround at 1509 Kbps...

ffmpeg -ss 00:00:00 -t 00:05:00 -i input.mkv -c:v libx265 -crf 25 -c:a dca -b:a 1509k -strict -2 output.mkv

Test file, 5 minutes long, CFR of 28, audio of AC3 surround at 384 Kbps...

ffmpeg -ss 00:00:00 -t 00:05:00 -i input.mkv -c:v libx265 -crf 28 -c:a ac3 -b:a 384k output.mp4

Test file, 5 minutes long, CFR of 22, audio of AAC, 6 channels at 160 Kbps...

ffmpeg -ss 00:00:00 -t 00:05:00 -i input.mkv -c:v libx265 -crf 22 -c:a libfdk_aac -b:a 160k -ac 6 output.mp4

WEBM - https://trac.ffmpeg.org/wiki/Encode/VP8

ffmpeg -i input-file.mp4 -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis output-file.webm

Using HandBrake...

MKV - https://trac.ffmpeg.org/wiki/Encode/H.264

HandBrakeCLI --input input_file.vob --title 1 --markers --encoder x264 --x264-preset veryslow --x264-tune animation --audio 1 --aencoder copy --subtitle 1 --output output_file.mkv

Using ffmpeg...

MKV

ffmpeg -y -i input_file.mkv -s hd720 -f matroska -vcodec libx264 -profile:v main -level 41 -preset veryslow -tune film -crf 22 -threads 0 -b 1152k -metadata title="Movie Title" -metadata:c title="" -metadata:s title="" -acodec ac3 -ab 384k -ac 6 -ar 48000 -sn output_file.mkv

AVI

"/usr/local/bin/ffmpeg" -y -i "input.mkv" -map 0:v:0 -map 0:a:0 -s 852x356 -f avi -vcodec mpeg4 -vtag xvid -b 1152k -mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -g 300 -acodec aac -ab 128k -ac 2 -ar 44100 -threads 1 -map_metadata -1 -metadata title="Movie Title (YEAR)" -metadata:s:a:0 language=eng -map_chapters -1 -sn "output.avi"

Command line to convert a MKV file to a smaller xVid AVI file, by reducing the video bit rate and audio quality...

ffmpeg -y -i input_file.mkv -s 640x360 -aspect 16:9 -f avi -vcodec mpeg4 -vtag xvid -b 1152k -mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -g 300 -acodec mp2 -ab 128k -ac 2 -ar 44100 -threads 1 output_file.avi

Command line to convert a large HD (1280x720) video at 1Gb into a smaller 350Mb video...

ffmpeg -y -i input_file.m4v -s 640x360 -aspect 16:9 -f avi -vcodec mpeg4 -qscale 3 -acodec libmp3lame -ab 128k -ac 2 -ar 44100 output_file.avi

Command line to convert a Full HD mkv video at 17Gb into a smaller HD avi video at 2Gb but keeping excellent quality...

ffmpeg -y -i input_file.mkv -s hd720 -f avi -vcodec libx264 -vpre hq -crf 22 -threads 0 -b 1152k -acodec libmp3lame -ab 128k -ac 2 -ar 44100 output_file.avi

Before and after (the 4 hours of re-encoding [on a Quad-Core with 8Gb of RAM])...

-rw-r--r-- 1 paully paully  17G 2010-08-31 22:43 input_file.mkv
-rw-r--r-- 1 paully paully 2.1G 2010-09-01 05:12 output_file.avi

Using Mplayer On A DVD To Produce A VOB File

mplayer dvd://1 -dumpstream -dumpfile mydvd.vob

Using Mplayer On An ISO To Produce A VOB File

Extract whole film...

mplayer dvd://1 -dvd-device mydvd.iso -dumpstream -dumpfile mydvd.vob

Extract just one chapter...

mplayer dvd://1 -dvd-device mydvd.iso -chapter 1-1 -dumpstream -dumpfile mydvd-1.vob

Change Metadata In A MKV File

mkvpropedit movie.mkv --edit info --set "title=The movie" --edit track:a1 --set language=fre --edit track:a2 --set language=ita

https://mkvtoolnix.download/doc/mkvpropedit.html#mkvpropedit.examples

Extract The Subtitles From a VOB File

mencoder mydvd.vob -nosound -ovc copy -o /dev/null -vobsubout mydvd -sid 0 -vobsuboutindex 0 -vobsuboutid en

Extract The Subtitles From an MKV File

mkvinfo "movie.mkv"
mkvextract tracks "movie.mkv" 6:"movie.eng.srt"

Extract Just The Audio Track From A Video File

This command will jump to a specific point in the video then only get 30 seconds of mp3 audio...

(The -vn helps because it tells ffmpeg to ignore the video stream)

ffmpeg -i input_file.avi -ss 00:03:51 -t 00:00:30 -vn -acodec libmp3lame -ab 128k -ac 2 -ar 44100 output_file.mp3

Convert M4A To MP3

for f in *.m4a; do ffmpeg -i "$f" -acodec libmp3lame -ab 320k "${f%.m4a}.mp3"; done

Convert FLAC To MP3

for f in *.flac; do ffmpeg -i "$f" -acodec libmp3lame -ab 320k "${f%.flac}.mp3"; done

Installing the LATEST Release of ffmpeg in Ubuntu Linux

NEW NOTES

  1. nasm
  2. x264
  3. x265 (Release Notes) MULTILIB (April 2019)
  4. fdk-aac
  5. zimg
  6. lame
  7. ffmpeg
sudo apt-get remove ffmpeg x264 libx264-dev yasm nasm lame
cd ~/Downloads
wget https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.gz
tar -xzvf nasm-2.15.05.tar.gz
cd nasm-2.15.05/
./configure
make
sudo make install
sudo ldconfig
which nasm
nasm --version
cd ~/Downloads
git clone https://code.videolan.org/videolan/x264.git
cd x264/
git pull
./configure --enable-shared
make
sudo make install
sudo ldconfig
which x264
x264 --version
cd ~/Downloads
sudo apt-get install cmake cmake-curses-gui
git clone https://github.com/videolan/x265.git
cd x265/
git pull
cd build/linux/
./multilib.sh
cd 8bit/
sudo make install
sudo ldconfig
which x265
x265 --version
  x265 [info]: HEVC encoder version 3.2_RC1
  x265 [info]: build info [Linux][GCC 5.4.0][64 bit] 8bit+10bit+12bit
  x265 [info]: using cpu capabilities: MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX XOP FMA4 FMA3 BMI1
cd ~/Downloads
sudo apt-get install autoconf libtool
git clone https://github.com/mstorsjo/fdk-aac.git
cd fdk-aac/
git pull
./autogen.sh
./configure
make
sudo make install
sudo ldconfig
cd ~/Downloads
git clone https://github.com/sekrit-twc/zimg.git
cd zimg/
git pull
./autogen.sh
./configure
make
sudo make install
sudo ldconfig
cd ~/Downloads
wget https://netix.dl.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
tar -xzvf lame-3.100.tar.gz
cd lame-3.100/
./configure
make
sudo make install
sudo ldconfig
which lame
lame -v
cd ~/Downloads
sudo apt-get install pkg-config
git clone git://git.videolan.org/ffmpeg
cd ffmpeg/
git pull
./configure --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-libzimg --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libass --enable-libfontconfig --enable-libfreetype --enable-openssl
make clean
make
sudo make install
sudo ldconfig
which ffmpeg
ffmpeg

OLD NOTES

Uninstall and Install the necessary software...

sudo apt-get remove ffmpeg x264 libx264-dev
sudo apt-get update
sudo apt-get install build-essential subversion git-core checkinstall yasm texi2html libfaac-dev \
libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libvorbis-dev \
libx11-dev libxfixes-dev libxvidcore-dev zlib1g-dev

Create the necessary new application install directories...

sudo mkdir /usr/local/include
sudo mkdir /usr/local/lib/pkgconfig
sudo mkdir /usr/local/share/man

Install x264 first...

cd
sudo rm -rf x264/
git clone git://git.videolan.org/x264.git
cd x264
./configure --enable-static
make
sudo checkinstall --pkgname=x264 --pkgversion "svn`date +%Y%m%d`+`git rev-list HEAD -n 1 | head -c 7`" --backup=no --default
cd
which x264
x264 --version
echo x264 hold | sudo dpkg --set-selections

Then install ffmpeg...

cd
sudo rm -rf ffmpeg/
git clone git://git.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac \
--enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvorbis \
--enable-libx264 --enable-libxvid --enable-x11grab --enable-pthreads
make
sudo checkinstall --pkgname=ffmpeg --pkgversion "svn`date +%Y%m%d`+`git rev-list HEAD -n 1 | head -c 7`" --backup=no --default
cd
which ffmpeg
ffmpeg
echo ffmpeg hold | sudo dpkg --set-selections

HEVC H.265 X265

Release Notes

https://trac.ffmpeg.org/wiki/Encode/H.265

  • Choose a CRF. The default is 28, and it should visually correspond to libx264 video at CRF 23, but result in about half the file size. Other than that, CRF works just like in x264.
  • Choose a preset. The default is medium. The preset determines how fast the encoding process will be – at the expense of compression efficiency. Put differently, if you choose ultrafast, the encoding process is going to run fast, but the file size will be larger when compared to medium. The visual quality will be the same. Valid presets are ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow and placebo.
  • Choose a tune. By default, this is disabled, and it is generally not required to set a tune option. x265 supports the following -tune options: psnr, ssim, grain, zerolatency, fastdecode. They are rexplained in the H.264 guide.

Example #1 X265: First chapter (3 minutes) of the Wall-E Blu-ray, 1080p MKV to 1080p MP4 with CRF 25, FDK AAC Surround Sound, edited Metadata and NO chapter markers...

ffmpeg -ss 00:00:00 -t 00:03:36 -i "input.mkv" -c:v libx265 -crf 25 -c:a libfdk_aac -b:a 384k -ac 6 -map_metadata -1 -metadata title="Wall-E (2008)" -map_chapters -1 "output.mp4"

FILE SIZE = 40Mb

Example #2 X264: For size comparison...

ffmpeg -ss 00:00:00 -t 00:03:36 -i "input.mkv" -c:v libx264 -crf 22 -c:a libfdk_aac -b:a 384k -ac 6 -map_metadata -1 -metadata title="Wall-E (2008)" -map_chapters -1 "output.mp4"

FILE SIZE = 80Mb

Quiet Output

-hide_banner -nostats -loglevel quiet

Verbose Media Information

ffprobe -v quiet -print_format json -show_format -show_streams movie.mp4

Rename Chapter Titles

Export the current metadata...

ffmpeg -i input.mkv -f ffmetadata metadata.txt

It will look like this...

;FFMETADATA1
title=Rush - Clockwork Angels Deluxe
encoder=Lavf58.26.101
[CHAPTER]
TIMEBASE=1/1000000000
START=0
END=435201433333
title=Chapter 01
[CHAPTER]
TIMEBASE=1/1000000000
START=435201433333
END=779044933333
title=Chapter 02

Edit the file...

nano metadata.txt
;FFMETADATA1
title=Rush - Clockwork Angels Tour (2013)
encoder=Lavf58.26.101
[CHAPTER]
TIMEBASE=1/1000000000
START=0
END=435201433333
title=Subdivisions
[CHAPTER]
TIMEBASE=1/1000000000
START=435201433333
END=779044933333
title=The Big Money

Import the edited metadata file...

ffmpeg -i input.mkv -i metadata.txt -map_metadata 1 -codec copy output.mkv

http://ffmpeg.org/ffmpeg-formats.html#Metadata-1

Remove Chapter Markers

-map_chapters -1

Thanks - https://video.stackexchange.com/questions/20270/ffmpeg-delete-chapters#20324

Add Subtitles

Hardsub

The subtitles video filter can be used to hardsub, or burn-in, the subtitles. This requires re-encoding and the subtitles become part of the video itself.

Softsub

These are additional streams within the file. The player simply renders them upon playback. More flexible than hardsubbing because:

  • You do not need to re-encode the video.
  • You can have multiple subtitles (various languages) and switch between them.
  • You can toggle them on/off during playback.
  • They can be resized with any player worth using.
  • Of course sometimes hardsubs are needed if the device or player is unable to utilize softsubs.

To mux subtitles into a video file using stream copy mode:

ffmpeg -i input.mkv -i subtitles.srt -codec copy -map 0 -map 1 output.mkv

Nothing is re-encoded, so the whole process will be quick and the quality and formats will be preserved.

Using SubStation Alpha (ASS) subtitles will allow you to format the subtitles however you like.

Info

https://stackoverflow.com/questions/8672809/use-ffmpeg-to-add-text-subtitles#17584272

https://stackoverflow.com/questions/21363334/how-to-add-font-size-in-subtitles-in-ffmpeg-video-filter

https://en.wikibooks.org/wiki/FFMPEG_An_Intermediate_Guide/subtitle_options

Remove Subtitles

-sn

Copy Subtitle Track

-map 0:s:0 -scodec copy

Set Audio Metadata Language

AVI

Include both the 'IAS1' tag and the 'title' tag...

-metadata IAS1=eng -metadata:s:a:0 title="English"

Thanks - https://superuser.com/questions/783895/ffmpeg-edit-avi-metadata-and-audio-track-naming

MP4

-metadata:s:a:0 language=eng

MKV

-metadata:s:a:0 language=eng

Set Subtitle Metadata Language

-metadata:s:s:0 language=eng

https://en.wikibooks.org/wiki/FFMPEG_An_Intermediate_Guide/subtitle_options#Set_Subtitle_Language

Set Subtitle Forced Flag

-disposition:s:s:0 forced

https://stackoverflow.com/questions/26956762/ffmpeg-set-subtitles-track-as-default

Set Movie Title

-metadata title="Movie Name (YEAR)"

Allow DTS

-acodec dca -strict -2 "/path/to/output.mkv"

Cropping The Black Bars

You would not believe the lack of SIMPLE, clear documentation with _examples_ on the internet for this!

It took me over 2 hours of mucking around to get this right...

Basically, you resize your picture, crop from the top, and then state your finished picture size.

The magic formula was scaled height - cropped height divided by 2 e.g. 720 - 544 / 2 = 88

It makes perfect sense now, but why has no-one provided examples for dummies by now?!

ffmpeg -y -i input_file.mkv -s 1280X720 -vf crop=0:88:1280:544 -f avi -vcodec libx264 -vpre hq \
-crf 22 -threads 0 -b 1152k -acodec libmp3lame -ab 128k -ac 2 -ar 44100 output_file.avi
-s the scaled picture size to start cropping
-vf the video filter to use
crop=pixels_from_left:pixels_from_top:final_picture_width:final_picture_height

xVid Compatible AVI

ffmpeg -y -i Title_S01E09.vob -f avi -vcodec mpeg4 -vtag xvid -b 1152k -mbd rd -flags +mv4+aic \
-trellis 2 -cmp 2 -subcmp 2 -g 300 -acodec libmp3lame -ab 128k Title_S01E09.avi

ffmpeg: How To Strip Metadata

-map_metadata -1
ffmpeg -i input.mkv -map_metadata -1 -vcodec copy -acodec copy output.mkv

ffmpeg: How To Add Metadata

MKV

This will copy both video and audio tracks, plus keep existing metadata but add the audio track language as English (eng)...

ffmpeg -i input.mkv -c copy -map_metadata 0 -metadata:s:a:0 language=eng output.mkv

MP4

-map_metadata -1 -metadata title="Movie Name" -metadata:s:a:0 language=eng 

AVI

Set language code to 'English'...

ffmpeg -i input.mkv -c copy -map_metadata -1 -metadata IAS1=eng -metadata:s:a:0 title="English" output.avi

Android and Playstation 3 Compatible MP4

The Sony Playstation 3 is fussy about what video files it can play.

  • Container: MP4
  • Video: H264 AVC Profile Main Level 3.1
  • Audio: AAC
nice -19 ffmpeg -y -i ~/Videos/Film/Film.mkv -map_metadata -1 -s 960x540 -f mp4 -vcodec libx264 -profile:v main -level 31 -preset veryslow -tune film -crf 22 -threads 0 -b 1152k -metadata title="Film" -acodec libvo_aacenc -ab 160k -ac 2 -ar 44100 ~/Videos/Film/Film.mp4

Android Compatible AVI

Don't use this, use the MP4 above.

This will create an .avi movie file suitable for playing with MX Player on HD Android devices.

Example #1 - film, medium speed

ffmpeg -y -i ~/Videos/Film/Film.mkv -s hd720 -f avi -vcodec libx264 -preset medium -tune film -crf 22 -threads 0 -b 1152k -acodec libmp3lame -ab 128k -ac 2 -ar 44100 ~/Videos/Film/Film.avi

Example #2 - animation, slower, clearing stream metadata, forcing profile main and level 3.1

ffmpeg -y -i ~/Videos/Film/Film.mkv -s hd720 -f avi -vcodec libx264 -profile:v main -level 31 -preset slower -tune animation -crf 22 -threads 0 -b 1152k -metadata:s title="" -acodec libmp3lame -ab 128k -ac 2 -ar 44100 ~/Videos/Film/Film.avi

Example #3 - film, very slow, profile main, level 3.1, setting and clearing metadata, plus nice value

nice -19 ffmpeg -y -i ~/Videos/Film/Film.mkv -s hd720 -f avi -vcodec libx264 -profile:v main -level 31 -preset veryslow -tune film -crf 22 -threads 0 -b 1152k -metadata title="Film" -metadata:c title="" -metadata:s title="" -acodec libmp3lame -ab 128k -ac 2 -ar 44100 ~/Videos/Film/Film.avi

ALSO - you can use cpulimit to stop frying your processor when transcoding...

sudo cpulimit -b -e ffmpeg -l 80

Why do this? You do this because the slower it takes, the better the result. You also finish with a smaller sized file!

If you do this correctly, a 32Gb FullHD MKV movie can come right down to 1Gb.

** This will take about 12 hours so leave it running overnight :-) **

You can read more about the x264 settings here:-

http://mewiki.project357.com/wiki/X264_Settings

x264 Presets

These are the new main x264 presets. In a nutshell, the slower the encoding produces a better quality and smaller size file.

  • ultrafast
  • superfast
  • veryfast
  • faster
  • fast
  • medium (32 fps)
  • slow (16 fps)
  • slower (8 fps)
  • veryslow (4 fps)
  • placebo (2 fps)

** UPDATE - Thursday, May 5 2016 **

http://dev.beandog.org/x264_preset_reference.html

** UPDATE - Monday, April 25 2011 **

You call the x264 presets with ffpmpeg differently now.

(OLD METHOD)   -vcodec libx264 -vpre veryslow
(NEW METHOD)   -vcodec libx264 -preset veryslow -tune film

Here's a list of the preset settings in x264...

ultrafast: cabac=0 / ref=1  / deblock=0:0:0 / analyse=0:0       / me=dia  / subme=0  / psy=1 / psy_rd=1.00:0.00 / mixed_ref=0 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=0  / threads=6 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=0                                                                           / weightp=0 / keyint=250 / keyint_min=23 / scenecut=0  / intra_refresh=0                   / rc=crf / mbtree=0 / crf=16.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40                 / aq=0
superfast: cabac=1 / ref=1  / deblock=1:0:0 / analyse=0x3:0x3   / me=dia  / subme=1  / psy=1 / psy_rd=1.00:0.00 / mixed_ref=0 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=0  / threads=6 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3  / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=1 / keyint=250 / keyint_min=23 / scenecut=40 / intra_refresh=0                   / rc=crf / mbtree=0 / crf=16.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / pb_ratio=1.30 / aq=1:1.00
veryfast:  cabac=1 / ref=1  / deblock=1:0:0 / analyse=0x3:0x113 / me=hex  / subme=2  / psy=1 / psy_rd=1.00:0.00 / mixed_ref=0 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=0  / threads=6 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3  / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=1 / keyint=250 / keyint_min=23 / scenecut=40 / intra_refresh=0 / rc_lookahead=10 / rc=crf / mbtree=1 / crf=16.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40                 / aq=1:1.00
faster:    cabac=1 / ref=2  / deblock=1:0:0 / analyse=0x3:0x113 / me=hex  / subme=4  / psy=1 / psy_rd=1.00:0.00 / mixed_ref=0 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=0  / threads=6 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3  / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=1 / keyint=250 / keyint_min=23 / scenecut=40 / intra_refresh=0 / rc_lookahead=20 / rc=crf / mbtree=1 / crf=16.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40                 / aq=1:1.00
fast:      cabac=1 / ref=2  / deblock=1:0:0 / analyse=0x3:0x113 / me=hex  / subme=6  / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=6 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3  / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=1 / keyint=250 / keyint_min=23 / scenecut=40 / intra_refresh=0 / rc_lookahead=30 / rc=crf / mbtree=1 / crf=16.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40                 / aq=1:1.00
medium:    cabac=1 / ref=3  / deblock=1:0:0 / analyse=0x3:0x113 / me=hex  / subme=7  / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=6 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3  / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=23 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=crf / mbtree=1 / crf=16.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40                 / aq=1:1.00
slow:      cabac=1 / ref=5  / deblock=1:0:0 / analyse=0x3:0x113 / me=umh  / subme=8  / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=6 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3  / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=23 / scenecut=40 / intra_refresh=0 / rc_lookahead=50 / rc=crf / mbtree=1 / crf=16.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40                 / aq=1:1.00
veryslow:  cabac=1 / ref=16 / deblock=1:0:0 / analyse=0x3:0x133 / me=umh  / subme=10 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=24 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=6 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=8  / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=23 / scenecut=40 / intra_refresh=0 / rc_lookahead=60 / rc=crf / mbtree=1 / crf=16.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40                 / aq=1:1.00
placebo:   cabac=1 / ref=16 / deblock=1:0:0 / analyse=0x3:0x133 / me=tesa / subme=11 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=24 / chroma_me=1 / trellis=2 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=0 / chroma_qp_offset=-2 / threads=6 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=16 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=23 / scenecut=40 / intra_refresh=0 / rc_lookahead=60 / rc=crf / mbtree=1 / crf=16.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40                 / aq=1:1.00

MythTV To AVI Script

Transcode a MythTV recording into an xvid .avi with the title and subtitle as filename.

Put this in /usr/local/bin/ and create a new MythTV User Job called 'Export to AVI' with the command input line:

myththto.sh %FILE%

Latest version: 2012-08-11: http://wiki.indie-it.com/images/5/5f/Mythtvto.sh

MKV To AVI Script

#!/bin/bash
#
#    mkvtoavi.sh
#
#    Description: convert a really big Full HD Matroska video file into a much smaller Standard HD Audio Video Interleave file.
#
#    Details: Conversion will take 4 x length of movie using the X264 codec at 'veryslow' preset.
#             For an average movie this will mean about 8 hours - so leave it running overnight!
#
#    Usage: ./mkvtoavi.sh input.file output.file
#
INPUTFILE="$1"
X264PRESET="veryslow"    # (superfast,veryfast,faster,fast,medium,slow,slower,veryslow,placebo)
OUTPUTFILE="$2"
if [ ! $# == 2 ]
  then echo "Usage: ./mkvtoavi.sh input.file output.file"
  else time ffmpeg -y -i "$INPUTFILE" -s hd720 -f avi -vcodec libx264 -preset "$X264PRESET" -crf 22 -threads 0 -b 1152k -acodec libmp3lame -ab 128k -ac 2 -ar 44100 "$OUTPUTFILE"
fi

FFHEVC and FFX264

Wow, a really impressive script for doing HDR to SDR conversion... the blackest of black arts.

If this works, I'll eat my hat...

UPDATE - yeah, it worked. Hat eaten :-)

https://ffhevc.sourceforge.io/

https://forum.doom9.org/showthread.php?t=172558

https://ffx264.sourceforge.io/

https://forum.doom9.org/showthread.php?t=172558

Mencoder

UPDATE: 2013-05-16

  1. http://www.axllent.org/code/divxcalc/
  2. http://quadpoint.org/projects/simplerip/

Check the correct tile track of a DVD...

lsdvd

Two pass encode with target size of 700Mb...

mencoder dvd://1 -alang en -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=128 -xvidencopts pass=1 -o /dev/null
mencoder dvd://1 -alang en -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=128 -xvidencopts pass=2:bitrate=-700000 -o output.avi

Two pass encode with black bar cropping of a 2.35:1 Widescreen UK PAL 720x576 picture and fixed bitrate for smaller final size...

mplayer dvd://1 -chapter 3 -vf cropdetect
mencoder dvd://1 -vf crop=720:416:0:80,scale=640:256 -ovc xvid -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=1 -oac copy -o /dev/null
mencoder dvd://1 -vf crop=720:416:0:80,scale=640:256 -ovc xvid -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=2 -alang en -oac mp3lame -lameopts br=96:cbr:vol=6 -o output.avi

Same, but for a 16:9 Widescreen UK PAL 720x576 picture...

mencoder dvd://1 -alang en -vf crop=720:410:0:80,scale=720:400 -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=96:vol=6 -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=1 -o /dev/null
mencoder dvd://1 -alang en -vf crop=720:410:0:80,scale=720:400 -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=96:vol=6 -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=2 -o output.avi 

Same, but for a 4:3 Standard Definition UK PAL picture...

mencoder dvd://1 -alang en -vf crop=688:560:18:8,scale=640:480 -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=96:vol=6 -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=1 -o /dev/null
mencoder dvd://1 -alang en -vf crop=688:560:18:8,scale=640:480 -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=96:vol=6 -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=2 -o output.avi 

http://ubuntuguide.org/wiki/Video_Conversion

http://www.axllent.org/docs/video/mencoder-dvd-to-mpeg4

Transcode

Example #1

Create an XviD .avi video of the 5th chapter of the 3rd title of a DVD ISO file, reducing the picture size to 640x360, whilst keeping the 16:9 aspect ratio...

transcode -i ~/Videos/Being_Human_S01D01.iso -T 3,5 -Z 640x360 -y xvid4,tcaud -o ~/Videos/Being_Human_S01E01-5.avi

-i = input filename. -T = title and chapter. -Z = resize video. -y = output format. -o = output filename.

Example #2

Create an XviD .avi video of a MythTV recording, using mplayer to probe for video and audio types, just use the first 2 streams for video and audio, reducing the picture size to 640x360, whilst keeping the 16:9 aspect ratio...

transcode -q 0 -i 7335_20110329090000.mpg --mplayer_probe -S 0,0-1 -Z 640x360 -y xvid4,tcaud -o /var/lib/mythtv/exports/test.avi

-q = debug level - quiet. --mplayer_probe = use mplayer. -S = stream and channel(s). -i = input filename. -T = title and chapter. -Z = resize video. -y = output format. -o = output filename.

Troubleshooting

Error...

[export_xvid4.so] warning: Usage of this module for audio encoding is deprecated.
[export_xvid4.so] warning: Consider switch to export_tcaud module.

Fix...

-y xvid4,tcaud

Full example...

trancode - i /dev/dvd -y xvid4,tcaud -o /path/to/filename.avi

ffmpeg Audio Channel Layouts

-channel_layout
7.1: Front: L C R, Surround: L R, Back: L R, LFE
6.1: Front: L C R, Surround: L R, Back: C, LFE
5.1: Front: L C R, Surround: L R, LFE 
layout_map[] = {
   {  1, CHANNEL_LAYOUT_MONO                                 , "mono"        },
   {  2, CHANNEL_LAYOUT_STEREO                               , "stereo"      },
   {  4, CHANNEL_LAYOUT_QUAD                                 , "quad"        },
   {  6, CHANNEL_LAYOUT_5POINT1                              , "5.1"         },
   {  8, CHANNEL_LAYOUT_5POINT1|CHANNEL_LAYOUT_STEREO_DOWNMIX, "5.1+downmix" },
   {  8, CHANNEL_LAYOUT_7POINT1                              , "7.1"         },
   {  8, CHANNEL_LAYOUT_7POINT1_WIDE                         , "7.1(wide)"   },
   { 10, CHANNEL_LAYOUT_7POINT1|CHANNEL_LAYOUT_STEREO_DOWNMIX, "7.1+downmix" },
   {  0 }

Set Language For Tracks In MKV

mkvpropedit movie.mkv --edit track:a1 --set language=fre --edit track:a2 --set language=ita

Set Movie Name In MKV

mkvpropedit movie.mkv --edit info --set "title=The movie"

Set Default Subtitles In MKV

Set for 1 track...

mkvpropedit movie.mkv --edit track:s1 --set flag-default=1

Remove from 1 track and set it for another track...

mkvpropedit movie.mkv --edit track:s1 --set flag-default=0 --edit track:s2 --set flag-default=1

Rename Chapters

Make your chapters text file...

CHAPTER01=00:00:00.000
CHAPTER01NAME=So What
CHAPTER02=00:05:34.360
CHAPTER02NAME=Creeping Death
CHAPTER03=00:12:24.120
CHAPTER03NAME=Sad But True

Then run this command to remove the existing chapters and add the new chapters into a second new movie file...

mkvmerge --no-chapters --chapters chapters.txt -o output.mkv input.mkv

Join MKV Files Together

sudo apt-get install mkvtoolnix
mkvmerge -o output.mkv input1.mkv +input2.mkv

Join MP4 Files Together

METHOD A

cat mylist.txt
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
   
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4

https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg

METHOD B

Install GPAC first, easily from ubuntu repository:

sudo apt-get install gpac

Then using one of GPAC's tool, named MP4Box, we can join or merge multiple mp4 files by concatenating into one file. With concatenating we save a huge amount of processing time which is usually spent for rendering/re-encoding.

MP4Box -cat file1.mp4 -cat file2.mp4 -cat file3.mp4 OutputFile.mp4

HOWTO: FIX: Error: Too many packets buffered for output stream 0:1 Conversion Failed

This will occur when you are trying to transcode 4K UHD Blu-ray rips with Dolby Atmos 7.1 audio.

The fix is to add a parameter to limit the amount of data read in one go...

-max_muxing_queue_size 1024

Add this to the end of the VIDEO options...

"/usr/local/bin/ffmpeg" -y -i "Movie Name (YEAR) UHD.mkv" -hide_banner -stats -loglevel quiet -t 00:05:00 -map 0:v:0 -map 0:a:0 -s uhd2160 -aspect 16:9 -vcodec libx265 -crf 26 -preset slow -max_muxing_queue_size 1024 -acodec copy -map_metadata -1 -metadata title="Movie Name (YEAR)" -metadata:s:a:0 language=eng -map_chapters -1 -sn "output_4K.mkv"

Thanks - https://stackoverflow.com/questions/49686244/ffmpeg-too-many-packets-buffered-for-output-stream-01#50262835

HOWTO: FIX: ERROR: ffmpeg: libmp3lame not found

sudo apt-get install libavcodec-extra-53

HOWTO: Install Latest ffmpeg In Ubuntu Linux

Ubuntu seems to be straying behind again, and the latest Ubuntu build is version 9.

However, the libav developers are up to version 11 now.

This is the latest daily build for Ubuntu. Things may break, but they might not. So, you'll be fine.

It will give you version 12. Which means that youtube-dl won't complain anymore :)

sudo add-apt-repository ppa:motumedia/libav-daily
sudo aptitude update
sudo aptitude -y safe-upgrade
which ffmpeg
ffmpeg -version

Now you need to move the old FFMPEG out of the way, and make it a symlink to the new ffmpeg.

sudo mv /usr/bin/ffmpeg /usr/bin/ffmpeg.old
sudo ln -s /usr/bin/ffmpeg /usr/bin/ffmpeg

Thanks - https://launchpad.net/~motumedia/+archive/ubuntu/libav-daily

HOWTO: Screenshots

NEW

ffmpeg -ss 00:23:00 -i Mononoke.Hime.mkv -frames:v 1 out1.jpg

OLD

ffmpeg -ss 1 -i <input_filename>.ts -f image2 -vf "select='eq(pict_type,I)'" -vframes 1 -vsync 2 <output_filename>.png