Advertisement
steftru

Chromecast batch conversion script

Apr 15th, 2014
2,935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.37 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. # Batch Convert Script by Stefano
  4. # The Purpose of this Script is to batch convert any video file to mp4 or mkv format for chromecast compatibility
  5. # this script only convert necessary tracks if the video is already
  6. # in H.264 format it won't convert it saving your time!
  7.  
  8. # Put all video files need to be converted in a folder!
  9. # the name of files must not have " " Space!
  10. # Rename the File if contain space
  11.  
  12. # Variable used:
  13. # outmode should be mp4 or mkv
  14. # sourcedir is the directory where to be converted videos are
  15. # indir is the directory where converted video will be created
  16.  
  17.  
  18. # usage:
  19. #########################
  20. #########################
  21. # cast.sh mp4 /home/user/divx /home/user/chromecastvideos
  22. # or
  23. # cast.sh mkv /home/user/divx /home/user/chromecastvideos
  24. #########################
  25. #########################
  26.  
  27. # working mode
  28. outmode=$1
  29. # check output mode
  30. if [[ $outmode ]]; then
  31. if [ $outmode = "mp4" ] || [ $outmode = "mkv" ]
  32.     then
  33.     echo "WORKING MODE $outmode"
  34.     else
  35.     echo "$outmode is NOT a Correct target format. You need to set an output format! like cast.sh mp4 xxxx or cast.sh mkv xxxx"
  36.     exit
  37. fi
  38. else
  39. echo "Working mode is missing. You should set a correct target format like mp4 or mkv"
  40. exit
  41. fi
  42.  
  43. # Source dir
  44. sourcedir=$2
  45. if [[ $sourcedir ]]; then
  46.      echo "Using $sourcedir as Input Folder"
  47.     else
  48.      echo "Error: Check if you have set an input folder"
  49.      exit
  50. fi
  51.  
  52. # Target dir
  53. indir=$3
  54. if mkdir -p $indir/castable
  55.     then
  56.      echo "Using $indir/castable as Output Folder"
  57.     else
  58.      echo "Error: Check if you have the rights to write in $indir"
  59.      exit
  60. fi
  61.  
  62. # set format
  63. if [ $outmode=mp4 ]
  64.     then
  65.      outformat=mp4
  66.     else
  67.      outformat=matroska
  68. fi
  69.  
  70. # Check FFMPEG Installation
  71. if ffmpeg -formats > /dev/null 2>&1
  72.     then
  73.      ffversion=`ffmpeg -version 2> /dev/null | grep ffmpeg | sed -n 's/ffmpeg\s//p'`
  74.      echo "Your ffmpeg verson is $ffversion"
  75.     else
  76.      echo "\nERROR:\nYou need ffmpeg installed with x264 and an aac encoder"
  77.      exit
  78. fi
  79.  
  80. if ffmpeg -formats 2> /dev/null | grep "E mp4" > /dev/null
  81.     then
  82.      echo "Check mp4 container format ... OK"
  83.     else
  84.      echo "Check mp4 container format ... NOK"
  85.      exit
  86. fi
  87.  
  88. if ffmpeg -formats 2> /dev/null | grep "E matroska" > /dev/null
  89.         then
  90.          echo "Check mkv container format ... OK"
  91.         else
  92.          echo "Check mkv container format ... NOK"
  93.          exit
  94. fi
  95.  
  96. if ffmpeg -codecs 2> /dev/null | grep "libfdk_aac" > /dev/null
  97.         then
  98.          echo "Check AAC Audio Encoder ... OK"
  99.         else
  100.          echo "Check AAC Audio Encoder ... NOK"
  101.          exit
  102. fi
  103.  
  104. if ffmpeg -codecs 2> /dev/null | grep "libx264" > /dev/null
  105.         then
  106.          echo "Check x264 the free H.264 Video Encoder ... OK"
  107.         else
  108.          echo "Check x264 the free H.264 Video Encoder ... NOK"
  109.          exit
  110. fi
  111.  
  112. echo "Your FFMpeg is OK\nEntering File Processing\n\n"
  113.  
  114. ################################################################
  115. cd "$sourcedir"
  116. for filelist in `ls`
  117. do
  118.     if ffmpeg -i $filelist 2>&1 | grep 'Invalid data found'     #check if it's video file
  119.        then
  120.        echo "ERROR File $filelist is NOT A VIDEO FILE can be converted!"
  121.        exit    
  122.    
  123.     fi
  124.  
  125.     if ffmpeg -i $filelist 2>&1 | grep Video: | grep h264       #check video codec
  126.        then
  127.         vcodec=copy
  128.        else
  129.         vcodec=libx264
  130.     fi
  131.  
  132.     if ffmpeg -i $filelist 2>&1 | grep Video: | grep "High 10"  #10 bit H.264 can't be played by Hardware.
  133.        then
  134.         vcodec=libx264
  135.     fi
  136.  
  137.     if [ ffmpeg -i $filelist 2>&1 | grep Audio: | grep aac ] || [   ffmpeg -i $filelist 2>&1 | grep Audio: | grep mp3 ] #check audio codec
  138.        then
  139.         acodec=copy
  140.        else
  141.         acodec=libfdk_aac
  142.     fi
  143.  
  144.  
  145.     echo "\n\nUseing video codec: $vcodec audio codec: $acodec and Container format $outformat for\nFile: $filelist\n\n\n\nStarting Converting\n"
  146.  
  147. # using ffmpeg for real converting
  148.     echo "ffmpeg -i $filelist -y -f $outformat -acodec $acodec -ab 192k -ac 2 -absf aac_adtstoasc -async 1 -vcodec $vcodec -vsync 0 -profile:v main -level 3.1 -qmax 22 -qmin 20 -x264opts no-cabac:ref=2 -threads 0 $indir/castable/$filelist.$outmode"
  149.     ffmpeg -i $filelist -y -f $outformat -acodec $acodec -ab 192k -ac 2 -absf aac_adtstoasc -async 1 -vcodec $vcodec -vsync 0 -profile:v main -level 3.1 -qmax 22 -qmin 20 -x264opts no-cabac:ref=2 -threads 0 $indir/castable/$filelist.$outmode
  150.  
  151.    
  152. done
  153.     echo ALL Processed!
  154.  
  155.  
  156.  
  157. ###################
  158. echo "DONE"
  159. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement