From 69491a64b6512b75340983474bcb5c3d39087998 Mon Sep 17 00:00:00 2001 From: q Date: Wed, 29 Jan 2025 13:43:09 +0200 Subject: [PATCH] run ffmpegs --- tsmark/__init__.py | 9 ++++++++- tsmark/video_annotator.py | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/tsmark/__init__.py b/tsmark/__init__.py index 15464df..aced909 100644 --- a/tsmark/__init__.py +++ b/tsmark/__init__.py @@ -2,7 +2,7 @@ import argparse from tsmark.video_annotator import Marker -VERSION = "0.6.0" +VERSION = "0.6.1" def get_options(): @@ -56,6 +56,13 @@ def get_options(): required=False, help="Print FFMPEG commands: Discard audio track", ) + parser.add_argument( + "--ffmpeg-run", + action="store_true", + default=False, + required=False, + help="Run FFMPEG commands instead of just printing them", + ) parser.add_argument("--version", action="version", version=VERSION) parser.add_argument(action="store", dest="video") diff --git a/tsmark/video_annotator.py b/tsmark/video_annotator.py index 2cd3b79..98aed1f 100755 --- a/tsmark/video_annotator.py +++ b/tsmark/video_annotator.py @@ -1,14 +1,16 @@ import os +import shlex +import subprocess import sys import time import cv2 -AUDIO_COMPRESS = "-c:a libmp3lame -ar 44100 -b:a 192k" -AUDIO_COPY = "-c:a copy" -AUDIO_DISCARD = "-an" -VIDEO_COMPRESS = "-c:v mpeg2video -q:v 3 -g 1" -VIDEO_COPY = "-c:v copy" +AUDIO_COMPRESS = "-c:a libmp3lame -ar 44100 -b:a 192k".split(" ") +AUDIO_COPY = "-c:a copy".split(" ") +AUDIO_DISCARD = "-an".split(" ") +VIDEO_COMPRESS = "-c:v mpeg2video -q:v 3 -g 1".split(" ") +VIDEO_COPY = "-c:v copy".split(" ") EXT_COMPRESS = ".mpeg" @@ -356,7 +358,7 @@ class Marker: def print_timestamps(self): if self.crop[2] is None: - cropstr = "" + cropstr = [] else: self.opts.ffmpeg_copy = False x, y = [ @@ -373,7 +375,7 @@ class Marker: if h < 0: y = y + h h = -h - cropstr = f"-filter:v crop={w}:{h}:{x}:{y} " + cropstr = ["-filter:vf", f"crop={w}:{h}:{x}:{y}"] self.stamps.sort() print("# Timestamps:") audio_str = AUDIO_COPY if self.opts.ffmpeg_copy else AUDIO_COMPRESS @@ -397,9 +399,25 @@ class Marker: to_ft = self.format_time(to_ts) from_str = str(from_ts).zfill(padlen) to_str = str(to_ts).zfill(padlen) - print( - f'ffmpeg -i "{src_name}" {cropstr}{video_str} {audio_str} -ss {from_ft} -to {to_ft} "{tgt_name}.trim.{from_str}-{to_str}{tgt_ext}"' - ) + ffmpeg_cmd = [ + "ffmpeg", + "-hide_banner", + "-i", + shlex.quote(src_name), + *cropstr, + *video_str, + *audio_str, + "-ss", + f"{from_ft}", + "-to", + f"{to_ft}", + shlex.quote(f"{tgt_name}.trim.{from_str}-{to_str}{tgt_ext}"), + ] + print(" ".join(ffmpeg_cmd)) + if self.opts.ffmpeg_run: + ffmpeg_cmd[3] = src_name + ffmpeg_cmd[-1] = f"{tgt_name}.trim.{from_str}-{to_str}{tgt_ext}" + subprocess.run(ffmpeg_cmd) def save_timestamps(self): @@ -589,5 +607,6 @@ class Marker: self.read_next = True self.video_reader.release() + cv2.destroyAllWindows() self.print_timestamps() self.save_timestamps()