Skip to content

Instantly share code, notes, and snippets.

@kowalcj0
Created November 27, 2021 12:25
Show Gist options
  • Save kowalcj0/aaee77676a135f6af8179caa99291a96 to your computer and use it in GitHub Desktop.
Save kowalcj0/aaee77676a135f6af8179caa99291a96 to your computer and use it in GitHub Desktop.
Append video resolution and codec to file name [bash function]
function renamevideofiles(){
input_ext="mkv"
dry_mode=
local OPTIND o d
while getopts :d: name; do
case "${name}" in
d)
dry_mode=1
input_ext="${OPTARG}"
;;
*)
echo "Usage: renamevideofiles [-d] video_extension"
return
;;
esac
done
shift $((OPTIND-1))
if [ ! -z "$dry_mode" ]; then
echo "Dry-mode enabled"
fi
for file in *".${input_ext}" ; do
filename="${file%.*}"
extension="${file##*.}"
vid_details=`ffprobe -v error -select_streams v -show_entries stream=codec_name,width,height -of default=noprint_wrappers=1:nokey=1 "${file}"`
readarray -t strarr <<< "$vid_details"
vid_codec=${strarr[0]}
vid_width=${strarr[1]}
vid_height=${strarr[2]}
if [ ! -z "$dry_mode" ]; then
echo mv "${file}" "${filename}-[${vid_width}x${vid_height}][${vid_codec}].${extension}"
else
mv "${file}" "${filename}-[${vid_width}x${vid_height}][${vid_codec}].${extension}"
echo Renamed "${file}" to "${filename}-[${vid_width}x${vid_height}][${vid_codec}].${extension}"
fi
done
}
@kowalcj0
Copy link
Author

This bash function appends video width, height & codec to all video files that match provided file extension. It uses ffprobe to detect video properties. The default file extension is mkv.
There's also dry-mode option available. Just pass -d followed by the file extension to only print the rename command without executing it.

Examples:

Rename mode:

renamevideofiles
Renamed vid1.mkv to vid1-[1280x720][h264].mkv
Renamed vid2.mkv to vid2-[1280x720][vp9].mkv
Renamed vid3.mkv to vid3-[1280x720][av1].mkv

Dry-mode:

renamevideofiles -d mkv
Dry-mode enabled
mv vid1.mkv vid1-[1280x720][h264].mkv
mv vid2.mkv vid2-[1280x720][vp9].mkv
mv vid3.mkv vid3-[1280x720][av1].mkv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment