Advertisement
Guest User

Generate Frames (make frames + filter)

a guest
Jun 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.48 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Still a work in progress!
  4.  
  5. #   Pseudocode:
  6.  
  7. #   Make a variable called continue, and initialize it to true
  8. #   Make folder in /Users/$USER/Desktop called ExtractedVidFrames
  9. #   Make folder in /Users/$USER/Desktop/ExtractedVidFrames called Banneker
  10. #   Make folder in /Users/$USER/Desktop/ExtractedVidFrames called JFK
  11. #   Make folder in /Users/$USER/Desktop/ExtractedVidFrames called Unknown
  12. #   Go to the folder NIH2018_Data/Video
  13. #   for s from 1 to 5:
  14. #       if continue is true:
  15. #           Enter the folder /Session + s + /
  16. #           Make folder in /Users/$USER/Desktop/ExtractedVidFrames/Banneker called /Session + s + /
  17. #           Make folder in /Users/$USER/Desktop/ExtractedVidFrames/JFK called /Session + s + /
  18. #           Make folder in /Users/$USER/Desktop/ExtractedVidFrames/Unknown called /Session + s + /
  19. #           for every file ending with the .MP4 extension
  20. #               Split the file name by "_" and store the first string in a var called name
  21. #               if name starts with "p0"
  22. #                   Make the dir /Users/$USER/Desktop/ExtractedVidFrames/Banneker/Session$s/$name if it doesn't already exist
  23. #                   count the number of directories in /Users/$USER/Desktop/ExtractedVidFrames/Banneker/Session$s/$name and store in a variable called count
  24. #                   Make a folder in /Users/$USER/Desktop/ExtractedVidFrames/Banneker/Session$s/$name called (count + 1)
  25. #               else if name starts with "p1"
  26. #                   Make the dir /Users/$USER/Desktop/ExtractedVidFrames/JFK/Session$s/$name if it doesn't already exist
  27. #                   count the number of directories in /Users/$USER/Desktop/ExtractedVidFrames/JFK/Session$s/$name and store in a variable called count
  28. #                   Make a folder in /Users/$USER/Desktop/ExtractedVidFrames/JFK/Session$s/$name called (count + 1)
  29. #               else
  30. #                   Make the dir /Users/$USER/Desktop/ExtractedVidFrames/Unknown/Session$s/$name if it doesn't already exist
  31. #                   count the number of directories in /Users/$USER/Desktop/ExtractedVidFrames/Unknown/Session$s/$name and store in a variable called count
  32. #                   Make a folder in /Users/$USER/Desktop/ExtractedVidFrames/Unknown/Session$s/$name called (count + 1)
  33. #          
  34. #               calculate video duration using ffprobe and store it in a var called duration
  35. #               create variables called startTime and durationTime
  36. #               if duration is greater than 7 minutes
  37. #                   set startTime to 2min and durationTime to 5min
  38. #               else if duration is greater than 5 minutes
  39. #                   set startTime to (duration minus 5 minutes)/2, and set durationTime to 5 min
  40. #               else
  41. #                   set startTime to 0
  42. #                   set durationTime to duration
  43. #
  44. #               use ffmpeg to generate frames, starting at startTime and lasting durationTime, cutting the height and width in half, at 3 fps, using the name "frame%05d.bmp" and stored in the current folder
  45. #               cd to the folder containing the Filtering program
  46. #               run Filtering program to filter the newly created frames (thus saving space)
  47. #               echo Done processing $fileName!
  48. #           echo Done with Session $s!
  49. #       Stay on standby until "done" is entered; if "exit" is entered, set continue to false
  50.  
  51. # Note: this script requires the following programs to be installed and aliased:
  52. # ffmpeg as ffmpeg
  53. # ffprobe as ffprobe
  54. # c# as dotnet
  55.  
  56.  
  57. pathToAllVids="/Volumes/My Passport/Dropbox (MIT)/NIH2018_Data/Video"
  58. pathToFilterPrgm="/Users/$USER/Desktop/Yaseen script files/Filtering/"
  59. pathToExtractedVids="/Volumes/My Passport/ExtractedVidFrames"
  60. continue=true
  61.  
  62. mkdir "$pathToExtractedVids"
  63. mkdir "$pathToExtractedVids"/Banneker
  64. mkdir "$pathToExtractedVids"/JFK
  65. mkdir "$pathToExtractedVids"/Unknown
  66.  
  67. cd "$pathToAllVids"
  68.  
  69. #this is needed for the fileName for-loop
  70. shopt -s nullglob
  71.  
  72. for s in `seq 2 5`;
  73. do
  74.     if [ "$continue" = true ] ; then
  75.         cd "$pathToAllVids/Session$s/"
  76.         mkdir -p "$pathToExtractedVids"/Banneker/Session$s/
  77.         mkdir -p "$pathToExtractedVids"/JFK/Session$s/
  78.         mkdir -p "$pathToExtractedVids"/Unknown/Session$s/
  79.  
  80.         for fileName in *.mp4 *.MP4; do
  81.             #split name and store into #splitName:
  82.             OIFS=$sFS
  83.             IFS='_' read -r -a splitName <<< "$fileName"
  84.             IFS=$OIFS
  85.  
  86.             #store first element into $name:
  87.             name=${splitName[0]}
  88.             frameDir=""
  89.  
  90.             if [[ $name == p0* ]]; then
  91.                 mkdir -p "$pathToExtractedVids"/Banneker/Session$s/$name/
  92.                 count=$(ls -l $pathToExtractedVids/Banneker/Session$s/$name | grep -c ^d)
  93.                 frameDir="$pathToExtractedVids/Banneker/Session$s/$name/$(($count + 1))"
  94.                 mkdir -p $frameDir
  95.             elif [[ $name == p1* ]]; then
  96.                 mkdir -p "$pathToExtractedVids"/JFK/Session$s/$name/
  97.                 count=$(ls -l $pathToExtractedVids/JFK/Session$s/$name | grep -c ^d)
  98.                 frameDir="$pathToExtractedVids/JFK/Session$s/$name/$(($count + 1))"
  99.                 mkdir -p $frameDir
  100.             else
  101.                 mkdir -p $"pathToExtractedVids"/Unknown/Session$s/$name/
  102.                 count=$(ls -l $pathToExtractedVids/Unknown/Session$s/$name | grep -c ^d)
  103.                 frameDir="$pathToExtractedVids/Unknown/Session$s/$name/$(($count + 1))"
  104.                 mkdir -p $frameDir
  105.             fi
  106.  
  107.             duration=$(ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "$pathToAllVids/Session$s/$fileName")
  108.        
  109.             startTime=0
  110.             durationTime=0
  111.             if (( $(echo "$duration > 420.0" | bc -l) )); then
  112.                 startTime=120.0
  113.                 durationTime=300.0
  114.             elif (( $(echo "$duration > 300.0" | bc -l) )); then
  115.                 startTime=$(echo "($duration - 300.0)/2" | bc -l)
  116.                 durationTime=300.0
  117.             else
  118.                 startTime=0
  119.                 durationTime=$duration
  120.             fi
  121.  
  122.             ffmpeg -ss $startTime -i "$pathToAllVids/Session$s/$fileName" -t $durationTime -vf fps=3,scale=iw/2:ih/2 "$frameDir/frame%05d.bmp" -hide_banner
  123.             cd "$pathToFilterPrgm"
  124.             dotnet FilteringTerminalArgs.dll $frameDir -no_output
  125.             echo Done processing $fileName!
  126.         done
  127.  
  128.         echo Done with all the videos in Session $s!
  129.     fi
  130. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement