Search code examples
bashshellscripting

I can't make my shell script detect spaces and hyphens in file names


I'm a hobbyist media collector and I recently started a project of mine, being a self-hosted media server with my owned media. I've been doing everything manually for a while but started getting into shell scripting for it to automate a lot of typing. In my script for compressing seasons of series I can't get the script to detect spaces and hyphens on the file name.

There's my script:


echo "Warning! Please ensure before running this script 
    that the files you'd like to convert are in the following format: 
    'series_name - SXXEYY' and mind that by running this script 
    you're aware that the uncompressed files are goint to be 
    automatically deleted. If you change your mind press 'Ctrl + C' 
    to cancel or press [ENTER] to continue:"
read
echo "Enter destination of the input folder:"
read input
echo "Enter series name(how it's written inside the folder):"
read series_name
echo "Enter season number:"
read season_no
echo "Enter number of episodes in the season:"
read ep_no
echo "Enter destination of the output folder:"
read output

for i in $(seq -f "%02g" 1 $ep_no); do
    input_file="$input/"$series_name - S${season_no}E${i}.mkv""
    output_file="$output/"$series_name - S${season_no}E${i}.mkv""
    ffmpeg -i "$input_file" -c:v libx264 -crf 22 -c:a copy -c:s copy -map 0 "$output_file"
done
rm -rf "$input"
clear
echo "Done :)"

and here's how I save my files:

nesco@nesker:~/uncompressed/Rome$ ls
'Rome - S01E01.mkv'  'Rome - S01E02.mkv'

I'm currently extracting the raw files again because last time I ran the script it did not detect and erased the folder


Solution

  • Why are you bothering asking for the season number and number of episodes? Do you typically only want to compress some of the shows before deleting all of them? Why not just process all the files in the "input" directory to the "output" directory:

    #!/bin/bash
    echo "Warning! Please ensure before running this script
        that the files you'd like to convert are in the following format:
        'series_name - SXXEYY' and mind that by running this script
        you're aware that the uncompressed files are going to be
        automatically deleted. If you change your mind press 'Ctrl + C'
        to cancel or press [ENTER] to continue:"
    read
    read -p "Enter destination of the input folder: " input
    read -p "Enter destination of the output folder: " output
    
    if [ ! -d "$input" ]; then
        echo "Error: $input is not a directory."
        exit 1
    fi
    
    if [ ! -d "$output" ]; then
        echo "Error: $output is not a directory."
        exit 1
    fi
    
    set -e # force the shell to exit if commands fail.
    for input_file in "${input}"/*; do
        output_file="$output"/$(basename "$input_file")
        ffmpeg -i "$input_file" -c:v libx264 -crf 22 -c:a copy -c:s copy -map 0 "$output_file"
    done
    rm -rf "$input"
    # clear  # why do this?
    echo "Done :)"
    

    Notice that I put in some error checking, pretty important to do, since you are deleting data.