Page 1 of 2

Batch conversion using CLI via Perl or Shell script??

Posted: Wed Feb 27, 2008 7:50 am
by m021478
I would be genuinely grateful if someone could help me finally figure how how to use either the perl script or the shell script (whose links are referenced below) to automate the process of batch converting hundreds of VIDEO_TS rips using a single specified present of my choosing...

VERY IMPORTANT!! -- Please refer to this forum thread: http://forums.macosxhints.com/showthread.php?t=86371 - in which you will see that I have beat the hell out of this issue for the last several days, and haven't had any success...

As you will notice from my initial post in the thread listed above, I am completely clueless when it comes to *nix - but as you may also notice from the last post in this thread...I AM SO CLOSE TO GETTING IT TO WORK!!

Any suggestions would be so completely vindicating, and would really help me feel as though I've not wasted hours of my life dealing with this issue (and would be very much appreciated on top of that)...

Thanks!

-------------

Perl Script : http://www.macosxhints.com/dlfiles/mediaforker_pl.txt -- (this is the script I've been dealing with throughout the thread listed above)

Shell Script : http://www.realisticsoftware.com/pages/ ... ml#MFbatch

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Wed Feb 27, 2008 2:59 pm
by jbrjake
There aren't any CLI wrapper scripts that have been updated for HB 0.9.2, especially not one still branded for MediaFork, which had its one and only release over a year ago. HBEncoder and PineAppleCLI are much more up to date.

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Sat Mar 08, 2008 9:38 pm
by ronnor
This is slightly different from your request, but I figured out to to loop through a dir full of MPEGs (from my BeyondTV PVR).

First, I had to copy them to my Mac and ran Automator to remove all the spaces and odd characters (like replace ampersands and parentheses with dashes or underscores).

Then I created a shell script to find all the MPGs and process them. My files were destined for an iPod Touch, but you can change the args as necessary. Handbrake presets should work too. Don't forget to chmod the file so you can execute it.

Just pass it the dir you want to process like this:

./myscript.sh /Users/somename/somedir

It will create a new file and add .mp4 to the end of the existing filename.

Code: Select all

#!/bin/sh

for i in $(find $1 -name '*.mpg') ; do

/Applications/Video/HandBrakeCLI -i $i -o $i.mp4 -e x264 -b 300 -B 128 -R 48 -E faac -f mp4 -I -w 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1 -2 -T

done

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Sun Mar 09, 2008 3:30 am
by ronnor
This seems to work ok for VIDEO_TS directories:

./myscript.sh /pathToSomeDirContainingABunchOfVIDEO_TSdirectories (it works recursively )

Obviously, you need to change your args for the bitrate, etc.

Code: Select all

#!/bin/sh

for i in $(find $1 -name VIDEO_TS) ; do

/Path/to/HandBrakeCLI -i $i -o $i.mp4 -e x264 -b 300 -B 128 -R 48 -E faac -f mp4 -I -w 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1 -2 -T -L

done
or to use a preset...

Code: Select all

#!/bin/sh


for i in $(find $1 -name VIDEO_TS) ; do

#default
/path/to/HandBrakeCLI -i $i -o $i.m4v  --preset="AppleTV" -2 -T -L -P


done


Re: Batch conversion using CLI via Perl or Shell script??

Posted: Sun Mar 09, 2008 11:21 pm
by micster
Here is a little shell script that I wrote. You put this script inside the directory with all of your video files to be encoded (in my case I use .iso but it should work for VIDEO_TS) and it will encode them one after another creating a .mp4 named the same thing as my .iso and then deleting the original .iso file before moving on to the next file until it has done all the files in that directory. I'm left with an entire folder of encoded files that all used the same setting. I then move all the finished files to their final resting place and then fill the directory back up with more .iso and start the script again. Heres the script:

Code: Select all

#!/bin/sh
# BatchEncode.sh is a script to batch execute the HandBrakeCLI
# Copyright (C) 2007  Mic Marvin

# set the global variables to default
toolName="HandBrakeCLI"
toolPath="/usr/local/bin/$toolName"
toolArgs="-e x264 -b 2500 -B 160 -R 48 -E faac -f mp4 -x level=41:subme=5:me=umh -m -p"
inputSearchDir="/var/lib/mythtv/videos"
outputDir="/var/lib/mythtv/videos"

# start main script
for moviefile in *.iso
do
dvdName=`basename "$moviefile" .iso`
inputFilePath="$inputSearchDir/${moviefile}"
outputFilePath="$outputDir/$dvdName.mp4"

cmd="$toolPath -i $inputFilePath $toolArgs -o $outputFilePath"

	eval $cmd
	rm -f -v $moviefile
	
done

exit 0
A few things to note are: once it starts there is no graceful way to stop it. The best thing I found is to wait until right after it finishes a file and then kill the script. It would be nice if I could schedule the script to run only for specific time periods. It would also be nice if there was a few seconds delay in between each encode where the script would ask if you want to continue or stop for now. That way it could still go on autopilot, but you would also have control. The script will only encode files that are in the same directory as the script, otherwise you get weird results and it will only encode files that were in the directory when the script starts running. For some reason I have to use "sudo ./BatchEncode.sh" for it to run, but thats probably due to my setup.

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Wed Mar 12, 2008 8:16 pm
by Cyander
I actually have a Bash script helper I use to write up rip scripts for TV shows. Mostly to ensure consistency in the rip itself if I am running a TV series, and so I can do some really wicked things with a simple batch script + csv file. :)

I'll post it and a sample later tonight. I have a version now where you source it in your bash script that does the rip, and just call out to the commands in the helper script. So you configure your settings as needed in your rip script, and then run a series of encodes from there. Since I am upgrading to an 8-core Mac Pro, I recently added support for setting how many encodes to run at the same time as well. So with a single rip script, even though you encode items in order, you can tell it to encode multiple items at the same time in subprocesses.

It is limited in some of the advanced encoder options, as I really only use two sets of settings in most cases. One for PC/ATV, and another for PC/ATV/iPhone. It isn't hard at all to modify this though.

I am also tinkering with additions to the helper script which can call out to AtomicParsley's CLI and help automate the task of tagging the files as well, immediately after the encode.

EDIT: Whoops, I know I meant to post the helper script and a sample rip script... but I got distracted with the AtomicParsley additions. Unfortunately, it doesn't work without AP anymore, so I need to do some fixes to the script before I post it so AP is optional.

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Fri Apr 25, 2008 4:51 pm
by mac.jedi
I've been using one of shell scripts listed above with great success, but my limited knowledge has kept me from being able to have it do everything I'd like.

The old mediaforker.pl and mediaforkCLI-batch.sh seem to be exactly what I'm looking for, but haven't figured out how to get either of them running.

Can anyone point me to a resource or a bit of code that can help me to create a batch script that will scan a TS folder and batch process each -t longer than say 20 minutes?

Any help would be greatly appreciated ... I've been working on this way to long.

Thanks!

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Mon May 05, 2008 9:26 pm
by ronnor
This is my evolving script for transcoding MacTheRipper files. Just save the file in a logical place (I keep it in the Movies Folder... same place I rip movies to). It will loop through all titles longer than X.

1) Save this to a file called "handbrake-longer-than.sh"

Code: Select all

#!/bin/sh

# usage is ./thisScript.sh minimumDuration SomeDirContainingVIDEO_TS
# duration is set in minutes ("30" equals 30 minutes)
# specify the container directory for the VIDEO_TS directory
# example ./handbrake-longer-than.sh 30 24_SEASON6_D1

#set default path to HandBrakeCLI
PathToHandBrakeCLI=/some/path/to/HandBrakeCLI


if [ "${1}x" = "x" ]; then
        echo "Minutes not provided"
        exit
fi

if [ "${2}x" = "x" ]; then
        echo "VIDEO_TS path not provided"
        exit
fi


time=$1
export time


for i in $(find $2 -type d -name VIDEO_TS) ; do

        for title in $($PathToHandBrakeCLI -t 0 -i $i -L 2>&1 | grep "has length" | sed 's/sec//' | sed 's/[()]//g' | awk '$8 > (60 * ENVIRON["time"]) { print $3 "-" $5 }   ') ; do

	#this names the title for the output file
	titlenum=$(echo $title | cut -f 2 -d '-')
        
	# you can change the preset or any other variables here
        $PathToHandBrakeCLI -i $i -o ${2}-title${titlenum}-appletv.m4v  --preset="AppleTV" -2 -T -P -t ${titlenum}
	
	#output example:  24_SEASON6_D1-title1-appletv.m4v
	
        done

done

2) Within the script, set the path to your HandBrakeCLI app on line 9.

3) Don't forget to run this before trying to execute it:

Code: Select all

chmod 777 ./handbrake-longer-than.sh
4) Execute it like this:

Code: Select all

./handbrake-longer-than.sh 30 24_SEASON6_D1
Where "30" is the shortest title you want to export and "24_SEASON6_D1" is the name of the folder containing your VIDEO_TS directory.

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Sun Jun 22, 2008 7:34 am
by Minbari
Using linux I typically use "vobcopy -l" and copy all the ripped content into a directory called 000 and then use a command line to batch convert everything in that directory to an alternate directory called 111 from where I manually load the files into respective categories.

This is a one liner batch rip.

Keep in mind I use this for the ps3, the format is perfect for viewing on a 42 " LCD, dont be fooled by the small size :).

The -C is nailed to use two cores so you may need to adjust to fit your needs.

============
for file in `ls /mnt/Media/Video_DVD/000`; do $(/var/Download/HandBrakeCLI -v -i /mnt/Media/Video_DVD/000/${file} -o /mnt/Media/Video_DVD/111/"${file%.vob}.mp4" -C 2 -T -N eng -e x264 -b 1300 -B 192 -E facc -R 48 -f mp4 level=41:subme=5:me=umh -U -F -w 480 -Y 320); done
============
The above is all on one line "must be joined"

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Thu Aug 14, 2008 7:09 pm
by SnorreSelmer
ronnor, that script is almost exactly what I'm looking for, but with one thing missing: shorter-than!

I'm currently ripping my B5 DVD-set to my iPhone and a longer-than script works 80% for me. Thing is, the DVDs are structured so that Title 1-4 are the individual episodes on each disc and Title 5 is a single title that plays all the individual episodes back to back.

If I run your script I'd get the four episodes and an extra title of wasted CPU-cycles.
Could you please mod your script in a way that lets me define a minimum and maximum time to use as a title-filter?

Thanks in advance.

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Fri Aug 29, 2008 2:59 pm
by trondmm
ronnor wrote:This is slightly different from your request, but I figured out to to loop through a dir full of MPEGs (from my BeyondTV PVR).

Code: Select all

for i in $(find $1 -name '*.mpg') ; do

/Applications/Video/HandBrakeCLI -i $i -o $i.mp4 -e x264 -b 300 -B 128 -R 48 -E faac -f mp4 -I -w 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1 -2 -T

done
Hi.

I can't get this to work. Am I missing something very obvious here? I mean - is Handbrake even supposed to be able to convert files that aren't DVDs?

I always get error messages like this:

Code: Select all

HandBrake 0.9.2 (2008021900) - http://handbrake.m0k.org/
2 CPUs detected
Opening testfile.mpg...
ERROR: dvd: DVDOpen failed (testfile.mpg)No title found.
HandBrake has exited.
I've used handbrake a lot lately to convert DVDs (both from VIDEO_TS folders and iso-images), and this has worked very well. Now I have a few mpeg and mkv-files that I'd like to convert too, preferrably using the same settings. So, it would be very useful if I could use handbrake for this too. If not - is there a way to figure out the corresponding ffmpeg-parameters given a set of handbrake CLI-parameters?

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Fri Aug 29, 2008 3:10 pm
by ronnor
trondmm, can you post your full CLI command and any changes you made to the script?

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Fri Aug 29, 2008 9:04 pm
by trondmm
trondmm, can you post your full CLI command and any changes you made to the script?
Sure. I didn't actually use the script, though. I tried to run the handbrake command your script executed for each of the files it found.

Like this:

Code: Select all

$ handbrake -i test.mpg -o test.mp4 -e x264 -b 300 -B 128 -R 48 -E faac -f mp4 -I -w 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1 -2 -T
HandBrake 0.9.2 (2008021900) - http://handbrake.m0k.org/
2 CPUs detected
Opening test.mpg...
ERROR: dvd: DVDOpen failed (test.mpg)No title found.
HandBrake has exited.
Now, as far as I can tell. Neither of the arguments in that commandline should have any impact on the kind of input handbrake accepts, so I've tried with the smallest possible set of arguments as well.

Code: Select all

$ handbrake -i test.mpg -o test.mp4
HandBrake 0.9.2 (2008021900) - http://handbrake.m0k.org/
2 CPUs detected
Opening test.mpg...
ERROR: dvd: DVDOpen failed (test.mpg)No title found.
HandBrake has exited.
I've also tried with an avi-file...

Code: Select all

$ handbrake -i test.avi -o test.mp4
HandBrake 0.9.2 (2008021900) - http://handbrake.m0k.org/
2 CPUs detected
Opening test.avi...
ERROR: dvd: DVDOpen failed (test.avi)No title found.
HandBrake has exited.
and an mkv-file...

Code: Select all

$ handbrake -i test.mkv -o test.mp4
HandBrake 0.9.2 (2008021900) - http://handbrake.m0k.org/
2 CPUs detected
Opening test.mkv...
ERROR: dvd: DVDOpen failed (test.mkv)No title found.
HandBrake has exited.
with the same result.

Finally, I tried the same command-line I've successfully used for converting my DVDs to PSP

Code: Select all

$ handbrake --aencoder faac --ab 64 --arate 48 --mixdown stereo --encoder x264 --vb 192 --format mp4 --x264opts "crf=18:ref=2:mixed-refs=1:bframes=3:b-rdo=1:bime=1:weightb=1:subme=7:trellis=2:analyse=all:level=3:merange=64:no-fast-pskip:me=umh:sar=1/1" --deblock --two-pass --turbo --drc 2 --width 480 --height 272 -i test.mpg -o test.mp4
HandBrake 0.9.2 (2008021900) - http://handbrake.m0k.org/
2 CPUs detected
Opening test.mpg...
ERROR: dvd: DVDOpen failed (test.mpg)No title found.
HandBrake has exited.
I've also reread the output from handbrake --help a couple of times, and I've found no hints of how to use handbrake with anything other than DVDs.

So, I'm starting to wonder if handbrake (or at the very least, my installation of handbrake) is able to convert standalone video files at all.

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Fri Aug 29, 2008 9:20 pm
by ronnor
ronnor wrote:So, I'm starting to wonder if handbrake (or at the very least, my installation of handbrake) is able to convert standalone video files at all.
In a nutshell, it will process any mpeg2 file (DVD, Capture from most PVR cards, Transport Stream files from OTA HD, etc.).

If your file is not mpeg2, try using Visual Hub.

If your file IS mpeg2, let me know... maybe send me the file to test on my end. (Did you try the GUI to see if you got an error there?)

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Fri Aug 29, 2008 9:36 pm
by trondmm
ronnor wrote:In a nutshell, it will process any mpeg2 file (DVD, Capture from most PVR cards, Transport Stream files from OTA HD, etc.).
Aha! Of course.

Neither of the files I've tested are mpeg2, so that explains it.
ronnor wrote:(Did you try the GUI to see if you got an error there?)
No. I'm only using the command line version on Linux.


Oh well. I guess I'll have to see if I can make sense of ffmpeg or mencoder instead (the ffmpeg command line options scares me slightly :) ) for these files, then.

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Sat Sep 06, 2008 10:29 am
by SnorreSelmer
OK, I've been trying to mod ronnor's script a bit to make it better at doing multiple directories in one go.

My folder structure:

Code: Select all

Series/
  24/
    24_SEASON1_DISC1/
    24_SEASON1_DISC2/
    24_SEASON1_DISC3/
    ...
  BABYLON_5/
    B5_SEASON1_DISC1/
    B5_SEASON1_DISC2/
    B5_SEASON1_DISC3/
    ...
The problem with ronnor's script is that if I run handbrake-longer-than.sh 30 24 (the base folder for 24) I would get files named 24-title1.mp4, which is fine for the first disc. As soon as the script starts on Disc 2, the previous files are overwritten.

What I'm trying to make is a variable that formats a nice per-disc-unique filename that names the output 24_SEASON1_DISC1-title1.mp4 instead.

I've managed to get this far:

Code: Select all

filename=$(echo $i | sed 's/[/]/ /g')
- $i is the path to the VIDEO_TS folder from the foldername given at execution (ex. "24/24_SEASON1_DISC1/VIDEO_TS")
- the sed-command replaces all '/' with a ' ' (ex. "24 24_SEASON1_DISC1 VIDEO_TS")

What I'm missing now is a way to only return the second text-block to the variable (ex. "24_SEASON1_DISC1"). Then I can do HandBrakeCLI -i $i -o ${filename}-title${titlenum} -t ${titlenum} in the script.

Finally, here's the full script:

Code: Select all

    #!/bin/sh

    # usage is ./thisScript.sh minimumDuration SomeDirContainingVIDEO_TS
    # duration is set in minutes ("30" equals 30 minutes)
    # specify the container directory for the VIDEO_TS directory
    # example ./handbrake-longer-than.sh 30 24_SEASON6_D1

    #set default path to HandBrakeCLI
    PathToHandBrakeCLI=/home/snorre/hb/trunk/HandBrakeCLI


    if [ "${1}x" = "x" ]; then
            echo "Minutes not provided"
            exit
    fi

    if [ "${2}x" = "x" ]; then
            echo "VIDEO_TS path not provided"
            exit
    fi


    time=$1
    export time


    for i in $(find $2 -type d -name VIDEO_TS) ; do

            for title in $($PathToHandBrakeCLI -t 0 -i $i -L 2>&1 | grep "has length" | sed 's/sec//' | sed 's/[()]//g' | awk '$8 > (60 * ENVIRON["time"]) { print $3 "-" $5 }   ') ; do

	#this names the filename after it's parent folder
	filename=$(echo $i | sed 's/[/]/ /g')

       #this names the title for the output file
       titlenum=$(echo $title | cut -f 2 -d '-')
           
       # you can change the preset or any other variables here
            $PathToHandBrakeCLI -i $i -o /home/snorre/Videos/${filename}-title${titlenum}-iPhone.mp4 -Z "iPhone / iPod Touch" -t ${titlenum}
       
       #output example:  24_SEASON6_D1-title1-iPhone.mp4
       
            done

    done

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Sat Sep 06, 2008 10:54 am
by SnorreSelmer
OK, so I'm dumb AND blind... :lol: I've been confusing sed and awk a bit..

I've been looking so hard at the inner for-loop that I've read awk and thought sed...

The answer to my variable is so painfully obvious and simple:

Code: Select all

filename=$(echo $i | sed 's/[/]/ /g' | awk '{print $2}')
:)

Now to make the script super-awesome I should find a way to do awk '{print $length-1}' so that you could (in theory) run it against / to encode EVERY DVD on your system... ;)

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Sat Sep 06, 2008 11:36 am
by SnorreSelmer
That wasn't too hard...
A bit of googling and found that what I was looking for was awk '{print $(NF-1)}'.
NF reports the number of fields in the buffer that AWK is working with, and (NF-1) gives the second last field.
For this to work properly, the folders containing the VIDEO_TS folder should be named in a manner that make them unique to prevent overwriting (or you could mod the script to save the output in the same folder as the video-source. It's not an option in my system, so I force all output to my home Videos-folder).

So finally I have a script that can do deep-scan DVD-encoding!

Here it is:

Code: Select all

    #!/bin/sh

    # usage is ./thisScript.sh minimumDuration SomeDirContainingVIDEO_TS
    # duration is set in minutes ("30" equals 30 minutes)
    # specify the container directory for the VIDEO_TS directory
    # example ./handbrake-longer-than.sh 30 24_SEASON6_D1

    #set default path to HandBrakeCLI
    PathToHandBrakeCLI=/home/snorre/hb/trunk/HandBrakeCLI


    if [ "${1}x" = "x" ]; then
            echo "Minutes not provided"
            exit
    fi

    if [ "${2}x" = "x" ]; then
            echo "VIDEO_TS path not provided"
            exit
    fi


    time=$1
    export time


    for i in $(find $2 -type d -name VIDEO_TS) ; do

            for title in $($PathToHandBrakeCLI -t 0 -i $i -L 2>&1 | grep "has length" | sed 's/sec//' | sed 's/[()]//g' | awk '$8 > (60 * ENVIRON["time"]) { print $3 "-" $5 }   ') ; do

	#this names the filenameafter it's parent folder
	filename=$(echo $i | sed 's/[/]/ /g' | awk '{print $(NF-1)}')

       #this names the title for the output file
       titlenum=$(echo $title | cut -f 2 -d '-')
           
       # you can change the preset or any other variables here
            $PathToHandBrakeCLI -i $i -o /home/snorre/Videos/${filename}-title${titlenum}-iPhone.mp4 -Z "iPhone / iPod Touch" -t ${titlenum}
       
       #output example:  24_SEASON6_D1-title1-iPhone.mp4
       
            done

    done
Thanks a lot to ronnor for getting me started on this. :) Now we could just use a few command-line options for maximum length, minimum length and input folder (and maybe even output folder).
Maybe something like:
-s "shorter than x minutes"
-l "longer than x minutes"
-i "input-folder"
-o "output-folder"

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Sat Sep 06, 2008 6:42 pm
by trondmm
SnorreSelmer wrote: Thanks a lot to ronnor for getting me started on this. :) Now we could just use a few command-line options for maximum length, minimum length and input folder (and maybe even output folder).
Maybe something like:
-s "shorter than x minutes"
-l "longer than x minutes"
-i "input-folder"
-o "output-folder"
Actually, the "shorter than" and "longer than" setting shouldn't be necessary. I've been working on parsing the output from the "-t 0" option with perl, and I have something that seems to work now. I probably have to test it on a few more DVDs to make sure it works every time, though.

Anyway - as far as I understand, the shorter/longer than option is desired as a way to pick individual episodes from a TV-series. But, since most TV DVDs include a play all option, you can actually use the longest title to find the individual episodes.

Have look at these titles and chapters from a DVD:
+ title 1:
+ chapters:
+ 1: cells 0->0, 36725 blocks, duration 00:01:28
+ 2: cells 1->1, 239064 blocks, duration 00:11:21
+ 3: cells 2->2, 171542 blocks, duration 00:08:46
+ 4: cells 3->3, 196490 blocks, duration 00:09:42
+ 5: cells 4->4, 257910 blocks, duration 00:12:55
+ 6: cells 5->5, 10099 blocks, duration 00:00:46

+ 7: cells 6->6, 178 blocks, duration 00:00:01
+ title 2:
+ chapters:
+ 1: cells 0->0, 37217 blocks, duration 00:01:28
+ 2: cells 1->1, 130575 blocks, duration 00:06:13
+ 3: cells 2->2, 124021 blocks, duration 00:06:18
+ 4: cells 3->3, 188030 blocks, duration 00:09:17
+ 5: cells 4->4, 134835 blocks, duration 00:06:47
+ 6: cells 5->5, 288224 blocks, duration 00:14:19
+ 7: cells 6->6, 10198 blocks, duration 00:00:46

+ 8: cells 7->7, 178 blocks, duration 00:00:01
+ title 3:
+ chapters:
+ 1: cells 0->0, 36964 blocks, duration 00:01:29
+ 2: cells 1->1, 172055 blocks, duration 00:09:06
+ 3: cells 2->2, 208151 blocks, duration 00:10:00
+ 4: cells 3->3, 251951 blocks, duration 00:12:11
+ 5: cells 4->4, 241960 blocks, duration 00:11:52
+ 6: cells 5->5, 10052 blocks, duration 00:00:46

+ 7: cells 6->6, 178 blocks, duration 00:00:01
+ title 4:
+ chapters:
+ 1: cells 0->0, 36990 blocks, duration 00:01:28
+ 2: cells 1->1, 145986 blocks, duration 00:07:28
+ 3: cells 2->2, 122029 blocks, duration 00:06:26
+ 4: cells 3->3, 215773 blocks, duration 00:10:29
+ 5: cells 4->4, 386394 blocks, duration 00:18:51
+ 6: cells 5->5, 9889 blocks, duration 00:00:45
+ title 5:
+ chapters:
+ 1: cells 0->0, 36725 blocks, duration 00:01:28
+ 2: cells 1->1, 239064 blocks, duration 00:11:21
+ 3: cells 2->2, 171542 blocks, duration 00:08:46
+ 4: cells 3->3, 196490 blocks, duration 00:09:42
+ 5: cells 4->4, 257910 blocks, duration 00:12:56
+ 6: cells 5->5, 10099 blocks, duration 00:00:46

+ 7: cells 6->6, 37217 blocks, duration 00:01:28
+ 8: cells 7->7, 130575 blocks, duration 00:06:13
+ 9: cells 8->8, 124021 blocks, duration 00:06:18
+ 10: cells 9->9, 188030 blocks, duration 00:09:17
+ 11: cells 10->10, 134835 blocks, duration 00:06:47
+ 12: cells 11->11, 288224 blocks, duration 00:14:20
+ 13: cells 12->12, 10198 blocks, duration 00:00:46

+ 14: cells 13->13, 36964 blocks, duration 00:01:29
+ 15: cells 14->14, 172055 blocks, duration 00:09:06
+ 16: cells 15->15, 208151 blocks, duration 00:10:00
+ 17: cells 16->16, 251951 blocks, duration 00:12:11
+ 18: cells 17->17, 241960 blocks, duration 00:11:53
+ 19: cells 18->18, 10052 blocks, duration 00:00:46

+ 20: cells 19->19, 36990 blocks, duration 00:01:28
+ 21: cells 20->20, 145986 blocks, duration 00:07:28
+ 22: cells 21->21, 122029 blocks, duration 00:06:26
+ 23: cells 22->22, 215773 blocks, duration 00:10:29
+ 24: cells 23->23, 386394 blocks, duration 00:18:51
+ 25: cells 24->24, 9889 blocks, duration 00:00:45
The longest title is title 5, and every chapter in that title can be found in other titles as well. By looking at the block count of each chapter in the longest title, you can search through the other titles and see if you can find a title where all chapters (except perhaps the last) are included in the main title. This seems like a very good way of identifying the individual episodes and their order.

This might be a bit tricky to do in a shell script, though, which is why I made an effort to use perl to parse the output.

I'm also trying to automate cropping. Handbrake's autocropping is great for getting rid of the black bars, but I'm mostly encoding for the PSP, and I think the screen is a bit too small for 2.35:1 or 4:3 videos. So I've decided to shave a little bit off the sides or top and bottom from these videos. Not so much that the video fills the screen (that would sacrifice too much of the picture), though. Normally I aim for a 2:1 aspect ratio for the 2.35:1 movies (480x240 pixels) and 1.53:1 for the 4:3 videos (416x272 pixels).

I think I'm getting pretty close to getting a working script now. I basically have all the parts I need, I just need to put it all together.

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Sun Sep 07, 2008 11:36 am
by SnorreSelmer
trondmm: No need for the perl parsing (for me atleast).. I present to the forum, my should-work-as-advertised batch-encoder script!

Code: Select all

#!/bin/sh

###############################################################################
# Shellscript : hb.sh - Batch-encoder script for HandBrakeCLI
# Author      : Snorre Selmer <snorre.selmer@gmail.you-know-the-rest>
# Date        : 2008-09-07
# Requires    : HandBrakeCLI, awk, sed
###############################################################################
# Description:
# This script does recursive scanning of a given folder and encodes all titles
# that are within an optionally given minimum and maximum length, and outputs
# the resulting files into an optionally given folder.
###############################################################################
# Cudos:
# - First of all, big thanks to the HandBrake team for making the most awesome
#   MP4-encoder I've ever used.
# - Second thanks goes to the HandBrake CLI-forum (http://forum.handbrake.fr)
#   and especially ronnor for making the script I used as a base for this one.
#   I learned a lot from "disassembling" your script.
# - The SED and AWK tutorials at http://www.grymoire.com/Unix/index.html were
#   a great help in understanding ronnor's script.
# - Finally, http://www.shelldorado.com/goodcoding/cmdargs.html is where I
#   got the code for the commandline arguments. Excellent shell-script site!
###############################################################################
# Disclaimer:
# I give no guarantees that this script will work on your system. It works on
# mine (GNU/Linux Ubuntu 8.04 x86_64 with HandBrakeCLI svn1667) and that's the
# only place I've been able to test it so far. If you find a bug, let me know
# on the HandBrake forum at http://forum.handbrake.fr/viewtopic.php?f=10&t=5070
# and we can look at it.
# DO NOT E-MAIL ME BUGS! Bugs are best squashed when many can look at them,
# so use the forum!
###############################################################################
# Todo:
# - Sort out a command-line argument to enable deinterlacing
# - Sort out a command-line argument to select presets
# - Space for sale, user-input required
###############################################################################

#basic program-info stuff
name=`basename "$0"`
version='0.2'

#path to the HandBrakeCLI binary
CLIpath=/home/snorre/hb/trunk/HandBrakeCLI

#default minimum title-time (in minutes) to encode
longerthan=0

#default maximum title-time (in minutes) to encode (default = 24 hours)
shorterthan=1440

#the folder you want to scan for DVDs to encode
input=

#the folder you want to store the encoded files in (default is the folder you're in when you run the script)
output=.

#function that prints usage-instructions if the user input something wrong
Usage () {
    echo >&2 "$name - HandBrakeCLI batch-script, v$version
usage: $name -i PathToDVDs [-o PathToSaveFolder] [-l MinimumTitleTimeToEncode] [-s MaximumTitleTimeToEncode]
    -i:  path to scan for VIDEO_TS folders.
    -o:  path to where you want to store the finished encodes.
           Default: the current folder (./)
    -l:  only encode titles that are longer than this (in minutes).
           Ex: \"-i 30\" will only encode titles longer than 30 minutes.
           Can be used in conjunction with -s.
    -s:  only encode titles that are shorter than this (in minutes).
           Ex: \"-s 30\" will only encode titles shorter than 30 minutes.
           Can be used in conjunction with -l.

Example:
$name -l 30 -s 60 -i Scrubs -o /home/username/mp4

This will scan the subfolder Scrubs and encode all titles that are longer than
30 minutes and shorter than 60 minutes, and store them in /home/username/mp4.

Input- and output-paths can be either relative or absolute (ie. Scrubs, ../Scrubs or /path/to/Scrubs).
"
    exit 1
}

BadArg() {
    echo >&2 "Invalid argument.
Try \"$0 -h\" for more information"
    exit 1
}

#command-line argument parser
while [ $# -gt 0 ]
do
    case "$1" in
        -i)	input="$2"; shift;;
	-o)	output="$2"; shift;;
	-l)	longerthan="$2"; shift;;
	-s)	shorterthan="$2"; shift;;
	-h)	Usage;;
	-*)	BadArg;;
    esac
    shift
done

#test to make sure the user gave an input-path (the minimum required to successfully run the script)
if [ "${input}" = "" ]
    then echo "This script requires that you atleast give it an input-path.
Try \"$0 -h\" for more information"
    exit 1
fi

export longerthan
export shorterthan

    #scans the input-path for VIDEO_TS folders
    for i in $(find $input -type d -name VIDEO_TS) ; do

        #scans all the titles in the DVD $i, scrapes out the length in seconds and compares them to $longerthan and $shorterthan
        for title in $($CLIpath -t 0 -i $i -L 2>&1 | grep "has length" | sed 's/sec//' | sed 's/[()]//g' | awk '$8 >= (60 * ENVIRON["longerthan"]) && $8 <= (60 * (ENVIRON["shorterthan"])) { print $3 "-" $5 }   ') ; do

        #this names the filenameafter it's parent folder
        filename=$(echo $i | sed 's/[/]/ /g' | awk '{print $(NF-1)}')

        #this names the title for the output file
        titlenum=$(echo $title | cut -f 2 -d '-')

        #this executes the HandBrakeCLI 
        #you can change the preset or any other variables here
        $CLIpath -i $i -o ${output}/${filename}-title${titlenum}.mp4 -Z "iPhone / iPod Touch" -t ${titlenum}

        #output example:  24_SEASON6_D1-title1.mp4
       
            done

    done
Copy-paste the code into a file named hb.sh, make it executable and run it with the -h argument ("./hb.sh -h") to get usage-instructions.

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Wed Sep 10, 2008 2:07 am
by SnorreSelmer
Ok, v0.3 of my script is out.
I added the argument -c to scan a folder and print the duration of titles to screen.
It works in conjunction with -l and -s to limit the output.
A great feature to test what's included and excluded with the duration-restrictions you want to use.

The script:

Code: Select all

#!/bin/sh

###############################################################################
# Shellscript : hb.sh - Batch-encoder script for HandBrakeCLI
# Author      : Snorre Selmer <snorre.selmer@gmail.you-know-the-rest>
# Date        : 2008-09-10
# Requires    : HandBrakeCLI, awk, sed
###############################################################################
# Description:
# This script does recursive scanning of a given folder and encodes all titles
# that are within an optionally given minimum and maximum length, and outputs
# the resulting files into an optionally given folder.
###############################################################################
# Cudos:
# - First of all, big thanks to the HandBrake team for making the most awesome
#   MP4-encoder I've ever used.
# - Second thanks goes to the HandBrake CLI-forum (http://forum.handbrake.fr)
#   and especially ronnor for making the script I used as a base for this one.
#   I learned a lot from "disassembling" your script.
# - The SED and AWK tutorials at http://www.grymoire.com/Unix/index.html were
#   a great help in understanding ronnor's script.
# - Finally, http://www.shelldorado.com/goodcoding/cmdargs.html is where I
#   got the code for the commandline arguments. Excellent shell-script site!
###############################################################################
# Disclaimer:
# I give no guarantees that this script will work on your system. It works on
# mine (GNU/Linux Ubuntu 8.04 x86_64 with HandBrakeCLI svn1667) and that's the
# only place I've been able to test it so far. If you find a bug, let me know
# on the HandBrake forum at http://forum.handbrake.fr/viewtopic.php?f=10&t=5070
# and we can look at it.
# DO NOT E-MAIL ME BUGS! Bugs are best squashed when many can look at them,
# so use the forum!
###############################################################################
# Todo:
# - Find and eliminate the cause of the "Segmentation fault" errors
# - Sort out a command-line argument to enable deinterlacing
# - Sort out a command-line argument to select presets
# - Space for sale, user-input required
###############################################################################

#basic program-info stuff
name=`basename "$0"`
version='0.3'

#path to the HandBrakeCLI binary
CLIpath=/home/snorre/src/hb/trunk/HandBrakeCLI

#default minimum title-time (in minutes) to encode
longerthan=0

#default maximum title-time (in minutes) to encode (default = 24 hours)
shorterthan=1440

#the folder you want to scan for DVDs to encode
input=

#the folder you want to store the encoded files in (default is the folder you're in when you run the script)
output=.

#title-duration check-flag
check=0

#function that prints usage-instructions if the user input something wrong
Usage () {
    echo >&2 "$name - HandBrakeCLI batch-script, v$version
usage: $name -i PathToDVDs [-o PathToSaveFolder] [-l MinimumTitleTimeToEncode] [-s MaximumTitleTimeToEncode]
    -i:  path to scan for VIDEO_TS folders.
    -o:  path to where you want to store the finished encodes.
           Default: the current folder (./)
    -l:  only encode titles that are longer than this (in minutes).
           Ex: \"-i 30\" will only encode titles longer than 30 minutes.
           Can be used in conjunction with -s.
    -s:  only encode titles that are shorter than this (in minutes).
           Ex: \"-s 30\" will only encode titles shorter than 30 minutes.
           Can be used in conjunction with -l.
    -c:  scans the input-path, checks and prints out the length of the titles (in minutes).
           Can be used with -l and -s, but -o has no meaning.

Example:
$name -l 30 -s 60 -i Scrubs -o /home/username/mp4

This will scan the subfolder Scrubs and encode all titles that are longer than
30 minutes and shorter than 60 minutes, and store them in /home/username/mp4.

Input- and output-paths can be either relative or absolute (ie. Scrubs, ../Scrubs or /path/to/Scrubs).
"
    exit 1
}

BadArg() {
    echo >&2 "Invalid argument.
Try \"$0 -h\" for more information"
    exit 1
}

#command-line argument parser
while [ $# -gt 0 ]
do
    case "$1" in
        -i)	input="$2"; shift;;
	-o)	output="$2"; shift;;
	-l)	longerthan="$2"; shift;;
	-s)	shorterthan="$2"; shift;;
	-h)	Usage;;
	-c)	check=1;;
	-*)	BadArg;;
    esac
    shift
done

#test to make sure the user gave an input-path (the minimum required to successfully run the script)
if [ "${input}" = "" ]
    then echo "This script requires that you atleast give it an input-path.
Try \"$0 -h\" for more information"
    exit 1
fi

export longerthan
export shorterthan

    #scans the input-path for VIDEO_TS folders
    for i in $(find $input -type d -name VIDEO_TS) ; do

        #this names the filenameafter it's parent folder
        filename=$(echo $i | sed 's/[/]/ /g' | awk '{print $(NF-1)}')

	if [ $check = 0 ]
	then
        #scans all the titles in the DVD $i, scrapes out the length in seconds and compares them to $longerthan and $shorterthan
        for title in $($CLIpath -t 0 -i $i -L 2>&1 | grep "has length" | sed 's/sec//' | sed 's/[()]//g' | awk '$8 >= (60 * ENVIRON["longerthan"]) && $8 <= (60 * (ENVIRON["shorterthan"])) { print $3 "-" $5 }   ') ; do

        #this names the title for the output file
        titlenum=$(echo $title | cut -f 2 -d '-')

        #this executes the HandBrakeCLI 
        #you can change the preset or any other variables here
        $CLIpath -i $i -o ${output}/${filename}-title${titlenum}.mp4 -t ${titlenum} -Z "iPhone / iPod Touch"

        done

	fi

	if [ $check = 1 ]
	then

	#scans all the titles in the DVD $i, scrapes out the length in seconds and prints title duration (in minutes)
	echo "\n$filename"
	$CLIpath -t 0 -i $i -L 2>&1 | grep "has length" | sed 's/sec//' | sed 's/[()]//g' | awk '$8 >= (60 * ENVIRON["longerthan"]) && $8 <= (60 * (ENVIRON["shorterthan"])) { print " " $2 " " $5 " is " $8/60 " minutes long " }'

	fi
    done
...and two samples of output.

One without limitations:

Code: Select all

snorre@snorre-desktop:~$ ./hb.sh -c -i /media/Series1/Firefly/
Segmentation fault
Firefly_Disc_1 Title 1 is 83.1833 minutes long 
Firefly_Disc_1 Title 2 is 41.0833 minutes long 
Firefly_Disc_1 Title 3 is 42.15 minutes long 
Firefly_Disc_1 Title 4 is 0.433333 minutes long 
Segmentation fault
Firefly_Disc_2 Title 1 is 42.2167 minutes long 
Firefly_Disc_2 Title 2 is 41.0333 minutes long 
Firefly_Disc_2 Title 3 is 42.1833 minutes long 
Firefly_Disc_2 Title 4 is 42.2 minutes long 
Firefly_Disc_2 Title 5 is 0.433333 minutes long 
Segmentation fault
Firefly_Disc_3 Title 1 is 42.2 minutes long 
Firefly_Disc_3 Title 2 is 41.1 minutes long 
Firefly_Disc_3 Title 3 is 41.1667 minutes long 
Firefly_Disc_3 Title 4 is 41.95 minutes long 
Firefly_Disc_3 Title 5 is 0.433333 minutes long 
Segmentation fault
Firefly_Disc_4 Title 1 is 42.3 minutes long 
Firefly_Disc_4 Title 2 is 40.75 minutes long 
Firefly_Disc_4 Title 3 is 42.3 minutes long 
Firefly_Disc_4 Title 4 is 0.433333 minutes long 
Firefly_Disc_4 Title 5 is 28.6667 minutes long 
Firefly_Disc_4 Title 6 is 9.76667 minutes long 
Firefly_Disc_4 Title 7 is 1.4 minutes long 
Firefly_Disc_4 Title 8 is 1.6 minutes long 
Firefly_Disc_4 Title 9 is 4.61667 minutes long 
Firefly_Disc_4 Title 10 is 3.7 minutes long 
Firefly_Disc_4 Title 11 is 2.2 minutes long 
Firefly_Disc_4 Title 12 is 1.08333 minutes long 
Firefly_Disc_4 Title 13 is 2.7 minutes long 
Firefly_Disc_4 Title 14 is 1.28333 minutes long 
Firefly_Disc_4 Title 15 is 2.63333 minutes long 
...and one with:

Code: Select all

snorre@snorre-desktop:~$ ./hb.sh -c -i /media/Series1/Firefly/ -l 30 -s 90
Segmentation fault
Firefly_Disc_1 Title 1 is 83.1833 minutes long 
Firefly_Disc_1 Title 2 is 41.0833 minutes long 
Firefly_Disc_1 Title 3 is 42.15 minutes long 
Segmentation fault
Firefly_Disc_2 Title 1 is 42.2167 minutes long 
Firefly_Disc_2 Title 2 is 41.0333 minutes long 
Firefly_Disc_2 Title 3 is 42.1833 minutes long 
Firefly_Disc_2 Title 4 is 42.2 minutes long 
Segmentation fault
Firefly_Disc_3 Title 1 is 42.2 minutes long 
Firefly_Disc_3 Title 2 is 41.1 minutes long 
Firefly_Disc_3 Title 3 is 41.1667 minutes long 
Firefly_Disc_3 Title 4 is 41.95 minutes long 
Segmentation fault
Firefly_Disc_4 Title 1 is 42.3 minutes long 
Firefly_Disc_4 Title 2 is 40.75 minutes long 
Firefly_Disc_4 Title 3 is 42.3 minutes long 

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Thu Oct 23, 2008 7:18 pm
by SnorreSelmer
I found an issue that prevented the -c option from triggering the checking-function if it wasn't at the end of the command-line.

Here's the new code:

Code: Select all

#!/bin/sh

###############################################################################
# Shellscript : hb.sh - Batch-encoder script for HandBrakeCLI
# Author      : Snorre Selmer <snorre.selmer@gmail.you-know-the-rest>
# Date        : 2008-10-23
# Requires    : HandBrakeCLI, awk, sed
###############################################################################
# Description:
# This script does recursive scanning of a given folder and encodes all titles
# that are within an optionally given minimum and maximum length, and outputs
# the resulting files into an optionally given folder.
###############################################################################
# Cudos:
# - First of all, big thanks to the HandBrake team for making the most awesome
#   MP4-encoder I've ever used.
# - Second thanks goes to the HandBrake CLI-forum (http://forum.handbrake.fr)
#   and especially ronnor for making the script I used as a base for this one.
#   I learned a lot from "disassembling" your script.
# - The SED and AWK tutorials at http://www.grymoire.com/Unix/index.html were
#   a great help in understanding ronnor's script.
# - Finally, http://www.shelldorado.com/goodcoding/cmdargs.html is where I
#   got the code for the commandline arguments. Excellent shell-script site!
###############################################################################
# Disclaimer:
# I give no guarantees that this script will work on your system. It works on
# mine (GNU/Linux Ubuntu 8.04 x86_64 with HandBrakeCLI svn1667) and that's the
# only place I've been able to test it so far. If you find a bug, let me know
# on the HandBrake forum at http://forum.handbrake.fr/viewtopic.php?f=10&t=5070
# and we can look at it.
# DO NOT E-MAIL ME BUGS! Bugs are best squashed when many can look at them,
# so use the forum!
###############################################################################
# Todo:
# - Find and eliminate the cause of the "Segmentation fault" errors
# - Sort out a command-line argument to enable deinterlacing
# - Sort out a command-line argument to select presets
# - Space for sale, user-input required
###############################################################################

#basic program-info stuff
name=`basename "$0"`
version='0.4'

#path to the HandBrakeCLI binary
CLIpath=/home/snorre/.src/hb/trunk/HandBrakeCLI

#default minimum title-time (in minutes) to encode
longerthan=0

#default maximum title-time (in minutes) to encode (default = 24 hours)
shorterthan=1440

#the folder you want to scan for DVDs to encode
input=

#the folder you want to store the encoded files in (default is the folder you're in when you run the script)
output=.

#title-duration check-flag
check="n"

#function that prints usage-instructions if the user input something wrong
Usage () {
	echo >&2 "$name - HandBrakeCLI batch-script, v$version
usage: $name -i PathToDVDs [-o PathToSaveFolder] [-l MinimumTitleTimeToEncode] [-s MaximumTitleTimeToEncode]
    -i:  path to scan for VIDEO_TS folders.
    -o:  path to where you want to store the finished encodes.
           Default: the current folder (./)
    -l:  only encode titles that are longer than this (in minutes).
           Ex: \"-i 30\" will only encode titles longer than 30 minutes.
           Can be used in conjunction with -s.
    -s:  only encode titles that are shorter than this (in minutes).
           Ex: \"-s 30\" will only encode titles shorter than 30 minutes.
           Can be used in conjunction with -l.
    -c:  scans the input-path, checks and prints out the length of the titles (in minutes).
           Can be used with -l and -s, but -o has no meaning.

Example:
$name -l 30 -s 60 -i Scrubs -o /home/username/mp4

This will scan the subfolder Scrubs and encode all titles that are longer than
30 minutes and shorter than 60 minutes, and store them in /home/username/mp4.

Input- and output-paths can be either relative or absolute (ie. Scrubs, ../Scrubs or /path/to/Scrubs).
"
	exit 0
}

BadArg() {
	echo >&2 "Invalid argument.
Try \"$0 -h\" for more information"
	exit 0
}

#command-line argument parser
while [ $# -gt 0 ]
do
	case "$1" in
	-i)   input="$2"; shift;;
	-o)   output="$2"; shift;;
	-l)   longerthan="$2"; shift;;
	-s)   shorterthan="$2"; shift;;
	-h)   Usage;;
	-c)   check="y";;
	-*)   BadArg;;
	esac
	shift
done

#test to make sure the user gave an input-path (the minimum required to successfully run the script)
if [ "${input}" = "" ]
	then echo "This script requires that you atleast give it an input-path.
Try \"$0 -h\" for more information"
	exit 0
fi

export longerthan
export shorterthan

#scans the input-path for VIDEO_TS folders
for i in $(find $input -type d -name VIDEO_TS) ; do

#this names the filenameafter it's parent folder
	filename=$(echo $i | sed 's/[/]/ /g' | awk '{print $(NF-1)}')

	if [ $check = "y" ]
		then

#scans all the titles in the DVD $i, scrapes out the length in seconds and prints title duration (in minutes)
		echo "$filename"
		$CLIpath -t 0 -i $i -L 2>&1 | grep "has length" | sed 's/sec//' | sed 's/[()]//g' | awk '$8 >= (60 * ENVIRON["longerthan"]) && $8 <= (60 * ENVIRON["shorterthan"]) {print " " $2 " " $5 " is " $8/60 " minutes long"}'
	fi

	if [ $check = "n" ]
		then
#scans all the titles in the DVD $i, scrapes out the length in seconds and compares them to $longerthan and $shorterthan
		for title in $($CLIpath -t 0 -i $i -L 2>&1 | grep "has length" | sed 's/sec//' | sed 's/[()]//g' | awk '$8 >= (60 * ENVIRON["longerthan"]) && $8 <= (60 * (ENVIRON["shorterthan"])) { print $3 "-" $5 }   ') ; do

#this names the title for the output file
		titlenum=$(echo $title | cut -f 2 -d '-')

#this executes the HandBrakeCLI
#you can change the preset or any other variables here
		$CLIpath -i $i -o ${output}/${filename}-title${titlenum}.mp4 -t ${titlenum} -Z "iPhone / iPod Touch" --deinterlace="2"
		done
	fi
done

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Tue Dec 02, 2008 10:02 pm
by john143
Brilliant! Thanks all, This works wonders

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Thu Dec 04, 2008 1:54 am
by john143
The above script did not work on my file folders that had spaces in the names, rather than work on your script I just worked around it and made a small script that would replace spaces in the folder names with underscores. I then made a script for the backend to remove these underscores in the filename output along with the the title name that is appended (for example The_Movie-title1.m4v becomes The Movie.m4v). The second script also moves the renamed file under a newly created folder for organization sake. I like this method of keeping things so that any extra stuff like nfo files, movie art, ect.. is all under one place for the flick. ALSO note that I am a COMPLETE hack when it comes to writing scripts so I am sure these will not work for everyone, the hope is that they can help in some way. Big thanks to all for the great work on this project

Script 1

Code: Select all

#!/bin/bash
# replaces characters recursively in filenames and folder names
#####################################

if [ -z $1 ];then echo Give target directory; exit 0;fi

find "$1" -depth -name '*' | while read file ; do
        directory=$(dirname "$file")
        oldfilename=$(basename "$file")
        newfilename=$(echo "$oldfilename" | tr ' ' '_' | sed 's/_-_/-/g')
        if [ "$oldfilename" != "$newfilename" ]; then
                mv -i "$directory/$oldfilename" "$directory/$newfilename"
                echo ""$directory/$oldfilename" ---> "$directory/$newfilename""
        fi
        done
exit 0

Script 2

Code: Select all

#!/bin/bash
# Convert underscores to spaces
# makes a folder based off of the file name
# moves the new file under this new folder
#####################################


if [ -z $1 ];then echo Give target directory; exit 0;fi

find "$1" -name '*' | while read file ; do
        directory=$(dirname "$file")
        oldfilename=$(basename "$file")
        newfilename=$(echo "$oldfilename" | tr '_' ' ' | sed 's/-title[0-9]//g')
	newdirectoryname=$(echo "$newfilename" | sed 's/.m4v//g')
        if [ "$oldfilename" != "$newfilename" ]; then
                mv -i "$directory/$oldfilename" "$directory/$newfilename"
                echo ""$directory/$oldfilename" ---> "$directory/$newfilename""
        fi
		mkdir "$directory/$newdirectoryname"
		mv -i "$directory/$newfilename" "$directory/$newdirectoryname"
        done
exit 0

Re: Batch conversion using CLI via Perl or Shell script??

Posted: Tue Dec 23, 2008 4:45 am
by deepsiks
Here's the bash script I use on my Ubuntu file server. It's started at boot, checks to see if media is in the optical drive and if so mounts it, rips it, and then ejects it when complete.
The only issue I have is that the chapters are called Chapter 1, Chapter 2, and so on - instead of the actually movie chapter names. Does anyone know how I could fix this through command line?

Code: Select all

#!/bin/bash
# ------------------------------------------------------------
# Setup Environment
# ------------------------------------------------------------
PDIR=${0%`basename $0`}
LCK_FILE=`basename $0`.lck
# ------------------------------------------------------------
# Am I Running
# ------------------------------------------------------------
if [ -f "${LCK_FILE}" ]; then
  # The file exists so read the PID
  # to see if it is still running
  MYPID=`head -n 1 "${LCK_FILE}"`
  TEST_RUNNING=`ps -p ${MYPID} | grep ${MYPID}`
  if [ -z "${TEST_RUNNING}" ]; then
    # The process is not running
    # Echo current PID into lock file
    echo "Not running"
    echo $$ > "${LCK_FILE}"
  else
    echo "`basename $0` is already running [${MYPID}]"
    exit 0
  fi
else
  echo "Not running"
  echo $$ > "${LCK_FILE}"
fi
# ------------------------------------------------------------
# Do Something
# ------------------------------------------------------------
while true
do
echo
ismediaincheck=$(hal-device | grep /dev/scd0)
isi=${#ismediaincheck}
echo $isi" : Only two lines (84) means no dvd in drive."


if [ $isi -gt 85 ]
then
echo "GO! "
 date
TITLE=$(lsdvd /dev/scd0 | grep -i Disc | sed 's/Disc Title: //g')
COPYTO=$"/mnt/raid/Shared/Open/fresh/"$TITLE".m4v"
OUTPUT=$"/mnt/raid/handbraketmp/"$TITLE".m4v"
if [ -f $COPYTO ]
then 
echo
echo "RIP exists! "
 date
umount /dev/scd0
eject /dev/scd0

else
  echo "Ripping title to " $COPYTO
HandBrakeCLI -L -i /dev/scd0 -o $OUTPUT -e x264 -q 0.589999973773956 -a 1,1 -E faac,ac3 -B 160,auto -R 48,Auto -6 dpl2,auto -f mp4 -4 -X 960 -P -m -x level=30:cabac=0:ref=3:mixed-refs=1:bframes=6:weightb=1:direct=auto:no-fast-pskip=1:me=umh:subq=7:analyse=all

echo "Done ripping, moving file, umounting disc and ejecting."
mv $OUTPUT $COPYTO
umount /dev/scd0
eject /dev/scd0
fi
umount /dev/scd0
eject /dev/scd0
else
echo "NO-GO!"
 date
echo
fi
sleep 15
done
# ------------------------------------------------------------
# Cleanup
# ------------------------------------------------------------
rm -f "${LCK_FILE}"
# ------------------------------------------------------------
# Done
# ------------------------------------------------------------
exit 0