#! /bin/bash # Last update 2013/06/22 # # This optionally renames a movie based on imdb metadata and gets its english subtile # This script relies on the powerful lm.py script: https://github.com/RedRise/lm # # Example of use to attempt to rename all the video files in a directory: # find -maxdepth 1 -type f -iregex '.*\.\(mp4\|divx\|avi\|mkv\|mpg\|mpeg\|wmv\|mov\|flv\|rmvb\|asf\|3gp\|vob\)' ! -regex '.*([12][0-9][0-9][0-9],[0-9]\.[0-9],[0-9].*' -execdir GetSub.sh "{}" \; # # Warning: lm.py does not make the distinction if a video is split in two parts: they may both be renamed the same # So be careful if you see the message "rename File-CD2.avi to SomeName.avi, SomeName.avi already exist, are you sure?" # Do not proceed or you'll lose one of them. # The language for the subtitle download. Change according to taste. # Use lowercase standard abbreviation according to ISO639-1 codes, like eng/fre/dut/ger LNG=eng # Min file size in Mb, skip anything smaller MS=500 # Temporary directory where to move the file # Temp dir should be on same filesystem to avoid copy (so /tmp is not suitable) # and avi file should be movable # If you interrupt this script, check to ensure that no files are left in there TD=_tmp_GetSub # You shouldn't have anything to change below here if [ $# -ne 1 ]; then echo "Use: $0 videofile.avi to rename a video file meaningfuly and also get its $LNG subtitle"; exit 1; fi # Trap ctrl-c and call ctrl_c() so we can move the file back to its original place trap ctrl_c INT function ctrl_c() { HERE=$(basename $(pwd)) if [ "$HERE" == "$TD" ]; then cd -; fi if [ -e $TD ]; then mv -i $TD/* .; rmdir $TD; fi exit 1 } # Use a temporary dir so we have only one file in it as lm.py has the drawback of working on an entire tree if [ -e $TD ] then # Aborted previous operation ? mv -i $TD/* . if rmdir $TD; then echo "File(s) remains in ./$TD/, please sort it out"; ls -alF $TD; exit 1; fi fi mkdir $TD mv "$1" $TD/ # We'll move it back later cd $TD/ LM=$(lm.py -lf @size:$MS) # Skip small files Res=$? #LMC=$( echo $LM | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" ) # remove color metachars LMC=$( echo $LM | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | iconv -c -f ASCII ) # remove any extra char # "$LMC" ~= .*NOTFOUND.* || if [[ $Res -ne 0 || "$LMC" == *NOTFOUND* || x"$LMC" == x || "${LMC:0:17}" == "Getting metadata:" ]] then echo "lm.py failed: $LM" # Failed or empty else echo "$LM" # SOURCE=$(echo $LMC | sed -e "s/.*: //") SOURCE=$1 EXT=${1##*.} LEN=$(( ${#1}+2 )) # for the ": " between the two names DEST=${LMC:0:-LEN}.$EXT echo -e "Rename '$SOURCE'\n to '$DEST' ?" select yn in "Rename" "Only get subtitle" "Quit" do case $yn in Rename ) mv -vi "$SOURCE" "$DEST" && SOURCE="$DEST"; break;; "Only get subtitle" ) break;; Quit ) break;; esac done if [[ "$yn" != "Quit" ]] then # Now get the subtitle file(s) lm.py -lf @size:$MS --download $LNG ls *.srt if [ -e *_LM3.srt ] then # Delete if identical cmp -s *_LM1.srt *_LM3.srt && rm *_LM3.srt fi if [ -e *_LM2.srt ] then cmp -s *_LM1.srt *_LM2.srt && rm *_LM2.srt fi if [ -e *_LM1.srt ] then # Give the 1st one the same name as the file name so it starts by default in most video players rename "s/_${LNG^^}_LM1//" *_LM1.srt fi fi fi cd - mv -i $TD/* . # Move the file back to its original location rmdir $TD echo