Created
November 20, 2023 11:48
-
-
Save isurfer21/df780d3ef3362bae1810a41ed2f55bab to your computer and use it in GitHub Desktop.
A CLI wrapper app to fix the Non-monotonous DTS in media file using ffmpeg
This file contains 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 | |
# Check the number of arguments | |
if [ $# -ne 1 ]; then | |
echo "Error: No arguments provided" | |
exit 1 | |
fi | |
display_help_menu() { | |
echo "Fix the Non-monotonous DTS in media file" | |
echo "Usage:" | |
echo " ${0##*/} <input> <output> <video_tbn> <audio_hz>" | |
echo "Arguments:" | |
echo " input input video file path" | |
echo " output output video file path" | |
echo " video_tbn video timebase number [12800 tbn]" | |
echo " audio_hz audio sample rate [44100 Hz]" | |
} | |
get_timestamped_filename() { | |
file=$(basename $1) | |
file_basename="${file%.*}" | |
file_extension="${file##*.}" | |
echo "$file_basename-$(date +%Y%m%d%H%M%S).$file_extension" | |
} | |
# Loop through the arguments | |
for arg in "$@"; do | |
# Check if the argument is -h or --help | |
case $arg in | |
-h|--help) | |
display_help_menu | |
exit 0 | |
;; | |
esac | |
done | |
if [ $# -ne 1 ]; then | |
display_help_menu | |
exit 1 | |
fi | |
# Assign the arguments to variables | |
input=$1 | |
output=$2 | |
video_tbn=$3 | |
audio_hz=$4 | |
# Set to default value of arguments if missing | |
if [[ ! -f $input ]]; then | |
echo "Invalid 'input' file: $input" | |
exit 2 | |
fi | |
if [ -z "$output" ]; then | |
echo "Warning: Missing 'output' file" | |
output=$(get_timestamped_filename $input) | |
echo "Set output file to '$output' by default" | |
fi | |
if [ -z "$video_tbn" ]; then | |
echo "Warning: Missing 'video_tbn' value" | |
video_tbn=12800 | |
echo "Set video timebase number to $video_tbn tbn by default" | |
fi | |
if [ -z "$audio_hz" ]; then | |
echo "Warning: Missing 'audio_hz' value" | |
audio_hz=44100 | |
echo "Set audio sample rate to $audio_hz Hz by default" | |
fi | |
# Check the format of the arguments | |
if [[ ! $video_tbn =~ ^[0-9]+$ ]]; then | |
echo "Invalid 'video_tbn' value: $video_tbn" | |
exit 2 | |
fi | |
if [[ ! $audio_hz =~ ^[0-9]+$ ]]; then | |
echo "Invalid 'audio_hz' value: $audio_hz" | |
exit 2 | |
fi | |
echo "Probe streams (before)" | |
ffprobe -hide_banner -i $input | |
output_filename=$(basename $output) | |
output_extension="${output_filename##*.}" | |
temp_file="$(date +%Y%m%d%H%M%S)-$output_filename" | |
echo "Fix video timebase number" | |
ffmpeg -hide_banner -i $input -c:v libx264 -c:a copy -pix_fmt yuv420p -video_track_timescale $video_tbn $temp_file | |
echo "Fix audio sample rate" | |
ffmpeg -hide_banner -i $temp_file -acodec aac -vcodec copy -ar $audio_hz $output | |
rm $temp_file | |
echo "Probe streams (after)" | |
ffprobe -hide_banner -i $input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment