From 3079169b60baccaf71678104372536d715bb4ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=88=9A=28noham=29=C2=B2?= <100566912+NohamR@users.noreply.github.com> Date: Mon, 5 Jan 2026 21:32:47 +0100 Subject: [PATCH] Add video trimmer utility script --- utils/trimmer.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 utils/trimmer.py diff --git a/utils/trimmer.py b/utils/trimmer.py new file mode 100644 index 0000000..5a5ff7b --- /dev/null +++ b/utils/trimmer.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +import argparse +import subprocess +import re + +def hhmmss_to_seconds(time_str): + """Convert hh:mm:ss to total seconds.""" + parts = list(map(int, time_str.split(':'))) + if len(parts) == 3: + h, m, s = parts + elif len(parts) == 2: + h = 0 + m, s = parts + elif len(parts) == 1: + h = 0 + m = 0 + s = parts[0] + else: + raise ValueError("Invalid time format") + return h * 3600 + m * 60 + s + +def get_video_duration(filename): + """Return video duration in seconds using ffprobe.""" + cmd = [ + "ffprobe", "-v", "error", "-show_entries", + "format=duration", "-of", + "default=noprint_wrappers=1:nokey=1", filename + ] + result = subprocess.run(cmd, capture_output=True, text=True) + return float(result.stdout.strip()) + +def trim_video(input_file, output_file, remove_start=None, remove_end=None): + # Convert times to seconds + start_sec = hhmmss_to_seconds(remove_start) if remove_start else 0 + duration = None + if remove_end: + total_duration = get_video_duration(input_file) + end_sec = hhmmss_to_seconds(remove_end) + duration = total_duration - start_sec - end_sec + if duration <= 0: + raise ValueError("Trim times are too long; resulting duration is <= 0") + + cmd = ["ffmpeg", "-y"] + if start_sec > 0: + cmd += ["-ss", str(start_sec)] + cmd += ["-i", input_file] + if duration: + cmd += ["-t", str(duration)] + cmd += ["-c", "copy", output_file] + + print("Running command:", " ".join(cmd)) + subprocess.run(cmd, check=True) + +def main(): + parser = argparse.ArgumentParser(description="Trim video start/end times") + parser.add_argument("input_file", help="Input video file") + parser.add_argument("output_file", help="Output trimmed video file") + parser.add_argument("--remove-start", help="Time to remove from start (hh:mm:ss)") + parser.add_argument("--remove-end", help="Time to remove from end (hh:mm:ss)") + args = parser.parse_args() + + trim_video(args.input_file, args.output_file, args.remove_start, args.remove_end) + +if __name__ == "__main__": + main() \ No newline at end of file