fant0men

cue_time_addup bash script

Jul 13th, 2019
158
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # This script reads from the prompt, and uses numbers in the format: 00:00:00 (mm:ss:ff).
  3. # Minutes, seconds and frames.
  4. # There are 75 frames in one second, according to the Cue sheet specification.
  5. # Each time a number is given, it's added to the total number.
  6. # The script is based around an infinite while loop, and only quits once killed or Ctrl+C is pressed.
  7.  
  8. # Initiate the global variables, minutes ($mm), seconds ($ss) and frames ($frames).
  9. # And also $t, for counting the iterations of the loop, which will be echoed as track number.
  10. mm=0
  11. ss=0
  12. ff=0
  13. t=1
  14.  
  15. echo -e "\nThis script will calculate the total time of all the times given.\n"
  16. echo -e "Type or paste a time in the mm:ss:ff format.\n"
  17.  
  18. while true; do
  19.  
  20.     # Read input.
  21.     read in
  22.  
  23.     # Add 1 to the track ($t) variable.
  24.     let t++
  25.    
  26.     # Cut the input string, and get field 1, then set the minutes ($mm) variable.
  27.     tmp=$(echo "$in" | cut -d':' -f1 | sed 's/^0//')
  28.     mm=$(( mm + tmp ))
  29.  
  30.     # Cut the input string, and get field 2, then set the seconds ($ss) variable.
  31.     tmp=$(echo "$in" | cut -d':' -f2 | sed 's/^0//')
  32.     ss=$(( ss + tmp ))
  33.  
  34.     # If seconds are greater than 60, then calculate the remainder ($rem), else set the remainder ($rem) variable to 0.
  35.     if [[ $ss -gt 60 ]]; then
  36.         rem=$(( ss % 60 ))
  37.     else
  38.         rem=0
  39.     fi
  40.  
  41.     # If seconds are equal to 60, then add 1 to the minutes ($mm) variable, and reset the seconds ($ss) variable to 0.
  42.     # If remainder ($rem) is greater than 0, then add 1 to the minutes ($mm) variable, and set the seconds ($ss) variable to $rem.
  43.     if [[ $ss -eq 60 ]]; then
  44.         let mm++
  45.         ss=0
  46.     elif [[ $rem -gt 0 ]]; then
  47.         let mm++
  48.         ss="$rem"
  49.     fi
  50.  
  51.     # Cut the input string, and get field 3, then set the frames ($ff) variable.
  52.     tmp=$(echo "$in" | cut -d':' -f3 | sed 's/^0//')
  53.     ff=$(( ff + tmp ))
  54.  
  55.     # If frames are greater than 75, then calculate the remainder ($rem), else set the remainder ($rem) variable to 0.
  56.     if [[ $ff -gt 75 ]]; then
  57.         rem=$(( ff % 75 ))
  58.     else
  59.         rem=0
  60.     fi
  61.  
  62.     # If frames are equal to 75, then add 1 to the seconds ($ss) variable, and reset the frames ($ff) variable to 0.
  63.     # If remainder ($rem) is greater than 0, then add 1 to the seconds ($ss) variable, and set the frames ($ff) variable to $rem.
  64.     if [[ $ff -eq 75 ]]; then
  65.         let ss++
  66.         ff=0
  67.     elif [[ $rem -gt 0 ]]; then
  68.         let ss++
  69.         ff="$rem"
  70.     fi
  71.  
  72.     # Unsets the temporary ($tmp) and remainder ($rem) variables for the next iteration of the while loop.
  73.     unset -v tmp rem
  74.  
  75.     # If the minutes ($mm) variable is greater than (or equal to) 100, reset it to the remainder.
  76.     if [[ $mm -ge 100 ]]; then
  77.         mm=$(( mm % 100 ))
  78.     fi
  79.  
  80.     # Prints the current total time in the mm:ss:ff format.
  81.     printf "\n*** Track %d start: %02d:%02d:%02d ***\n" $t $mm $ss $ff
  82. done
RAW Paste Data