Advertisement
Frost1

ffmpeg.py

May 10th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. import os
  2. import sys
  3. import subprocess
  4. import re
  5.  
  6. HHMMSS_regex = re.compile(r"[0-9]{2}:[0-5][0-9]:[0-5][0-9]") # 00:00:00 - 99:59:59
  7. MMSS_regex = re.compile(r"^[0-5][0-9]:[0-5][0-9]") # 00:00 - 59:59
  8.  
  9. def time_to_seconds(time: str) -> int:
  10.     h, m, s = time.split(":")
  11.     return int(h) * 3600 + int(m) * 60 + int(s)
  12.  
  13. def seconds_to_time(seconds: int) -> str:
  14.     m, s = divmod(seconds, 60)
  15.     h, m = divmod(m, 60)
  16.  
  17.     return f"{h:02d}:{m:02d}:{s:02d}"
  18.  
  19. def main(args):
  20.     if (len(args) < 2):
  21.         print("[error] No file given")
  22.         return -1
  23.    
  24.     filepath = args[1]
  25.  
  26.     path = os.path.dirname(filepath)
  27.  
  28.     filename = os.path.basename(filepath)
  29.     filename, ext = os.path.splitext(filename)
  30.  
  31.     out_filepath = os.path.join(path, f"ffmpeg_{filename}{ext}")
  32.     print(out_filepath)
  33.  
  34.     start = input("Enter start time (MM:SS) or leave blank to start at 00:00: ")
  35.  
  36.     if (not start): # start at 00:00:00
  37.         start = "00:00:00"
  38.     elif (not HHMMSS_regex.match(start) and MMSS_regex.match(start)): # if its XX:XX not XX:XX:XX, add 00: as hours
  39.         start = f"00:{start}"
  40.     else:
  41.         print(f"[error] '{start}' start time is invalid") # invalid time
  42.         return -1
  43.  
  44.     print(f"starting at '{start}'")
  45.  
  46.     end = input("Enter end time (MM:SS) or leave blank to record to end: ")
  47.  
  48.     if (not end): # start at 00:00:00
  49.         end = "00:00:00"
  50.     elif (not HHMMSS_regex.match(end) and MMSS_regex.match(end)): # if its XX:XX not XX:XX:XX, add 00: as hours
  51.         end = f"00:{end}"
  52.     else:
  53.         print(f"[error] '{end}' end time is invalid") # invalid time
  54.         return -1
  55.  
  56.     try:
  57.         start_seconds = time_to_seconds(start)
  58.  
  59.         record_args = f"-ss {start}"
  60.  
  61.         if (end):
  62.             end_seconds = time_to_seconds(end)
  63.             diff = end_seconds - start_seconds
  64.  
  65.             if (diff <= 0):
  66.                 print(f"[error] Start time '{start}' is after end time '{end}'")
  67.                 return -1
  68.  
  69.             end_str = seconds_to_time(diff)
  70.             record_args += f" -t {end_str}" # add record time argument
  71.     except Exception as e:
  72.         print("[error] Invalid time given", e)
  73.         return -1
  74.  
  75.     cmd_args = f"ffmpeg {record_args} -i \"{filepath}\" -c:v libx264 -crf 18 -preset slow -c:a aac -movflags +faststart \"{out_filepath}\""
  76.    
  77.     subprocess.call(cmd_args)
  78.  
  79.     return 0
  80.  
  81. if __name__ == "__main__":
  82.     error_code = main(sys.argv)
  83.  
  84.     if (error_code < 0):
  85.         input()
  86.     else:
  87.         input("done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement