Skip to content

Instantly share code, notes, and snippets.

@dmgig
Last active February 4, 2019 05:34
Show Gist options
  • Save dmgig/a2d2024aff6812104b376fca3673195c to your computer and use it in GitHub Desktop.
Save dmgig/a2d2024aff6812104b376fca3673195c to your computer and use it in GitHub Desktop.
Basic ffmpeg commands

Some basic ffmpeg commands

convert file from one format to another

ffmpeg -i INPUT.mkv INPUT.mp4

batch convert files in folder

more information at stackoverflow

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

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

cut section from video

ffmpeg -i input.mp4 -ss 4099 -t 65 -acodec copy -vcodec copy output.mp4

-ss start time in seconds

-t length of cut in seconds

concat video or audio

more info at trac.ffmpeg.org

ffmpeg -f concat -safe 0 -i <(printf "file '$PWD/%s'\n" ./*.wav) -c copy output.wav

ffmpeg -f concat -safe 0 -i <(printf "file '$PWD/%s'\n" ./*.mp3) -c copy output.wav

extract audio stream

-write_xing 0 required on mac: https://superuser.com/questions/607703/wrong-audio-duration-with-ffmpeg

ffmpeg -i INPUT.mp4 -write_xing 0 OUTPUT.mp3

replace audio stream

more info at ochremusic.com

ffmpeg -i INPUT.mp4 -i AUDIO.wav -map 0:0 -map 1:0 -c:v copy -c:a aac -b:a 256k -shortest OUTPUT.mp4

extract two audio channels into two files

ffmpeg -i 001.mov  -map 0:1 -acodec copy track1.wav -map 0:2 -acodec copy track2.wav

get length of audio/video (in seconds)

CURRENT_VIDEO_LENGTH=`ffprobe -i $MP3PATH -show_entries format=duration -v quiet -of csv="p=0"`

resize an image to fit within 16:9 frame with transparent background

VW=1280
VH=720
  
ffmpeg -y \
-i $IMGPATH -vf \
"thumbnail,scale=iw*min\($VW/iw\,$VH/ih\):ih*min\($VW/iw\,$VH/ih\),pad=$VW:$VH:\($VW-iw\)/2:\($VH-ih\)/2" \
$PNGPATH

Resize and Concat

# resize
ffmpeg -i original.mp4  -vf scale=960:540 resized.mp4

# concat
ffmpeg -i FIRST.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts; ffmpeg -i  SECOND.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts; ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc OUTPUT.mp4

combine an image and audio track, fade in endcard over last 7 seconds of clip

CURRENT_VIDEO_LENGTH=`ffprobe -i $MP3PATH -show_entries format=duration -v quiet -of csv="p=0"`
OFFSET_TIME=`echo $CURRENT_VIDEO_LENGTH - 7 | bc`

ffmpeg \
-loop 1 -i $PNGPATH \
-loop 1 -i $LOGOIMG \
-i $MP3PATH -c:a aac \
-filter_complex \
"[1:v]fade=t=in:st=$OFFSET_TIME:d=1:alpha=1[endcard]; \
[0:v][endcard]overlay=enable='gte(t,$OFFSET_TIME)'[v]" \
-map '[v]' -map 2:a -shortest -aspect 16:9 -pix_fmt yuv420p -r 30 -movflags +faststart $FINALPATH

Concat large group of files w/ test pattern between

ffmpeg -y -f lavfi -i smptebars=duration=2:size=640x360:rate=25 _testsrc.mp4; ffmpeg -y -f lavfi -i aevalsrc=0 -i _testsrc.mp4 -vcodec copy -acodec aac -map 0:0 -map 1:0 -shortest -strict experimental testsrc.mp4
ffmpeg -safe 0 -f concat -i concat.txt FRANK-CHURCH-JOINED.mp4

sample of concat file

file 'c0044615 _ AP Archive-173e8cca353c992112604fced5bc8bb0_360p25-h264-1500k.mp4'
file 'testsrc.mp4'
file 'c0049179 _ AP Archive-4b3a0ce7e41cd6bb8d5479b5edede36e_360p25-h264-1500k.mp4'
file 'testsrc.mp4'
file 'c0049559 _ AP Archive-989242f5b731632039bca6454acefd06_360p25-h264-1500k.mp4'
file 'testsrc.mp4'
file 'c0050009 _ AP Archive-9534c268f0cd844503229568720b5181_360p25-h264-1500k.mp4'
file 'testsrc.mp4'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment