FFMPEG, MKVMerge and MKVToolNix: Difference between revisions

From WikiMLT
mNo edit summary
 
mNo edit summary
Line 1: Line 1:
<noinclude>{{ContentArticleHeader/Linux_Server|toc=off}}{{ContentArticleHeader/Linux_Desktop}}</noinclude>
<noinclude>{{ContentArticleHeader/Linux_Server|toc=off}}{{ContentArticleHeader/Linux_Desktop}}</noinclude>
== References ==
* ...
* ...
== Section 1 ==
...
__TOC__
__TOC__
== MKVMerge and MKVToolNix ==


==MKVMerge and MKVToolNix==
====== References ======
Референции:
 
*[http://ubuntuhandbook.org/index.php/2018/08/mkvtoolnix-26-0-0-released/ MKVToolNix 26.0.0 Released, How to Install it in Ubuntu]
*[http://ubuntuhandbook.org/index.php/2018/08/mkvtoolnix-26-0-0-released/ MKVToolNix 26.0.0 Released, How to Install it in Ubuntu]
*[https://askubuntu.com/q/939821/566421 Script to merge Video and subs then delete the existing files (non recursive)]
*[https://askubuntu.com/q/939821/566421 Script to merge Video and subs then delete the existing files (non recursive)]


Инсталиране на <code>MKVToolNix</code>:
====== Install <code>MKVToolNix</code> ======
 
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
cd ~/Downloads
cd ~/Downloads
Line 181: Line 171:
A possible way to automatically parse the audio codec and name the audio file accordingly would be:
A possible way to automatically parse the audio codec and name the audio file accordingly would be:


<code>for file in *mp4 *avi; do ffmpeg -i "$file" -vn -acodec copy "$file".</code>ffprobe "$file" 2>&1 |sed -rn 's/.''Audio: (...), .''/\1/p'<code>; done</code>
<code>for file in *mp4 *avi; do ffmpeg -i "$file" -vn -acodec copy "$file"; ffprobe "$file" 2>&1 | sed -rn 's/.''Audio: (...), .''/\1/p'; done</code>


Note that this command uses sed to parse output from ffprobe for each file, it assumes a 3-letter audio codec name (e.g. mp3, ogg, aac) and will break with anything different.
Note that this command uses sed to parse output from ffprobe for each file, it assumes a 3-letter audio codec name (e.g. mp3, ogg, aac) and will break with anything different.
Line 188: Line 178:
You can use a Bash "for loop" to encode all files in a directory:
You can use a Bash "for loop" to encode all files in a directory:


<code>$ mkdir newfiles</code>  
<code>mkdir newfiles</code>  


<code>$ for f in *.m4a; do ffmpeg -i "$f" -codec:v copy -codec:a libmp3lame -q:a 2 newfiles/"${f%.m4a}.mp3"; done</code>
<code>for f in *.m4a; do ffmpeg -i "$f" -codec:v copy -codec:a libmp3lame -q:a 2 newfiles/"${f%.m4a}.mp3"; done</code>
----<code>ffmpeg -i input.m4a -acodec libmp3lame -ab 128k output.mp3</code>  
----<code>ffmpeg -i input.m4a -acodec libmp3lame -ab 128k output.mp3</code>  


Line 200: Line 190:


===Extract Single Image from a Video at Specified Frame===
===Extract Single Image from a Video at Specified Frame===
<code>$ vf [ss][filename][outputFileName]</code>
<code>vf [ss][filename][outputFileName]</code>


where <code>vf</code> is a custom bash script as follows:
where <code>vf</code> is a custom bash script as follows:


<code>$ ffmpeg -ss $1 -i $2 -qmin 1 -q:v 1 -qscale:v 2 -frames:v 1 -huffman optimal $3.jpg</code>
<code>ffmpeg -ss $1 -i $2 -qmin 1 -q:v 1 -qscale:v 2 -frames:v 1 -huffman optimal $3.jpg</code>


ss offset = frame number divided by FPS of video = the decimal (in milliseconds) ffmpeg needs i.e. 130.5
ss offset = frame number divided by FPS of video = the decimal (in milliseconds) ffmpeg needs i.e. 130.5
Line 214: Line 204:


===Split a Video into Images===
===Split a Video into Images===
<code>$ ffmpeg -i video.flv image%d.jpg</code>
<code>ffmpeg -i video.flv image%d.jpg</code>


===Convert Images into a Video===
===Convert Images into a Video===
<code>$ ffmpeg -f image2 -i image%d.jpg imagestovideo.mpg</code>
<code>ffmpeg -f image2 -i image%d.jpg imagestovideo.mpg</code>


===Convert mp4 to webm===
===Convert mp4 to webm===
<code>$ ffmpeg -i example.mp4 -f webm -c:v libvpx -b:v 1M -acodec libvorbis example.webm -hide_banner</code>
<code>ffmpeg -i example.mp4 -f webm -c:v libvpx -b:v 1M -acodec libvorbis example.webm -hide_banner</code>


*[https://www.bugcodemaster.com/article/convert-videos-webm-format-using-ffmpeg more info]
*[https://www.bugcodemaster.com/article/convert-videos-webm-format-using-ffmpeg more info]

Revision as of 06:55, 25 September 2022

MKVMerge and MKV­Tool­Nix

Ref­er­ences
In­stall MKV­Tool­Nix
cd ~/Downloads
sudo sh -c 'echo "deb https://mkvtoolnix.download/ubuntu/ $(lsb_release -sc) main" >> /etc/apt/sources.list.d/bunkus.org.list'
wget -q -O - https://mkvtoolnix.download/gpg-pub-moritzbunkus.txt | sudo apt-key add -
sudo apt update
sudo apt install mkvtoolnix mkvtoolnix-gui

In­stall a new­er ver­sion of FFm­peg

sudo add-apt-repository ppa:jonathonf/ffmpeg-4
sudo apt update
sudo apt install ffmpeg
ffmpeg -version

Con­vert videos to MKV us­ing FFM­PEG

Con­vert any video sup­port­ed by FFM­PEG to a video file in MKV for­mat us­ing FFM­PEG com­mand-line tool.

MKV for­mat is the ex­ten­sion file used for the Ma­tros­ka Mul­ti­me­dia Con­tain­er. This is a file for­mat which can hold an un­lim­it­ed num­ber of video, au­dio, pic­ture, or sub­ti­tle tracks or streams in one file. It is sim­i­lar in its de­sign to oth­er con­tain­er types such as MP4 or ASF but it is based in an open spec­i­fi­ca­tion.

MKV sup­ports many video en­cod­ed streams such as H.264, or VP8 en­cod­ed video, or AAC, or VOR­BIS au­dio streams, which are some of the most com­mon sup­port­ed stream types. The user play­ing one MKV file must have a play­er that sup­ports the en­cod­ed streams in or­der to view the file prop­er­ly, so if the play­er sup­ports on­ly the au­dio stream, the user could on­ly hear the au­dio but he won't see the video.

We rec­om­mend us­ing H.264 or VP8 for video streams and AAC or VOR­BIS for au­dio as they are wide­ly sup­port­ed. How­ev­er, oth­er en­coders can be used. We will show you some of these in this ar­ti­cle.

The WEBM for­mat is a pro­file of MKV files, so when you are con­vert­ing a video to WEBM for­mat, or you are stor­ing in WEBM for­mat, you are ac­tu­al­ly stor­ing in MKV for­mat. The dif­fer­ence is that WEBM is a sub­set of MKV con­fig­u­ra­tion us­ing VP8 en­cod­ing for video and VOR­BIS for au­dio streams. (VP9 video and OPUS au­dio have been in­tro­duced last years). Read more about how to con­vert a video file to a webm file us­ing FFM­PEG in this ar­ti­cle.

So, for con­vert­ing a video to MKV for­mat you can use the ba­sic FFM­PEG com­mand:

ffmpeg -i input_video.avi output_video.mkv

To con­vert all files in cer­tain di­rec­to­ry (ref­er­ence):

for i in *; do ffmpeg -i "$i" "${i%.*}.mkv"; done

By de­fault this com­mand will pro­duce an out­put video file us­ing libx264 li­brary (which pro­duces H.264 video stream) and lib­vor­bis li­brary (which pro­duces VOR­BIS au­dio stream). You can use the com­mand spec­i­fy­ing this op­tions (which will pro­duce the same re­sult):

ffmpeg -i input_video.avi -f matroska -vcodec libx264 -acodec libvorbis output_video.mkv

In which we are us­ing the pa­ra­me­ters:

  • -f ma­tros­ka which spec­i­fies that the out­put file will have Ma­tros­ka for­mat
  • -vcodec libx264 we're telling here to use the libx264 li­brary which en­codes video in H.264 cod­ed streams. As we have said be­fore you can use dif­fer­ent video en­coders. You can use oth­er codecs chang­ing libx264 for 'vp8' if you want to use VP8 en­cod­ing through lib­vpx li­brary or 'vp9' if you want to use VP9 en­cod­ing. You can use more video en­coders but the most wide­ly sup­port­ed codecs are H.264 and VP8.
  • -acodec lib­vor­bis which spec­i­fies to use the lib­vor­bis li­brary which en­codes au­dio us­ing VOR­BIS codec. You can chose an­oth­er au­dio codec. Use 'aac' for AAC en­cod­ing or 'opus' for OPUS en­cod­ing.

Rec­om­men­da­tions and Oth­er con­sid­er­a­tions

  • Re­mem­ber that you can per­form an­oth­er trans­for­ma­tions as chang­ing res­o­lu­tion or the frame rate to the video, when con­vert­ing the video to a new for­mat. For ex­am­ple use the next com­mand to con­vert a video to a VP8/VORBIS video chang­ing video res­o­lu­tion to 640x360px. (See more on chang­ing video res­o­lu­tion us­ing FFM­PEG)
ffmpeg -i input_file.avi -f matroska -vcodec vp8 -vf scale=640:360 -acodec libvorbis output_file.mkv

Merge two AVI files us­ing FFM­PEG

First you need to cre­ate a file (ex­am­ple mylist.txt):

file '/path/here/file1.avi'
file '/path/here/file2.avi'
file '/path/here/file3.avi'

Then pass that file to ffm­peg:

ffmpeg -f concat -i mylist.txt -c copy video_draft.avi

You can use this com­mand to make the list:

ls *.avi | while read each; do echo "file '$each'" >> mylist.txt; done

The linked page has more ad­vanced ex­am­ples for deal­ing with is­sue like dif­fer­ent codecs/​​​formats.

Ref­er­ences:

Crop a video with FFM­PEG

Use the crop fil­ter:

ffmpeg -i in.mp4 -filter:v "crop=out_w:out_h:x:y" out.mp4

Where the op­tions are as fol­lows:

  • out_​​​w is the width of the out­put rec­tan­gle
  • out_​​​h is the height of the out­put rec­tan­gle
  • x and y spec­i­fy the top left cor­ner of the out­put rec­tan­gle

Ex­am­ple: Crop 16:9 file that con­tains 2.39:1 video with black mar­gins in­side:

ffmpeg -i 3.Days.to.Kill.BG.mkv -filter:v "crop=1920:804:0:138" 3.Days.to.Kill.BG_crop.mkv

GitHub protrolium/ffmpeg.md FFM­PEG

Con­vert­ing Au­dio in­to Dif­fer­ent For­mats / Sam­ple Rates

Min­i­mal ex­am­ple: transcode from MP3 to WMA:

ffm­peg ‑i input.mp3 output.wma

You can get the list of sup­port­ed for­mats with:

ffm­peg ‑for­mats

Con­vert WAV to MP3, mix down to mono (use 1 au­dio chan­nel), set bit rate to 64 kbps and sam­ple rate to 22050 Hz:

ffm­peg ‑i input.wav ‑ac 1 ‑ab 64000 ‑ar 22050 output.mp3

Con­vert any MP3 file to WAV 16khz mono 16bit:

ffm­peg ‑i 111.mp3 ‑acodec pcm_​​​s16le ‑ac 1 ‑ar 16000 out.wav

Con­vert any MP3 file to WAV 20khz mono 16bit for AD­DAC WAV Play­er:

ffm­peg ‑i 111.mp3 ‑acodec pcm_​​​s16le ‑ac 1 ‑ar 22050 out.wav

cd in­to dir for batch process:

for i in *.mp3; do ffm­peg ‑i "$i" ‑acodec pcm_​​​s16le ‑ac 1 ‑ar 22050 "${i%.mp3}-encoded.wav"; done

Pick­ing the 30 sec­onds frag­ment at an off­set of 1 minute:

In sec­onds

ffm­peg ‑i input.mp3 ‑ss 60 ‑t 30 output.wav

In HH:MM:SS for­mat

ffm­peg ‑i input.mp3 ‑ss 0:01:00 ‑t 0:00:30 output.wav

Ex­tract Au­dio

ffm­peg ‑i input-video.avi ‑vn ‑acodec copy output-audio.aac

vn is no video.

acodec copy says use the same au­dio stream that's al­ready in there.

ffm­peg ‑i video.mp4 ‑f mp3 ‑ab 192000 ‑vn music.mp3

The ‑i op­tion in the above com­mand is sim­ple: it is the path to the in­put file. The sec­ond op­tion ‑f mp3 tells ffm­peg that the ouput is in mp3 for­mat. The third op­tion i.e ‑ab 192000 tells ffm­peg that we want the out­put to be en­cod­ed at 192Kbps and ‑vn tells ffm­peg that we dont want video. The last param is the name of the out­put file.

Prac­ti­cal ex­am­ple:

ffmpeg -i Star.Wars.*.avi -f mp3 -ab 192000 -vn music.mp3

Re­place Au­dio on a Video with­out re-en­cod­ing.

strip au­dio stream away from video

ffm­peg ‑i INPUT.mp4 ‑codec copy ‑an OUTPUT.mp4

com­bine the two streams to­geth­er (new au­dio with orig­i­nal­ly ex­isit­ing video)

ffm­peg ‑i INPUT.mp4 ‑i AUDIO.wav ‑short­est ‑c:v copy ‑c:a aac ‑b:a 256k OUTPUT.mp4


You say you want to "ex­tract au­dio from them (mp3 or ogg)". But what if the au­dio in the mp4 file is not one of those? you'd have to transcode any­way. So why not leave the au­dio for­mat de­tec­tion up to ffm­peg?

To con­vert one file:

ffm­peg ‑i videofile.mp4 ‑vn ‑acodec lib­vor­bis audiofile.ogg

To con­vert many files:

for vid in *.mp4; do ffm­peg ‑i "$vid" ‑vn ‑acodec lib­vor­bis "${vid%.mp4}.ogg"; done

You can of course se­lect any ffm­peg pa­ra­me­ters for au­dio en­cod­ing that you like, to set things like bi­trate and so on.

Use -acodec libmp3lame and change the ex­ten­sion from .ogg to .mp3 for mp3 en­cod­ing.

If what you want is to re­al­ly ex­tract the au­dio, you can sim­ply "copy" the au­dio track to a file us­ing ‑acodec copy. Of course, the main dif­fer­ence is that transcod­ing is slow and cpu-in­ten­sive, while copy­ing is re­al­ly quick as you're just mov­ing bytes from one file to an­oth­er. Here's how to copy just the au­dio track (as­sum­ing it's in mp3 for­mat):

ffm­peg ‑i videofile.mp4 ‑vn ‑acodec copy audiofile.mp3

Note that in this case, the au­diofile for­mat has to be con­sis­tent with what the con­tain­er has (i.e. if the au­dio is AAC for­mat, you have to say audiofile.aac). You can use the ff­probe com­mand to see which for­mats you have, this may pro­vide some in­for­ma­tion:

for file in *; do ff­probe $file 2>&1 |grep Au­dio; done

A pos­si­ble way to au­to­mat­i­cal­ly parse the au­dio codec and name the au­dio file ac­cord­ing­ly would be:

for file in *mp4 *avi; do ffm­peg ‑i "$file" ‑vn ‑acodec copy "$file"; ff­probe "$file" 2>&1 | sed ‑rn 's/​​​.Au­dio: (…), ./\1/p'; done

Note that this com­mand us­es sed to parse out­put from ff­probe for each file, it as­sumes a 3‑letter au­dio codec name (e.g. mp3, ogg, aac) and will break with any­thing dif­fer­ent.


En­cod­ing mul­ti­ple files

You can use a Bash "for loop" to en­code all files in a di­rec­to­ry:

mkdir new­files

for f in *.m4a; do ffm­peg ‑i "$f" ‑codec:v copy ‑codec:a libmp3lame ‑q:a 2 newfiles/"${f%.m4a}.mp3"; done


ffm­peg ‑i input.m4a ‑acodec libmp3lame ‑ab 128k output.mp3

m4a to mp3 con­ver­sion with ffm­peg and lame

A batch file ver­sion of the same com­mand would be:

for f in *.m4a; do ffm­peg ‑i "$f" ‑acodec libmp3lame ‑ab 256k "${f%.m4a}.mp3"; done

Ex­tract Sin­gle Im­age from a Video at Spec­i­fied Frame

vf [ss][filename][outputFileName]

where vf is a cus­tom bash script as fol­lows:

ffm­peg ‑ss $1 ‑i $2 ‑qmin 1 ‑q:v 1 ‑qscale:v 2 ‑frames:v 1 ‑huff­man op­ti­mal $3.jpg

ss off­set = frame num­ber di­vid­ed by FPS of video = the dec­i­mal (in mil­lisec­onds) ffm­peg needs i.e. 130.5

Merge Mul­ti­ple Videos

file names in fold­er, if they con­tain spaces, must be prop­er­ly es­caped

ls * | perl ‑ne 'print "file $_"' | ffm­peg ‑f con­cat ‑i – ‑c copy merged.mp4

Split a Video in­to Im­ages

ffm­peg ‑i video.flv image%d.jpg

Con­vert Im­ages in­to a Video

ffm­peg ‑f image2 ‑i image%d.jpg imagestovideo.mpg

Con­vert mp4 to webm

ffm­peg ‑i example.mp4 ‑f webm ‑c:v lib­vpx ‑b:v 1M ‑acodec lib­vor­bis example.webm ‑hide_​​​banner

Sim­ple FLAC con­vert

ffm­peg ‑i audio​.xxx ‑c:a flac audio.flac

Mix Stereo to Mono

You can mod­i­fy a video file di­rect­ly with­out hav­ing to re-en­code the video stream. How­ev­er the au­dio stream will have to be re-en­cod­ed.

Left chan­nel to mono: ffm­peg ‑i video.mp4 ‑map_​​​channel 0.1.0 ‑c:v copy mono.mp4

Left chan­nel to stereo:

ffm­peg ‑i video.mp4 ‑map_​​​channel 0.1.0 ‑map_​​​channel 0.1.0 ‑c:v copy stereo.mp4

If you want to use the right chan­nel, write 0.1.1 in­stead of 0.1.0.

Trim End of file (mp3)

Here's a com­mand line that will slice to 30 sec­onds with­out transcod­ing:

ffm­peg ‑t 30 ‑i inputfile.mp3 ‑acodec copy outputfile.mp3


To En­code or Re-en­code ?

Do you need to cut video with re-en­cod­ing or with­out re-en­cod­ing mode? You can try to fol­low­ing be­low com­mand.

Syn­op­sis: ffm­peg ‑i [input_​​​file] ‑ss [start_​​​seconds] ‑t [duration_​​​seconds] [output_​​​file]

use ffm­peg cut mp4 video with­out re-en­cod­ing

Ex­am­ple:

ffm­peg ‑i source.mp4 ‑ss 00:00:05 ‑t 00:00:10 ‑c copy cut_video.mp4

use ffm­peg cut mp4 video with re-en­cod­ing

Ex­am­ple:

ffm­peg ‑i source.mp4 ‑ss 00:00:05 ‑t 00:00:10 ‑async 1 ‑strict ‑2 cut_video.mp4

If you want to cut off sec­tion from the be­gin­ning, sim­ply drop ‑t 00:00:10 from the com­mand

re­duce file­size

Ex­am­ple:

ffm­peg ‑i input.avi ‑vcodec libx264 ‑crf 24 output.avi

It re­duced a 100mb video to 9mb.. Very lit­tle change in video qual­i­ty.

Ex­am­ple:

ffm­peg ‑i video​.mov ‑vf eq=saturation=0 ‑s 640x480 ‑c:v libx264 ‑crf 24 output.mp4

make a grayscale ver­sion and scale to 640x480

Con­vert MKV to MP4

ffm­peg ‑i file.mkv

check for streams that you want (video/​​​audio). be sure to convert/​​​specify DTS 6 chan­nel au­dio stream

ffm­peg ‑i input.mkv ‑strict ex­per­i­men­tal ‑map 0:0 ‑map 0:1 ‑c:v copy ‑c:a:1 libmp3lame ‑b:a 192k ‑ac 6 output.mp4

Add Wa­ter­mark over­lay (png) to the cen­ter of a video

ffm­peg ‑i source​.mov ‑i watermark.png ‑filter_​​​complex "overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2" output.mp4

More com­mands

Cut­ting the videos based on start and end time us­ing FFM­PEG

Re-en­cod­ing is the de­fault if you do not spec­i­fy copy. For ex­am­ple:

ffmpeg -i movie.mp4 -ss 00:00:03 -t 00:00:08 -async 1 cut.mp4
ffmpeg -i The.Matrix.1999.BG.EN.sub.mkv -ss 00:10:00 -t 00:00:37 -async 1 cut.mkv

When re-en­cod­ing you may al­so wish to in­clude ad­di­tion­al qual­i­ty-re­lat­ed op­tions or a par­tic­u­lar AAC en­coder. For de­tails, see ffmpeg's x264 En­cod­ing Guide for video and AAC En­cod­ing Guide for au­dio.

Al­so, the -t op­tion spec­i­fies a du­ra­tion, not an end time. The above com­mand will en­code 8s of video start­ing at 3s. To start at 3s and end at 8s use -t 5. If you are us­ing a cur­rent ver­sion of ffm­peg you can al­so re­place -t with -to in the above com­mand to end at the spec­i­fied time.


Try us­ing this. It is the fastest and best ffm­peg-way I have fig­ure it out:

 ffmpeg -ss 00:01:00 -i input.mp4 -to 00:02:00 -c copy output.mp4

This com­mand trims your video in sec­onds!

ffmpeg -ss 00:10:00 -i The.Matrix.1999.BG.EN.sub.mkv -to 00:00:37 -c copy cut.mkv

Ex­pla­na­tions (more de­tails):

  • -i: This spec­i­fies the in­put file. In that case, it is (input.mp4).
  • -ss: Used with ‑i, this seeks in the in­put file (input.mp4) to po­si­tion.
  • 00:01:00: This is the time your trimmed video will start with.
  • -to: This spec­i­fies du­ra­tion from start (00:01:40) to end (00:02:12).
  • 00:02:00: This is the time your trimmed video will end with.
  • -c copy: This is an op­tion to trim via stream copy. (NB: Very fast)
  • The tim­ing for­mat is: hh:mm:ss

Please note that the cur­rent high­ly up­vot­ed an­swer is out­dat­ed and the trim would be ex­treme­ly slow. For more in­for­ma­tion, look at this of­fi­cial ffm­peg ar­ti­cle.

FFm­peg and H.265 En­cod­ing Guide

ffm­peg has sup­port for H.265 / HEVC en­cod­ing us­ing the ​x265 en­coder.

libx265 can of­fer around 25–50% bi­trate sav­ings com­pared to H.264 video en­cod­ed with libx264, while re­tain­ing the same vi­su­al qual­i­ty. These gains will be most pro­nounced at res­o­lu­tions of 1080p and high­er.

Get­ting ffm­peg with libx265 sup­port

ffm­peg needs to be built with the –en­able-gpl –en­able-libx265 con­fig­u­ra­tion flag and re­quires x265 to be in­stalled on your sys­tem. The Com­pi­la­tion Guides show you how to do that.

You can al­so ​down­load a sta­t­ic build, all of which bun­dle libx265.

En­cod­ing

Sim­i­lar to x264, the x265 en­coder has mul­ti­ple rate con­trol al­go­rithms, in­clud­ing:

  • 1‑pass tar­get bi­trate (by set­ting -b:v)
  • 2‑pass tar­get bi­trate
  • Con­stant Rate Fac­tor (CRF)

In this guide we are go­ing to fo­cus on CRF and Two-Pass en­cod­ing, as 1‑pass tar­get bi­trate en­cod­ing is not rec­om­mend­ed.

Con­stant Rate Fac­tor (CRF)

Use this mode if you want to re­tain good vi­su­al qual­i­ty and don't care about the ex­act bi­trate or file­size of the en­cod­ed file. The mode works ex­act­ly the same as in x264, so please read the H.264 guide for more in­fo.

As with x264, you need to make two choic­es:

  • Choose a CRF. The de­fault is 28, and it should vi­su­al­ly cor­re­spond to libx264 video at CRF 23, but re­sult in about half the file size. Oth­er than that, CRF works just like in x264.
  • Choose a pre­set. The de­fault is medi­um. The pre­set de­ter­mines how fast the en­cod­ing process will be at the ex­pense of de­tail. Put dif­fer­ent­ly, if you choose ul­tra­fast, the en­cod­ing process is go­ing to run fast, and the file size will be small­er when com­pared to medi­um. The vi­su­al qual­i­ty will not be as good. Slow­er pre­sets use more mem­o­ry. Valid pre­sets are ul­tra­fast, su­per­fast, very­fast, faster, fast, medi­um, slow, slow­er, verys­low and place­bo.
  • Choose a tune. By de­fault, this is dis­abled, and it is gen­er­al­ly not re­quired to set a tune op­tion. x265 sup­ports the fol­low­ing -tune op­tions: psnr, ssim, grain, ze­ro­la­ten­cy, fast­de­code. They are rex­plained in the H.264 guide.

For ex­am­ple:

ffmpeg -i input -c:v libx265 -crf 28 -c:a aac -b:a 128k output.mp4

This ex­am­ple us­es AAC au­dio at 128 kBit/​​​s. This us­es the ffm­peg-in­ter­nal en­coder, but un­der AAC you will find in­fo about more op­tions.

Two-Pass En­cod­ing

This method is gen­er­al­ly used if you are tar­get­ing a spe­cif­ic out­put file size and out­put qual­i­ty from frame to frame is of less im­por­tance. This is best ex­plained with an ex­am­ple. Your video is 10 min­utes (600 sec­onds) long and an out­put of 200 MiB is de­sired. Since bi­trate = file size / du­ra­tion:

(200 MiB * 8192 [converts MiB to kBit]) / 600 seconds = ~2730 kBit/s total bitrate
2730 - 128 kBit/s (desired audio bitrate) = 2602 kBit/s video bitrate

You can al­so for­go the bi­trate cal­cu­la­tion if you al­ready know what fi­nal (av­er­age) bi­trate you need.

Two-Pass Ex­am­ple

For two-pass, you need to run ffm­peg twice, with al­most the same set­tings, ex­cept for:

  • In pass 1 and 2, use the -x265-params pass=1 and -x265-params pass=2 op­tions, re­spec­tive­ly.
  • In pass 1, out­put to a null file de­scrip­tor, not an ac­tu­al file. (This will gen­er­ate a log­file that ffm­peg needs for the sec­ond pass.)
  • In pass 1, you need to spec­i­fy an out­put for­mat (with -f) that match­es the out­put for­mat you will use in pass 2.
  • In pass 1, you can leave au­dio out by spec­i­fy­ing -an.

For libx265, the -pass op­tion (that you would use for libx264) is not ap­plic­a­ble.

ffmpeg -y -i input -c:v libx265 -b:v 2600k -x265-params pass=1 -an -f mp4 /dev/null && \
ffmpeg -i input -c:v libx265 -b:v 2600k -x265-params pass=2 -c:a aac -b:a 128k output.mp4

Note: Win­dows users should use NUL in­stead of /​​​dev/​​​null and ^ in­stead of \.

As with CRF, choose the slow­est -pre­set you can tol­er­ate, and op­tion­al­ly ap­ply a -tune set­ting. Note that when us­ing faster pre­sets with the same tar­get bi­trate, the re­sult­ing qual­i­ty will be low­er and vice-ver­sa.

Pass­ing Op­tions

Gen­er­al­ly, op­tions are passed to x265 with the -x265-params ar­gu­ment. For fine-tun­ing the en­cod­ing process, you can there­fore pass any op­tion that is list­ed in the ​x265 doc­u­men­ta­tion. Keep in mind that fine-tun­ing any of the op­tions is gen­er­al­ly not nec­es­sary, un­less you ab­solute­ly know what you need to change.

Set­ting Pro­files

Pro­files can be set via the -profile:v op­tion, sim­i­lar to libx264.

Re­size and de­code Video

# h265 720p
ffmpeg -i Exodus.Gods.and.Kings.2014.BG.mkv -vf scale=-1:720 -c:v libx265 -crf 0 -preset veryslow -c:a copy Exodus.Gods.and.Kings.2014.BG.720p.mkv

# h264 720p
ffmpeg -i MyMovie.mkv -vf scale=-1:720 -c:v libx264 -crf 0 -preset veryslow -c:a copy MyMovie_720p.mkv

# h264 720p visually lossless
ffmpeg -i MyMovie.mkv -vf scale=-1:720 -c:v libx264 -crf 18 -preset veryslow -c:a copy MyMovie_720p.mkv

Re­size and de­code Video and Copy­ing all au­dio streams (Se­lect­ing streams with ‑map)

ffmpeg -i in.mkv -vf scale=-1:720 -map 0 out.720p.mkv

Fix 'Past du­ra­tion X.XXX too large'

ffmpeg -i infile.avi outfile.mkv -async 1 -vsync 1
ffmpeg -i file.{avi,mkv} -async 1 -vsync 1

Ref­er­ences:

Un­der­stand­ing FFM­PEG Er­ror Ex­tract­ing Sub­ti­tles

ffmpeg -i in.mkv -vf scale=-1:720 -c:s copy -map 0 out.720p.mkv -async 1 -vsync 1


How do I split an au­dio file in­to mul­ti­ple?

ffmpeg -i somefile.mp3 -f segment -segment_time 3 -c copy out%03d.mp3
  • Where -segment_​​​time is the amount of time you want per each file (in sec­onds).