Advertisement
Guest User

batch convert files in linux using handbrake

a guest
Jan 29th, 2014
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.67 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ###############################################################################
  4. #execute using bash mkvconv.sh
  5.  
  6. # Script to recursively search a directory and batch convert all files of a given
  7. # file type into another file type via HandBrake conversion.
  8. #
  9. # To run in your environment set the variables:
  10. #   hbcli - Path to your HandBrakeCLI
  11. #
  12. #   source_dir - Starting directory for recursive search
  13. #
  14. #   input_file_types - Input file types to search for
  15. #
  16. #   output_file_type  - Output file type to convert into
  17. #
  18. #
  19. # Change log:
  20. # 2014-01-27: Initial release.  Tested on ubuntu 13.10.
  21. #http://stackoverflow.com/questions/21404059/bash-script-to-select-multiple-file-formats-at-once-for-encode-process/21404530#21404530
  22. ###############################################################################
  23.  
  24. hbcli=HandBrakeCLI
  25. source_dir="/media/rt/1tera_ext/1_Video_Stuff/1 Nova and bbc/Carbon diamonds"
  26.  
  27.  
  28. input_file_types=(avi wmv flv mp4 webm mov mpg)
  29. output_file_type="mkv"
  30.  
  31. echo "# Using HandBrakeCLI at "$hbcli
  32. echo "# Using source directory " "$source_dir"
  33. echo "# Converting "$input_file_types" to "$output_file_type
  34.  
  35. # Convert from one file to another
  36. convert() {
  37.     # The beginning part, echo "" | , is really important.  Without that, HandBrake exits the while loop.
  38.     #echo "" | $hbcli -i "$1" -o "$2" --preset="Universal"; # dont use with preses things are left out
  39.     echo "" | $hbcli -i "$1" -t 1 --angle 1 -c 1 -o "$2"  -f mkv  --decomb --loose-anamorphic  --modulus 2 -e x264 -q 20 --cfr -a 1,1 -E faac,copy:ac3 -6 dpl2,auto -R Auto,Auto -B 160,0 -D 0,0 --gain 0,0 --audio-fallback ffac3 --x264-profile=high  --h264-level="4.1"  --verbose=1
  40.  
  41. }
  42. # loop over the types and convert
  43. for input_file_types in "${input_file_types[@]}"
  44. do
  45.  
  46.     # Find the files and pipe the results into the read command.  The read command properly handles spaces in directories and files names.
  47.     #find "$source_dir" -name *.$input_file_type | while read in_file
  48.     find "$source_dir" -name "*.$input_file_types" -print0 | while IFS= read -r -d $'\0' in_file
  49.     #In order to correctly handle filenames containing whitespace and newline characters, you should use null delimited output. That's what the -print0 and read -d $'\0' is for.
  50.     do
  51.             echo "Processing…"
  52.         echo ">Input  "$in_file
  53.    
  54.         # Replace the file type
  55.         out_file=$(echo $in_file|sed "s/\(.*\.\)$input_file_types/\1$output_file_type/g")
  56.         echo ">Output "$out_file
  57.    
  58.         # Convert the file
  59.         convert "$in_file" "$out_file"
  60.    
  61.         if [ $? != 0 ]
  62.             then
  63.                 echo "$in_file had problems" >> handbrake-errors.log
  64.             fi
  65.    
  66.         echo ">Finished "$out_file "\n\n"
  67.     done
  68. done
  69. echo "DONE CONVERTING FILES"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement