5

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 :)

John Roe
  • 186
  • 1
  • 2
  • 9

5 Answers5

4

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
damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • I modified the script. now the encoding process run well. I can see this by looking the output it created which has its own name instead ".mp4".. let see couple video completed without any error and I will mark your ans :) btw, thanks :) – John Roe Oct 24 '13 at 10:59
3

I tried this script, and others like it, but I wanted to convert recursive directory tree's and have files placed in the same directory with .mp4 extension and delete .avi files, after much trial and error I gave up on this code and searched for a new code, id like to credit

http://www.surlyjake.com/blog/2010/08/10/script-to-run-handbrake-recursively-through-a-folder-tree/

For the original code!

Here is my modified script, barely modified BTW this script is short, sweet and easy to understand.

#!/bin/bash

# This Script Goes in Root Folder of TV show -- Example Folder Structure
# /Stargate/Season\ 1/Epiosde.avi
# /Stargate/Season\ 2/Epiosde.avi
# /Stargate/handbrake_folder.script
# Outputs all Files back inside same dir's and does all folders inside Startgate DIR
# /Stargate/Season\ 1/Epiosde.mp4
# /Stargate/Season\ 2/Epiosde.mp4

# PRESET = -o flags for CLI can be got from GUI under Activity Log or from https://trac.handbrake.fr/wiki/CLIGuide OR you can use actual Presets!

# PRESET="iPhone & iPod Touch"

PRESET="--modulus 2 -e x264 -q 20 --vfr -a 1 -E ac3 -6 5point1 -R Auto -B 384 -D 0 --gain 0 --audio-fallback ac3 --encoder-preset=veryfast  --encoder-level="5.2"  --encoder-profile=high  --verbose=1"

if [ -z "$1" ] ; then
TRANSCODEDIR="."
else
TRANSCODEDIR="$1"
fi
find "$TRANSCODEDIR"/* -type f -name "*.avi" -exec bash -c 'HandBrakeCLI -i "$1" -o "${1%\.*}".mp4 --preset="$PRESET"' __ {} \; && find . -name '*.avi' -exec rm -r     {} \;

BE WARNED: THIS WILL CONVERT THEN DELETE ALL .AVI FILES ABOVE THE SCRIPT IN FILE TREE!

Feel free to remove the

[-name "*.avi"] & [&& find . -name '*.avi' -exec rm -r     {} \;]

to disable only converting .avi and removal of .avi or modify to suite another extension.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57
  • Sorry to bother you, this is failing for me with Current CLI Version: 0.10.2 / OSX 10.11.2 The script seems to immediatly end in an exit; Any ideas?/thanks for your time! – Levi Jan 19 '16 at 04:51
  • 1
    I got a lot longer version [here](https://www.freesoftwareservers.com/index.php/2015/09/04/convert-videos-recursively-with-handbrake-script-handbrakecli/) but just out of curiosity u are converting AVI? and did you try simplifiying PRESET? – FreeSoftwareServers Jan 19 '16 at 04:55
  • I got the script working! Yes, I am converting AVI files, strangly though, the mp4 files are bigger than the original avi files... I did not try to specify a preset, I tried lowering the -q field to 38, but I am getting the same file size. I will try that script, thanks for making helpful things! – Levi Jan 20 '16 at 01:28
1

I have found the solution:

#!/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

for FILE in "$SRC"/*
do
    filename=$(basename "$FILE")
    extension=${filename##*.}
    filename=${filename%.*}
    $HANDBRAKE_CLI -i "$FILE" -o "$DEST"/"$filename".$DEST_EXT
done
0

I just tried using this script with the modification suggested above. I found I need to to put double quotes around the two uses of $FILE in order to handle file names with spaces.

So...

filename=$(basename "$FILE")

and

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

mmcc73
  • 1
  • 1
0

I'd rather prefer this solution:

#!/bin/bash

SRC="$1"
DEST="$2"
EXT='mp4'
PRESET='iPhone & iPod Touch'

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

    HandBrakeCLI -i "$SRC"/$FILE -o "$DEST"/"$filename"."$EXT" "$PRESET"
done
Roger
  • 8,286
  • 17
  • 59
  • 77