bashvideohandbrake

HandBrakeCLI bash script convert all videos in a folder


Firstly, I searched around for my problem. But none can solve it.

I want to convert all videos file in a directory and the output will be saved in another directory. I got a bash script from somewhere I dont remember.

#!/bin/bash

SRC="/home/abc/public_html/filex/store/vids/toriko/VIDEOS HERE"
DEST="/home/abc/public_html/filex/store/vids/toriko/51-100"
DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI
PRESET="iPhone & iPod Touch"

for FILE in "`ls $SRC`"
do
filename=$(basename $FILE)
extension=${filename##*.}
filename=${filename%.*}

$HANDBRAKE_CLI -i "$SRC"/$FILE -o "$DEST"/"$filename".$DEST_EXT "$PRESET"

done

the problem is, the output of the file will be without filename.. only ".mp4". and, there is only 1 file generated.. means, from 50 videos in the folder, only 1 files generated with name ".mp4" and after that, HandBrakeCLI exit. can anyone fix my code? I got no experince in bash coding.. so, the right script giiven will be appreciate :)


Solution

  • Your line

    for FILE in "`ls $SRC`"
    

    effectively creates only one iteration where FILE contains the list of the files (and it is not able to handle the space in $SRC). Better replace it with

    for FILE in "$SRC"/*
    

    Example:

    $ ls test
    1.txt  2.txt
    $ SRC=test; for f in "`ls $SRC`" ; do echo $f; done
    1.txt 2.txt
    $ SRC=test; for f in "$SRC"/* ; do echo $f; done
    test/1.txt
    test/2.txt
    

    Side note: you can have a space in there with no problem

    $ ls "the test"
    1.txt  2.txt
    $ SRC="the test"; for f in "$SRC"/* ; do echo $f; done
    the test/1.txt
    the test/2.txt