User Tools

Site Tools


mbstring extension must be loaded in order to run mPDF
ffmpeg

FFmpeg

Installation using brew

  1. brew tap varenc/ffmpeg
  2. brew install varenc/ffmpeg/ffmpeg –with-fdk-aac

x265 conversion

Command Effect
Conversion to Apple-compatible H265
Convert non-H265 with existing AAC audio for i in *.mp4;ffmpeg -i $i -c:v libx265 -crf 28 -preset veryfast -vtag hvc1 -movflags faststart -c:a copy ${i/.mp4}.x265.mp4
Convert non-H265 without existing AAC audio for i in *.mp4;ffmpeg -i $i -c:v libx265 -crf 28 -preset veryfast -vtag hvc1 -movflags faststart -c:a aac -b:a 128k ${i/.mp4}.x265.mp4
Convert H265 MKV without existing AAC audio for i in *.mkv;ffmpeg -i $i -vcodec copy -c:a aac -b:a 128k -pix_fmt yuv420p -tag:v hvc1 -movflags faststart ${i/.mkv}.mp4
Convert hev1 tag to hvc1 for i in *.x265.mp4;ffmpeg -i $i -c copy -map 0 -vtag hvc1 -movflags faststart -c:a copy ${i/.mp4}.hvc1.mp4

Trimming

ffmpeg -i input.mp4 -ss 00:02:39 -to 00:03:05 -c:v copy -c:a copy output.mp4

Splitting into parts

for i in 0 30 60 90 120 150 180 210 240 270 300;ffmpeg -i dragon4.mix\ -\ Amateur\ Teikyou\ CD-ROM\ \(Japan\)\ \(NEC\ PC-FXGA\)-pBJu9J-Jw98.mp4 -ss $i -t 30 dragon_$i.mp4

Join audio and video tracks

ffmpeg -i video.mp4 -i audio.m4a -c copy output.mp4

Joining multiple videos into one

ffmpeg -i “concat:input1.mp4|input2.mp4|input3.mp4|input4.mp4” -c copy output.mp4

Remux MKV to MP4 including all streams

ffmpeg -i $i -codec copy -map 0 ${i/.mkv}.mp4

Export all audio tracks from input video files

  1. Check which tracks are present and which IDs they have.
    ffprobe -v error -show_entries stream=index,codec_name,codec_type input.mkv
  2. For the main conversion command, note that the first audio track in the source file is 0:a:0, the second is 0:a:1, etc. Here is an example for a batch one-line:
    exportdir=/yoko/share/export; for i in *.mp4;ffmpeg -i $i -map 0:a:0 -c copy $exportdir/${i/mp4}deutsch.m4a -map 0:a:1 -c copy $exportdir/${i/mp4}english.m4a

MP4 Fast Start

Check if MOOV atom is at the beginning of the file

ffmpeg -v trace -i file.mp4 2>&1 | grep -e type:'mdat' -e type:'moov'

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5576c36880] type:'moov' parent:'root' sz: 9919011 40 1603822784
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5576c36880] type:'mdat' parent:'root' sz: 1593903733 9919059 1603822784

Enable Fast Start/move MOOV atom to beginning of file

ffmpeg -i original.mp4 -movflags faststart -c copy new.mp4

Subtitles

Shifting subtitle timestamps

ffmpeg -itsoffset 23.5 -i input.srt -c copy output.srt

Batch processing

for i in *.srt;do ffmpeg -itsoffset 23.5 -i $i -c copy ${i/srt/shifted.srt};done

Various utilities

lsmedia.zsh

lsmedia () {
        ls -1rt $@ | while read -r line
        do
                echo \"$line\"
                mediainfo --Inform="Video;%CodecID%" $line
                mediainfo --Inform="Audio;%CodecID%" $line
        done | sed 'N;N;s/\n/\; /g' | column -t -s ";"
}
lsmedia Examples
Command Result
lsmedia *.MOV
"MVI_9797.MOV"   avc1   sowt
"MVI_9798.MOV"   avc1   sowt
"MVI_9799.MOV"   avc1   sowt
"MVI_9800.MOV"   avc1   sowt
"MVI_9801.MOV"   avc1   sowt
lsmedia *.MOV | sed 's/\“\ \ .*$/\”/'
"MVI_9797.MOV"
"MVI_9798.MOV"
"MVI_9799.MOV"
"MVI_9800.MOV"
"MVI_9801.MOV"
ffmpeg.txt · Last modified: by wolfo

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki