Created
November 27, 2021 12:25
-
-
Save kowalcj0/aaee77676a135f6af8179caa99291a96 to your computer and use it in GitHub Desktop.
Append video resolution and codec to file name [bash function]
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ismkv
.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:
Dry-mode: