help with recursive handbrake script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with recursive handbrake script
# 1  
Old 08-27-2012
help with recursive handbrake script

I have a batch of mkv video files I would like to transcode to a more compressed format. After spending a significant amount of time experimenting with various parameters I think I have found the handbrake settings that give the best compromise. I now want to construct a simple bash script that will scan the directory containing the original mkv files, recreate the directory tree and then transcode them into the new directory. I then need to alter the file name to replace the underscores with spaces and add the episode number to the beginning based on the original file name.

My original file structure is as follows:

original_series
|
|-series1
| |-episode_1.s01e01.mkv
| |-episode_2.s01e02.mkv
|
|-series2
| |-episode1.s01e01.mkv
| |-episode2.s02e02.mkv

where the s01 represents season 1 and e01 or e02 represent episode in the season as per thetvdb.com. I would like to replicate this directory structure and change the file name to add the season number and episode number to the front of the file based on the sXX and eXX values, so that the new structure looks like:

transcoded_series
|
|-series1
| |-1-1-episode 1.mp4
| |-1-2-episode 2.mp4
|
|-series2
|-1-1-episode 1.mp4
|-2-2-episode 2.mp4

My first attempt looks like this:
Code:
#!/bin/bash

sourcedir="/media/video/original_series"
destdir="/media/video/transcoded_series"
cd "$sourcedir"

for i in *.mkv; do

HandBrakeCLI -i "$i" -o "$destdir/${i%.*}.mp4" -e x264 -q 20.0 -E copy  -B auto -6 auto -R Auto -D 0.0 \
 -f mkv --detelecine --decomb  --loose-anamorphic -m -x b-adapt=2:rc-lookahead=50

done

but it is not recursive, I have no idea how to recreate the directory tree and no idea how to alter the file namesSmilie

I would be really grateful for some (a lot of) help!

Last edited by jim mcnamara; 08-27-2012 at 06:20 PM..
# 2  
Old 08-27-2012
To make the directories:
Code:
cd /media/video/original_series
find . -type d | sort > /tmp/mdirlist.txt
cd /media/video/transcoded_series
while read d
do
   [ ! -d $d ]  && mkdir $d
done < /tmp/mdirlist.txt

It is not clear to me, at all, what all of the options to HandBrakeCLI are doing.
So this is a guess.
Code:
src=/media/video/original_series
dest=/media/video/transcoded_series
cd $src
find . -type f -name '*.mkv' |
while read fname 
do
   ofile=$dest/${fname}
   ofile=${ofile%.*}.mp4
   HandBrakeCLI -i "$fname" -o "$ofile" -e x264 -q 20.0 -E copy  -B \ 
   auto -6 auto -R Auto -D 0.0 \
    -f mkv --detelecine --decomb  \
  --loose-anamorphic -m -x b-adapt=2:rc-lookahead=50
done

# 3  
Old 08-28-2012
Jim, Thanks for you help.

It nearly works - the new directories are created but only the last mkv file in the last directory is transcoded, for example if I have:
dir1 - file1.mkv & file2.mkv
dir2 - file3.mkv & file4.mkv
only file4 is transcoded in the new dir2 that is created, dir1 is also created but remains empty.

I checked that the mdirlist.txt is correct.

Any further suggestions?
# 4  
Old 08-28-2012
what is the output of this

Code:
src=/media/video/original_series
dest=/media/video/transcoded_series
cd $src
echo $src
find . -type f -name '*.mkv'
echo $dest
cd $dest
find . -type f

# 5  
Old 08-29-2012
it basically lists everything in the source dir followed by everything in the destination dir, ie:
Code:
/media/video/original_series
./TEST2/TortillaFlaps.s01e05.mkv
./TEST2/WestOfPesos.s01e08.mkv
./TEST/AMessageToGracias.s01e14.mkv
./TEST/AHuntingWeWillGo.s01e27.mkv
/media/video/transcoded_series
./BEN_HOLLYS_LITTLE_KINGDOM/extrafanart/477431.jpg
./BEN_HOLLYS_LITTLE_KINGDOM/logo.png
./BEN_HOLLYS_LITTLE_KINGDOM/poster.jpg
./BEN_HOLLYS_LITTLE_KINGDOM/banner.jpg
./BEN_HOLLYS_LITTLE_KINGDOM/landscape.jpg
./BEN_HOLLYS_LITTLE_KINGDOM/TheLostEgg.s01e11.mp4
./BEN_HOLLYS_LITTLE_KINGDOM/fanart.jpg
......etc, etc

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

How to solve compiling handbrake errors?

Hello friends, Am trying to compile and build Handbrake 0.9.9 on my redhat box but getting the below errors. Same errors when I compile the source from github and also from the official web site. # uname -a Linux blah.com 2.6.18-274.el5 #1 SMP Fri Jul 8 17:36:59 EDT 2011 x86_64 x86_64... (0 Replies)
Discussion started by: prvnrk
0 Replies

2. Shell Programming and Scripting

Help with creating recursive unzip script

Hello. I am trying to create a script that recursively looks at my folder structure, and extracts any .7z files that have not already been extracted yet. I know that all .7z files only have a single .txt file in them, and the only .txt file in any directory would be from that .7z file. I am... (3 Replies)
Discussion started by: Davinator
3 Replies

3. UNIX for Advanced & Expert Users

recursive grep

Hi, on AIX 6.1 , in man document for grep : -r Searches directories recursively. By default, links to directories are followed. But when I use : oracle@XXX:/appli/XXX_SCRIPTS#grep -r subject *.sh It returns nothing. However I have at least one row in a file : ... (3 Replies)
Discussion started by: big123456
3 Replies

4. Shell Programming and Scripting

Logic needed to recursive looping in the script

Hello i have a requirement where in a file i will get string. The length could be from 1 to 20. if the string is less than 6 characters ( ex: ABCD) . i need to append 'X' on right hand side to make it 6 characters (ex: ABCDXX). if suppose i get the same string from the file as ABCDXX then i... (5 Replies)
Discussion started by: dsdev_123
5 Replies

5. Shell Programming and Scripting

Recursive Shell script

I'm trying to run the script below recursively but it is generating multiple and simultaneous processes. how do I do to start same script again (last line of my script) only after he had just run entirely? The file name is bachup.sh I am using this command: # / bin / bash / ... (8 Replies)
Discussion started by: sprool
8 Replies

6. Shell Programming and Scripting

Recursive call to find files and directories in Shell script from current path.

################################################################ Copy this script to your path from where you want to search for all the files and directories in subdirectories recursively. ################################################################# code starts here... (2 Replies)
Discussion started by: Ramit_Gupta
2 Replies

7. UNIX for Dummies Questions & Answers

Recursive grep

Hello, First time post - I have no formal unix training and could use some help with this. I have a list of strings in File1 that I want to use to do a recursive search (grep) under a specific directory. Here is an example of the string I need to search: /directory/dire... (16 Replies)
Discussion started by: upstate_boy
16 Replies

8. Shell Programming and Scripting

Script problem due to recursive directories Help please

Hello everyone , I am looking for a better solution then the one I have created for the my Task The task is: Create an automated script that will check for Uploads in a specified Directory and move them to another specified Directory if the files are completely uploaded. Files are FTP'd to... (2 Replies)
Discussion started by: robertmcol
2 Replies

9. Shell Programming and Scripting

Recursive pid script

I'm trying to create a script that allows me to determine all the pid's that spawned from an original process(ie - who's running this command).... I've created a script that searches the processes running based on an argument passed the script. It then is to get the parent pid and look that... (3 Replies)
Discussion started by: jbarnhar
3 Replies
Login or Register to Ask a Question