#! /bin/bash
# Script to create Kodi friendly NFO files and deletes corresponding .txt files if present
# the scipt uses ffmpeg, sed, echo -e and awk
# the script assumes that the files are in the folder this scipt is run and there are
# only video files and possibly correspodning nfo and or txt files
# it does not enter subfolders
# i use this scipt as preparation before i move them into the kodi library.
# i add a lot of genre tags. After this scipt run i manually delete the genre tags not needed
#replace all spaces in filenames
echo -e "\033[0;33m replace spaces in filnames in this directory...\033[0m"
find . -exec rename "s/ /_/g" {} \;
echo -e "\033[0;33m generate .nfo files for files with no corresponding .nfo files...\033[0m"
for file in `find -maxdepth 1 -type f | ls`
do
if [ $file = `basename "$0"` ] #if $file is the scipt itselfe: skip
then
continue
else
if [ -f "${file%*.*}.nfo" ]
then
continue #if nfo already exists, skip this file
else
set=""
plot=""
sender=""
#if the filenames indicates a movie belonging to a set rembember it
if [[ "$file" == *Terra_X* ]]
then
set="Terra X"
fi
if [[ "$file" == *Mit_offenen_Karten* ]]
then
set="Mit offenen Karten"
fi
if [[ "$file" == *Sternstunde_Religion* ]]
then
set="Sternstunde Religion"
fi
if [[ "$file" == *Sternstunde_Philosophie* ]] || [[ "$file" == *sternstundephilosophie* ]]
then
set="Sternstunde Philosophie"
fi
if [[ "$file" == *Einstein-* ]]
then
set="Einstein"
fi
if [[ "$file" == *Arena-* ]]
then
set="Arena"
fi
if [ -f "${file%*.*}.txt" ] #if there is a corresponding txt file, get info from there....
then
#Get everything after "Titel:" & any number of withe spaces from corresponding txt file
title=`sed -n -e 's/^.*Titel:[[:space:]]\+//p' ${file%*.*}.txt`
runtime=`sed -n -e 's/^.*Dauer:[[:space:]]\+//p' ${file%*.*}.txt`
stunden=${runtime::2}
minuten=${runtime:3:2}
runtime=$((${stunden/#0/} * 60 + ${minuten/#0/})) #&& echo "runtime: <runtime>$runtime</runtime>"
# Vorzicht: Bei führender Null werden Zahlen als octal erkannt, daher muss diese gelöscht werden!!
plot=`awk 'END{print $0}' RS= ${file%*.*}.txt`
sender=`sed -n -e 's/^.*Sender:[[:space:]]\+//p' ${file%*.*}.txt`
rm ${file%*.*}.txt
echo -e "file \033[0;35m${file%*.*}.txt\033[0m \033[0;31mdeleted\033[0m"
else #... or get them from filename and with the help of ffmpeg
title=${file//_/ }
runtime=`ffmpeg -i $file 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//`
fi
#generate nfo file
echo -e "<movie>\n <title>$title</title>\n <set>$set</set>\n <rating></rating>\n <runtime>$runtime</runtime>\n <plot>$plot\n--$sender\n </plot>\n \n <genre>Documentary</genre>\n <genre>Kultur</genre>\n <genre>Talkshow</genre>\n <genre>Short</genre>\n \n <genre>Animation</genre>\n <genre>Action</genre>\n <genre>Adventure</genre>\n <genre>Biography</genre>\n <genre>Comedy</genre>\n <genre>Crime</genre>\n <genre>Drama</genre>\n <genre>Food</genre>\n <genre>Family</genre>\n <genre>Fantasy</genre>\n <genre>History</genre>\n <genre>Mystery</genre>\n <genre>Politics</genre>\n <genre>Romance</genre>\n <genre>Science Fiction</genre>\n <genre>Thriller</genre>\n <genre>War</genre>\n <genre>Western</genre>\n</movie>" > ${file%*.*}.nfo
echo -e "file \033[0;35m${file%*.*}.nfo\033[0m created"
fi
fi
done
echo -e "\033[0;33mTask completed!\033[0m"