run ffmpegs

This commit is contained in:
q
2025-01-29 13:43:09 +02:00
parent 0932a6190d
commit 69491a64b6
2 changed files with 37 additions and 11 deletions

View File

@@ -2,7 +2,7 @@ import argparse
from tsmark.video_annotator import Marker from tsmark.video_annotator import Marker
VERSION = "0.6.0" VERSION = "0.6.1"
def get_options(): def get_options():
@@ -56,6 +56,13 @@ def get_options():
required=False, required=False,
help="Print FFMPEG commands: Discard audio track", 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("--version", action="version", version=VERSION)
parser.add_argument(action="store", dest="video") parser.add_argument(action="store", dest="video")

View File

@@ -1,14 +1,16 @@
import os import os
import shlex
import subprocess
import sys import sys
import time import time
import cv2 import cv2
AUDIO_COMPRESS = "-c:a libmp3lame -ar 44100 -b:a 192k" AUDIO_COMPRESS = "-c:a libmp3lame -ar 44100 -b:a 192k".split(" ")
AUDIO_COPY = "-c:a copy" AUDIO_COPY = "-c:a copy".split(" ")
AUDIO_DISCARD = "-an" AUDIO_DISCARD = "-an".split(" ")
VIDEO_COMPRESS = "-c:v mpeg2video -q:v 3 -g 1" VIDEO_COMPRESS = "-c:v mpeg2video -q:v 3 -g 1".split(" ")
VIDEO_COPY = "-c:v copy" VIDEO_COPY = "-c:v copy".split(" ")
EXT_COMPRESS = ".mpeg" EXT_COMPRESS = ".mpeg"
@@ -356,7 +358,7 @@ class Marker:
def print_timestamps(self): def print_timestamps(self):
if self.crop[2] is None: if self.crop[2] is None:
cropstr = "" cropstr = []
else: else:
self.opts.ffmpeg_copy = False self.opts.ffmpeg_copy = False
x, y = [ x, y = [
@@ -373,7 +375,7 @@ class Marker:
if h < 0: if h < 0:
y = y + h y = y + h
h = -h h = -h
cropstr = f"-filter:v crop={w}:{h}:{x}:{y} " cropstr = ["-filter:vf", f"crop={w}:{h}:{x}:{y}"]
self.stamps.sort() self.stamps.sort()
print("# Timestamps:") print("# Timestamps:")
audio_str = AUDIO_COPY if self.opts.ffmpeg_copy else AUDIO_COMPRESS 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) to_ft = self.format_time(to_ts)
from_str = str(from_ts).zfill(padlen) from_str = str(from_ts).zfill(padlen)
to_str = str(to_ts).zfill(padlen) to_str = str(to_ts).zfill(padlen)
print( ffmpeg_cmd = [
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",
) "-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): def save_timestamps(self):
@@ -589,5 +607,6 @@ class Marker:
self.read_next = True self.read_next = True
self.video_reader.release() self.video_reader.release()
cv2.destroyAllWindows()
self.print_timestamps() self.print_timestamps()
self.save_timestamps() self.save_timestamps()