tsahee

createTiles - create movie previews using mplayer

Apr 16th, 2011
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.15 KB | None | 0 0
  1. #/bin/bash
  2.  
  3. #default parameters
  4. tiles=19
  5. endPos=-5;
  6. startPos=5;
  7. tempDir=/tmp;
  8. oFileName=\${in}_preview.jpg;
  9. deleteIntermidiate=true;
  10. previewFile=true;
  11.  
  12. if [[ $# == 0 ]]; then
  13.     echo    "movie snapshot creator, by Tsahee Zidenberg"
  14.     echo    "usage: $0 [options] <filenames> [new options] <new filenames>"
  15.     echo    " options:"
  16.     echo    "  -b   (Begin) first shot to take [$startPos]"
  17.     echo    "  -e   (End) last shot to take [$startPos]"
  18.     echo    "           negative values count from end of file"
  19.     echo    "  -n   Number of shots [$tiles]"
  20.     echo    "  -t   Temporary dir to use [$tempDir]"
  21.     echo    "  -o   Output filename [$oFileName]"
  22.     echo    "           \\\${in} for input filename"
  23.     echo    "  -p   Preview with eye of gnome [default behaviour]"
  24.     echo    "  -NP  No preview"
  25.     echo    "  -D   Don't delete intermidiate Files"
  26.     echo    "Time is always in seconds"
  27.     echo    "requires: mplayer, ImageMagick (convert, montage)"
  28.     exit 1
  29. fi
  30.  
  31. #input variable: integer, number of secounds
  32. #ouput: time as h:mm:ss
  33. printTime(){
  34.     local hr min sec
  35.     (( hr = ($1 +0) / 3600 ))
  36.     (( min = ($1 +0 - hr*3600) / 60 ))
  37.     (( sec = ($1 +0) % 60 ))
  38.     if (( sec < 10 )); then
  39.         sec=0$sec
  40.     fi
  41.     if (( min < 10 )); then
  42.         min=0$min
  43.     fi
  44.     echo $hr:$min:$sec
  45. }
  46.  
  47. processFile() {
  48.     inputFile=$1;
  49.     echo "Processing $inputFile:"
  50.     #reading attributes from the file
  51.     #
  52.     fileAttributes=(`mplayer -identify -frames 0 -vo null -ao null $inputFile 2>/dev/null \
  53.                 | grep -e ID_LENGTH -e ID_VIDEO_WIDTH -e ID_VIDEO_HEIGHT -e ID_VIDEO_FORMAT  \
  54.                 | sort | cut -d"=" -f2 | cut -d"." -f1`)
  55.     fileLength=${fileAttributes[0]}
  56.     fileVideoFormat=${fileAttributes[1]}
  57.     fileVideoSize=${fileAttributes[2]}x${fileAttributes[3]}
  58.     inputFileDir=`dirname "$inputFile"`
  59.     inputFileDir=`cd "$inputFileDir"; pwd`
  60.  
  61.     #save directory and move to /tmp
  62.     cd "$tempDir"
  63.  
  64.     #creating the title tile
  65.     #
  66.     curTile="tmp_${inputFile}_tile_title.jpg"
  67.     rm -f $curTile
  68.     time=$(printTime $fileLength)
  69.     convert -size 200x200 xc:white -pointsize 16 -draw "text 0,30 ${inputFile}" -draw "text 0,60 \"$time\"" \
  70.                     -draw "text 0,90 \"${fileVideoSize}\"" -draw "text 0,120 \"${fileVideoFormat}\"" $curTile
  71.  
  72.     tileList[0]="$tempDir/$curTile"
  73.     tileNLabelList[0]="$tempDir/$curTile"
  74.  
  75.     #preparations for the loop
  76.     if (( endPos <= 0 )); then
  77.         (( lastPos = fileLength + endPos ))
  78.     else
  79.         (( lastPos = endPos ))
  80.     fi
  81.     (( jump = (lastPos - startPos) / (tiles-1) ))
  82.     curTileNo=1
  83.     curPos=$startPos
  84.     rm -f 00000001.jpg
  85.     while (( curTileNo <= tiles )); do
  86.         mplayer -frames 1 -ss $curPos -vo jpeg -ao null "$inputFileDir/$inputFile" -quiet >/dev/null 2>/dev/null
  87.         curTile="tmp_${inputFile}_tile_${curTileNo}.jpg"
  88.         mv -f 00000001.jpg "$curTile"
  89.         tileList[$curTileNo]="$tempDir/$curTile"
  90.         tileNLabelList[((curTileNo*3))]="-label"
  91.         tileNLabelList[((curTileNo*3+1))]="$(printTime $curPos)"
  92.         tileNLabelList[((curTileNo*3+2))]="$tempDir/$curTile"
  93.         echo " $curTileNo/$tiles"
  94.         (( curTileNo++ ))
  95.         if (( curTileNo == tiles )); then
  96.             curPos=$lastPos
  97.         else
  98.             (( curPos += jump ))
  99.         fi
  100.     done
  101.     echo "joining tiles.."
  102.  
  103.     #change back to the original directory
  104.     cd "$workingDir"
  105.  
  106.     #create the final image
  107.     outFile=`in=$inputFile;eval echo $oFileName`
  108.     rm -f "$outFile"
  109.     montage "${tileNLabelList[@]}" "$outFile"
  110.    
  111.     if [[ $previewFile == true ]]; then
  112.         eog "$outFile" &
  113.     fi
  114.  
  115.     #remove temporary files
  116.     if [[ $deleteIntermidiate == true ]]; then
  117.         rm -f "${tileList[@]}"
  118.     fi
  119. }
  120.  
  121. workingDir=`pwd`
  122.  
  123. while (( "$#" )); do
  124.     if [[ -f "$1" ]]; then
  125.         processFile "$1"
  126.         shift 1
  127.     elif [[ "$1" == "-p" ]]; then
  128.         previewFile=true;
  129.         shift 1
  130.     elif [[ "$1" == "-NP" ]]; then
  131.         previewFile=false;
  132.         shift 1
  133.     elif [[ "$1" == "-n" ]]; then
  134.         (( tiles = $2 +0 ))
  135.         shift 2
  136.     elif [[ "$1" == "-b" ]]; then
  137.         (( startPos = $2 +0 ))
  138.         shift 2;
  139.     elif [[ "$1" == "-e" ]]; then
  140.         (( endPos = $2 +0 ))
  141.         shift 2;
  142.     elif [[ "$1" == "-t" ]]; then
  143.         tmpDir="$2";
  144.         if [[ ! -d tmpDir ]]; then
  145.             echo "Not a directory: $2"
  146.             exit -1;
  147.         fi
  148.         shift 2
  149.     elif [[ "$1" == "-o" ]]; then
  150.         oFileName="$2"
  151.         shift 2
  152.     elif [[ "$1" == "-D" ]]; then
  153.         deleteIntermidiate=false;
  154.         shift 1
  155.     else
  156.         echo "bad parameter: $1"
  157.         echo "use: $0 with no parameters for help"
  158.         exit -1
  159.     fi
  160. done
Add Comment
Please, Sign In to add comment