Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 901
John Roe Avatar asked Oct 24 '13 10:10

John Roe


3 Answers

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
like image 114
damienfrancois Avatar answered Nov 15 '22 08:11

damienfrancois


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.

like image 40
FreeSoftwareServers Avatar answered Nov 15 '22 08:11

FreeSoftwareServers


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
like image 29
Joakim Kartveit Avatar answered Nov 15 '22 08:11

Joakim Kartveit