This commit is contained in:
+43
-25
@@ -36,12 +36,6 @@ def parse_duration(value):
|
||||
return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
|
||||
|
||||
|
||||
def format_label(height, ext):
|
||||
if height:
|
||||
return f"{height}p ({ext})"
|
||||
return f"Best ({ext})"
|
||||
|
||||
|
||||
def build_quality_options(formats: List[dict]):
|
||||
heights = []
|
||||
for item in formats:
|
||||
@@ -99,6 +93,45 @@ def probe(url):
|
||||
print(json.dumps(preview), flush=True)
|
||||
|
||||
|
||||
def parse_timestamp(value: str) -> int:
|
||||
text = (value or "").strip()
|
||||
if not text:
|
||||
return 0
|
||||
parts = text.split(":")
|
||||
try:
|
||||
if len(parts) == 3:
|
||||
hours, minutes, seconds = parts
|
||||
return int(hours) * 3600 + int(minutes) * 60 + int(float(seconds))
|
||||
if len(parts) == 2:
|
||||
minutes, seconds = parts
|
||||
return int(minutes) * 60 + int(float(seconds))
|
||||
return int(float(text))
|
||||
except ValueError:
|
||||
return 0
|
||||
|
||||
|
||||
def resolve_source_file(tmpdir: str) -> str:
|
||||
files = [os.path.join(tmpdir, name) for name in os.listdir(tmpdir)]
|
||||
media_files = [path for path in files if os.path.isfile(path)]
|
||||
if not media_files:
|
||||
raise RuntimeError("yt-dlp did not produce an output file")
|
||||
return sorted(media_files)[0]
|
||||
|
||||
|
||||
def should_trim(start: str, end: str) -> bool:
|
||||
start_seconds = parse_timestamp(start)
|
||||
end_seconds = parse_timestamp(end)
|
||||
return end_seconds > start_seconds
|
||||
|
||||
|
||||
def build_ffmpeg_cmd(source_file: str, output_path: str, start: str, end: str) -> List[str]:
|
||||
cmd = ["ffmpeg", "-y"]
|
||||
if should_trim(start, end):
|
||||
cmd.extend(["-ss", start, "-to", end])
|
||||
cmd.extend(["-i", source_file, "-c", "copy", output_path])
|
||||
return cmd
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--mode", choices=["probe", "download"], default="download")
|
||||
@@ -132,25 +165,10 @@ def main():
|
||||
run(download_cmd)
|
||||
emit("downloaded", 55, "Source downloaded")
|
||||
|
||||
files = [os.path.join(tmpdir, name) for name in os.listdir(tmpdir)]
|
||||
if not files:
|
||||
raise RuntimeError("yt-dlp did not produce an output file")
|
||||
source_file = sorted(files)[0]
|
||||
|
||||
ffmpeg_cmd = [
|
||||
"ffmpeg",
|
||||
"-y",
|
||||
"-ss",
|
||||
args.start,
|
||||
"-to",
|
||||
args.end,
|
||||
"-i",
|
||||
source_file,
|
||||
"-c",
|
||||
"copy",
|
||||
args.output,
|
||||
]
|
||||
emit("cropping", 75, "Cropping requested segment")
|
||||
source_file = resolve_source_file(tmpdir)
|
||||
ffmpeg_cmd = build_ffmpeg_cmd(source_file, args.output, args.start, args.end)
|
||||
message = "Cropping requested segment" if should_trim(args.start, args.end) else "Saving downloaded media"
|
||||
emit("cropping", 75, message)
|
||||
run(ffmpeg_cmd)
|
||||
|
||||
emit("completed", 100, "Download complete", args.output)
|
||||
|
||||
Reference in New Issue
Block a user