Skip to content

Instantly share code, notes, and snippets.

@kamysheblid
Last active September 4, 2023 19:55
Show Gist options
  • Save kamysheblid/56afde23d79fbc7b5f8c496817e360d5 to your computer and use it in GitHub Desktop.
Save kamysheblid/56afde23d79fbc7b5f8c496817e360d5 to your computer and use it in GitHub Desktop.
convert videos x265 to x264 and change filename
#!/usr/bin/env bash
### Initialize stuff
CUR_DIR=$PWD
OPTIONS="r:f:"
VIDEO_SUFFIXES='mkv|avi|mp4|mov'
HELP_MSG="Usage %s: [-D] [-r regex-remove] [-a regex-array-remove] [-d dir] [-f filename] args
Should work on all video files with suffixes ${VIDEO_SUFFIXES}. Give
it a file or a directory and it will convert all video files it finds
and save it with new filename."
help() {
printf $HELP_MSG
$0
exit 1;
}
### Handle option stuff
# Copied from man 1 getopts
while getopts Dur::af::d::h name
do
case $name in
r) rflag=1
regex="$OPTARG";;
a) aflag=1
regex_array="$OPTARG";;
d) dflag=1
dir="$OPTARG";;
D) echo Enablind DEBUG
set -x
DEBUG=true;;
f) fflag=1
filename="$OPTARG";;
u) ultrafast=1
ULTRAFAST="-preset ulrafast";;
h|?) help;;
esac
done
if [ -v DEBUG ]; then
if [ -n "$rflag" ]; then
printf 'Option -r "%s" specified\n' "$regex"
fi
if [ -n "$dflag" ]; then
printf 'Option -d "%s" specified\n' "$dir"
fi
if [ -n "$ultrafast" ]; then
printf 'Option -u "%s" specified\n' "$ULTRAFAST"
fi
if [ -n "$aflag" ]; then
printf 'Option -a "%s" specified\n' "$regex_array"
fi
if [ -n "$fflag" ]; then
printf 'Option -f "%s" specified\n' "$filename"
fi
printf 'Remaining arguments are: %s\n' "$*"
fi
shift $(($OPTIND - 1))
### The Program
REGEX_RESULT=""
function apply_regex () {
[[ -v DEBUG ]] && printf 'INFO: applying regex: %s\n' "$regex"
printf -v REGEX_RESULT $(sed -E "s/$regex//" <<< $1)
# ERROR: regex fail
[[ $? != 0 ]] && log_exit "regex failed" $regex $fflag
}
function main() {
if [[ -n "$dir" ]]; then
DIR="$dir"
else
DIR="$(dirname "$1")"
fi
OLD_NAME="$(basename "$1")"
is_video "$OLD_NAME";
NEW_NAME="$(sed -E 's/(720|1080)p/480p/g ; s/([xXhH]26)5/\14/g ; s/hevc/h264/g ' <<< $OLD_NAME)"
# ERROR: create new name fail
[[ -z $NEW_NAME ]] && log_exit "something went wrong, creating new name" "$OLD_NAME" $fflag
# ERROR: both names equal
[[ $OLD_NAME = $NEW_NAME ]] && log_exit "couldnt change the name" "$OLD_NAME" $fflag
# If regex is set apply that as well
if [[ $rflag == 1 ]]; then
apply_regex $NEW_NAME
NEW_NAME=$REGEX_RESULT
fi
cd "${CUR_DIR}/${DIR}"
file_exists $NEW_NAME
# Run program if not debugging
if [[ ( $OLD_NAME != $NEW_NAME ) && ( ! -v DEBUG ) && ( $SKIP == 0 ) ]]; then
ffmpeg -i "$OLD_NAME" $ULTRAFAST -c:v h264 -s 800x480 "$NEW_NAME"
# If DEBUG then output info
elif [[ -v DEBUG && $SKIP == 0 ]]; then
echo Old Name: $OLD_NAME
echo New Name: $NEW_NAME
echo Operating Dir: $CUR_DIR
echo Directory: $DIR
echo INFO: ffmpeg -i "$OLD_NAME" $ULTRAFAST -c:v h264 -s 800x480 "$NEW_NAME"
fi
}
function file_exists() {
FILENAME=$1
[[ -a "$FILENAME" ]] && log_exit "file exists" "$FILENAME" $fflag
}
function is_video() {
# Check that it is a video file
grep -qE '($VIDEO_SUFFIXES)$' <<< $1 ||
# ERROR: File exists
log_exit "Not a video" "$1" $fflag
}
function log_exit() {
ERROR_MSG=$1
THING=$2
printf 'ERROR: %s: %s\n' "$ERROR_MSG" "$THING"
# Exit if necessary
[[ $3 == 1 ]] && exit $RET_CODE || SKIP=1
}
if [[ -n $filename ]]; then
[[ -n $DEBUG ]] && echo Running main on $filename
main "$filename"
elif [[ -n $dir ]]; then
[[ -n $DEBUG ]] && echo Running for loop over directory
for file in $dir/* ; do
SKIP=0
[[ -a "$DEBUG" ]] && echo Running main on $file
main "$file"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment