Bash Scripts

From Bit Binary Wiki
Jump to: navigation, search

MythTV Movie Renamer

I had a large amount of videos that weren't named according to MythTV's recommendations and therefore metadata was not being updated. This script does a best guess of renaming the file according to MythTV's naming recommendations. You need to have /usr/share/mythtv/metadata/Movie/tmdb.py from mythtv-common installed.

By default the script will print out what would be done, use -f to actually perform the move.

Used -c for the root of the source directory, it will search recursively from that level down. Use -n for the destination directory, I wrote it to move files into the same destination directory (because MythTV won't care about sub folders per se).

#!/bin/bash

FORCE="false"

tmdb_exe="/usr/share/mythtv/metadata/Movie/tmdb.py"
#cur_dir="${1}"
#new_dir="${2}"


read_dom ()
{
    local IFS=\>
    read -d \< ENTITY CONTENT
}


output_movie()
{
    local name="${@}" ; shift

    while read_dom ; do
        if [[ "${ENTITY}" = "title" ]]
            then title="${CONTENT}"
                echo -n "${title} "
        fi
        if [[ "${ENTITY}" = "subtitle" ]]
            then subtitle="${CONTENT}"
                echo -n "${subtitle} "
        fi
        if [[ "${ENTITY}" = "releasedate" ]]
            then release_date="${CONTENT%%-*}"
            echo "${release_date}.${file_extension}"
        fi
    done < <("${tmdb_exe}" -M "$(echo ${name%.*} \
        | sed -e 's/(//g' -e 's/)//g' -e 's/\-e 's/\//g' -e 's/'\//g' -e 's/-//g' -e 's/\./ /g')") | head -n 1 
}


### command line arguments
while getopts "c:n:f" o ; do
    case "$o" in
        c)  cur_dir="${OPTARG}" ;;
        n)  new_dir="${OPTARG}" ;;
        f)  FORCE="true" ;;
        :)  echo "Option -${OPTARG} requires an argument." ; exit 1 ;;
    esac
done


while read file_name ; do

    file_extension="$(basename "${file_name##*.}")"
    new_name="$(output_movie "$(basename "${file_name%.*}")")"

    if [[ "${FORCE}" == "true" ]] ; then
        [[ "${new_name}" ]] && mv -v -n "${file_name}" "${new_dir}"/"${new_name}"
    else
        [[ "${new_name}" ]] && echo mv -v -n "${file_name}" "${new_dir}"/"${new_name}"
    fi

done < <(find "${cur_dir}" -type f)

Feh Browser

.bin/feh-browse.sh

#!/bin/bash


y="$(stat --printf=%F "${1}")"
feh_file_args="--image-bg black
               --auto-zoom
               --geometry 1024x640
               --fontpath /usr/share/fonts/truetype/ttf-liberation/
               --menu-font LiberationMono-Regular/8
               --font LiberationMono-Regular/8"
feh_dir_args=" ${feh_file_args}
               --sort filename"

case "${y}" in

    'regular file')

        shopt -s nullglob

        file=$(basename -- "${1}")
        dir=$(dirname -- "${1}")
        arr=()
        shift
        
        cd -- "${dir}"
        
        for i in * ; do
            [ -f "${i}" ] || continue
            arr+=("${i}")
            [ "${i}" == "${file}" ] && c=$((${#arr[@]} - 1))
        done
        
        exec feh ${feh_file_args} "${@}" -- "${arr[@]:c}" "${arr[@]:0:c}"
        ;; 

    'directory') exec feh ${feh_dir_args} "${1}"
        ;;

    *) echo "not a regular file or directory" ; exit 1
        ;;

esac


Parens Adder

This hacky script batch adds parentheses around the years in a file named Movie Name 2009.avi i.e. it ends up as Movie Name (2009).avi

It's primaraly useful if using tmdb3.py and your finding it doesn't obtain metadata correctly.

#!/bin/bash

movies_dir="/data/videos/Movies/"

while read line ; do
    movie="$(echo ${line} | sed 's/\ [0-9][0-9][0-9][0-9]\.[a-z][a-z].$//g')"
    year="$(echo ${line} | grep -o -E '\ [0-9][0-9][0-9][0-9]\.[a-z][a-z].$' | grep -o -E '[0-9][0-9][0-9][0-9]')"
    ext="$(echo ${line} | grep -o -E '\ [0-9][0-9][0-9][0-9]\.[a-z][a-z].$' | grep -o -E '\.[a-z][a-z].$')"
    mv "${movies_dir}/${line}" "${movies_dir}/${movie} (${year})${ext}"
done < <(ls "${movies_dir}" | grep -E '^.*\ [0-9][0-9][0-9][0-9]\.[a-z][a-z].$')
Personal tools