Last active
February 22, 2021 16:02
-
-
Save savishy/a64132a4ecd5cc553dff to your computer and use it in GitHub Desktop.
Useful Little Scripts
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 | |
set -e | |
echo "==== rename.sh ===== | |
This script recurses through videos in a folder, and | |
renames them into a format as follows: | |
test_[w]x[h]_[bitrate]_[duration].[extension] | |
e.g. | |
test_640x480_2973Kbps_34s.mov | |
This format is useful to parse the filename and get | |
useful information about the file. | |
Note: | |
- mediainfo is needed. It is supported for many major OSes. | |
https://mediaarea.net/en/MediaInfo/Download | |
- For Windows, download and install the commandline (CLI) tool from | |
https://mediaarea.net/en/MediaInfo/Download/Windows | |
Tested on: | |
- Cygwin + Windows | |
" | |
PLATFORM=`uname` | |
if [ $PLATFORM == Linux ]; then | |
echo "OS: $PLATFORM" | |
MEDIAINFO="mediainfo" | |
command -v $MEDIAINFO >/dev/null 2>&1 || { | |
echo "$MEDIAINFO package not found. Proceeding to download. (you can abort the download if you wish)" | |
sudo apt-get install mediainfo | |
} | |
elif [[ "$PLATFORM" =~ "CYGWIN"* ]]; then | |
echo "OS: $PLATFORM" | |
MEDIAINFO="./MediaInfo/MediaInfo.exe" | |
if [ ! -f $MEDIAINFO ]; then | |
echo "MediaInfo tool not found at $MEDIAINFO! Download it from https://mediaarea.net/en/MediaInfo/Download" | |
exit 1 | |
fi | |
else | |
echo "unsupported OS `uname`" && exit 1 | |
fi | |
if [ "$1" == "" ]; then | |
echo "first argument should be folder containing video files" | |
exit 1 | |
fi | |
if ! [ "$2" == "mp4" ] && ! [ "$2" == "3gp" ] && ! [ "$2" == "mov" ]; then | |
echo "warning: 2nd argument appears unsupported (3gp,mov,mp4)" | |
fi | |
FOLDER="$1" | |
SUPPORTED="$2" | |
echo " -- searching $FOLDER" | |
for i in `ls $FOLDER/*.$SUPPORTED`; do | |
DIRNAME=$(dirname $i) | |
DUR_RAW="" | |
BR_RAW="" | |
W_RAW="" | |
H_RAW="" | |
DUR_RAW=`$MEDIAINFO $i | grep -P "^Duration\s+" -m 1 | grep -oP "([0-9]+s)"` | |
BR_RAW=`$MEDIAINFO $i | grep -P "Overall\sbit\srate" | grep -oP "[0-9\.\s]+\s[MK]bps"` | |
W_RAW=`$MEDIAINFO $i | grep -P "Width" | grep -oP "([0-9\s]+)(?=\spixels)"` | |
H_RAW=`$MEDIAINFO $i | grep "Height" | grep -oP "([0-9\s]+)(?=\spixels)"` | |
if [ "$DUR_RAW" == "" ] || [ "$BR_RAW" == "" ] || [ "$W_RAW" == "" ] || [ "$H_RAW" == "" ]; then | |
echo "couldn't parse values; skipping..." | |
continue | |
fi | |
# remove whitespace | |
DUR="$(echo -e "${DUR_RAW}" | tr -d '[[:space:]]')" | |
BR="$(echo -e "${BR_RAW}" | tr -d '[[:space:]]')" | |
W="$(echo -e "${W_RAW}" | tr -d '[[:space:]]')" | |
H="$(echo -e "${H_RAW}" | tr -d '[[:space:]]')" | |
echo -n "---- $i : " | |
echo -n "$DUR, " | |
echo -n "$BR, " | |
echo -n "$W, " | |
echo "$H" | |
# if MOV, rename to mov | |
if [ "$SUPPORTED" == "MOV" ]; then | |
TARGET_EXT="mov" | |
else | |
TARGET_EXT=$SUPPORTED | |
fi | |
RENAME="test_${W}x${H}_${BR}_${DUR}.$TARGET_EXT" | |
if [ $DUR == "" ] || [ $BR == "" ] || [ $W == "" ] || [ $H == "" ]; then | |
echo "couldn't parse values; skipping..." | |
continue | |
fi | |
echo " rename to: $DIRNAME/$RENAME" | |
mv -v $i $DIRNAME/$RENAME | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment