#! /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"
# 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 "\n $title\n $set\n \n $runtime\n $plot\n--$sender\n \n \n Documentary\n Kultur\n Talkshow\n Short\n \n Animation\n Action\n Adventure\n Biography\n Comedy\n Crime\n Drama\n Food\n Family\n Fantasy\n History\n Mystery\n Politics\n Romance\n Science Fiction\n Thriller\n War\n Western\n" > ${file%*.*}.nfo
echo -e "file \033[0;35m${file%*.*}.nfo\033[0m created"
fi
fi
done
echo -e "\033[0;33mTask completed!\033[0m"