Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import subprocess
- import re
- HHMMSS_regex = re.compile(r"[0-9]{2}:[0-5][0-9]:[0-5][0-9]") # 00:00:00 - 99:59:59
- MMSS_regex = re.compile(r"^[0-5][0-9]:[0-5][0-9]") # 00:00 - 59:59
- def time_to_seconds(time: str) -> int:
- h, m, s = time.split(":")
- return int(h) * 3600 + int(m) * 60 + int(s)
- def seconds_to_time(seconds: int) -> str:
- m, s = divmod(seconds, 60)
- h, m = divmod(m, 60)
- return f"{h:02d}:{m:02d}:{s:02d}"
- def main(args):
- if (len(args) < 2):
- print("[error] No file given")
- return -1
- filepath = args[1]
- path = os.path.dirname(filepath)
- filename = os.path.basename(filepath)
- filename, ext = os.path.splitext(filename)
- out_filepath = os.path.join(path, f"ffmpeg_{filename}{ext}")
- print(out_filepath)
- start = input("Enter start time (MM:SS) or leave blank to start at 00:00: ")
- if (not start): # start at 00:00:00
- start = "00:00:00"
- elif (not HHMMSS_regex.match(start) and MMSS_regex.match(start)): # if its XX:XX not XX:XX:XX, add 00: as hours
- start = f"00:{start}"
- else:
- print(f"[error] '{start}' start time is invalid") # invalid time
- return -1
- print(f"starting at '{start}'")
- end = input("Enter end time (MM:SS) or leave blank to record to end: ")
- if (not end): # start at 00:00:00
- end = "00:00:00"
- elif (not HHMMSS_regex.match(end) and MMSS_regex.match(end)): # if its XX:XX not XX:XX:XX, add 00: as hours
- end = f"00:{end}"
- else:
- print(f"[error] '{end}' end time is invalid") # invalid time
- return -1
- try:
- start_seconds = time_to_seconds(start)
- record_args = f"-ss {start}"
- if (end):
- end_seconds = time_to_seconds(end)
- diff = end_seconds - start_seconds
- if (diff <= 0):
- print(f"[error] Start time '{start}' is after end time '{end}'")
- return -1
- end_str = seconds_to_time(diff)
- record_args += f" -t {end_str}" # add record time argument
- except Exception as e:
- print("[error] Invalid time given", e)
- return -1
- cmd_args = f"ffmpeg {record_args} -i \"{filepath}\" -c:v libx264 -crf 18 -preset slow -c:a aac -movflags +faststart \"{out_filepath}\""
- subprocess.call(cmd_args)
- return 0
- if __name__ == "__main__":
- error_code = main(sys.argv)
- if (error_code < 0):
- input()
- else:
- input("done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement