Last active
September 11, 2024 14:12
-
-
Save coderofsalvation/8dd3cafd0d21d1fa6dd9cb0de8e58628 to your computer and use it in GitHub Desktop.
ffmpeg commandline crossfade-looped video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
[[ ! -n $3 ]] && { echo "Usage: crossfadevideo <input.mp4> <fade in seconds> <output.mp4> [looptimes]"; exit; } | |
input="$1" | |
fade="$2" | |
duration="$(ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "$input")" | |
duration=$(echo "$duration-($fade)" | bc | sed 's/\..*//g') | |
[[ ${duration:0:1} == "." ]] && duration="0$duration" | |
output="$3" | |
[[ -n $4 ]] && loop=$4 && output="${output}.mkv" | |
set -x | |
ffmpeg -y -i "$input" -filter_complex ' | |
[0]split[body][pre]; | |
[pre]trim=duration='$fade',format=yuva420p,fade=d='$fade':alpha=1,setpts=PTS+('$(($duration-$fade))'/TB)[jt]; | |
[body]trim='$fade',setpts=PTS-STARTPTS[main]; | |
[main][jt]overlay' "$output" | |
# we use mkv for looping since ffmpeg loops mp4 badly | |
[[ -n $loop ]] && ffmpeg -y -stream_loop $loop -i $output -c copy ${output/\.mkv/} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
im getting a freeze for
fade
seconds at the end of the video (if i do not providelooptimes
), and also between every loop when i do providelooptimes
. any idea how i can fix this?this is my ffmpeg version
EDIT: i think this is because im using a file with two streams. removing the audio stream fixes the problem. would be great if we can detect and handle such files in the script