Skip to content

Instantly share code, notes, and snippets.

@emjayoh
Last active April 19, 2021 09:00
Show Gist options
  • Select an option

  • Save emjayoh/805f6a9e40249811565a069c8c2121a2 to your computer and use it in GitHub Desktop.

Select an option

Save emjayoh/805f6a9e40249811565a069c8c2121a2 to your computer and use it in GitHub Desktop.
[Audio+VIdeo merge] lossless #ffmpeg #audio #video #cli #bash
ffmpeg -i input.mp4 -i soundtrack.mp3 -filter_complex "[1:a]afade=t=out:st=$(bc <<< "$duration-$fade"):d=$fade[a]" -map 0:v:0 -map "[a]" -c:v copy -c:a aac -shortest with_audio.mp4
ffprobe -v error -select_streams v:0 -show_entries stream=duration -of csv=p=0 input.mp4
#!/bin/bash
f="$*" # all args
p="${f##*/}" # file
fn="${p%.*}" # name only
e="${p##*.}" # extension
echo
echo $f
echo $p
echo $fn
echo $e
echo
read -e -p "Number of seconds of fade-out: " -i 1 sec # prompt for fade duration in seconds
r=$(ffprobe -show_streams "$f" 2> /dev/null | grep r_frame_rate | head -1 | cut -d \= -f 2) # frame rate
let "frames = $r * $sec" # duration of fade as number of frames
echo $r
echo $sec
echo $frames
lf=$(ffprobe -show_streams "$f" 2> /dev/null | grep nb_frames | head -1 | cut -d \= -f 2) # total number of frames
let "lf = $lf - $frames" # initial frame to start fade at
ffmpeg -loglevel quiet -i "$f" -vf fade=out:$lf:$frames -r $r -vcodec libx264 -preset slow -crf 12 -bf 2 -flags +cgop -pix_fmt yuv420p -acodec copy "$fn $sec sec fade-out.$e"
open "$fn $sec sec fade-out.$e"
ffmpeg -i video.mp4 -i audio.wav -codec copy -shortest output.mov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment