(action_name, source_path, segments)
| 20567 | } |
| 20568 | |
| 20569 | def _build_segment_concat(action_name, source_path, segments): |
| 20570 | if not segments: |
| 20571 | raise ValueError("Nessun segmento valido per la concatenazione") |
| 20572 | ffmpeg_bin_local = _resolve_ffmpeg_bin() |
| 20573 | if not ffmpeg_bin_local: |
| 20574 | raise RuntimeError("ffmpeg non disponibile") |
| 20575 | seg_files = [] |
| 20576 | for idx, (s, e) in enumerate(segments): |
| 20577 | seg_path = media_dir / f"{job_id}_{action_name}_seg_{idx:02d}.mp4" |
| 20578 | proc_seg = _run_ffmpeg([ |
| 20579 | ffmpeg_bin_local, |
| 20580 | "-y", |
| 20581 | "-ss", f"{s}", |
| 20582 | "-to", f"{e}", |
| 20583 | "-i", str(source_path), |
| 20584 | "-c:v", "libx264", |
| 20585 | "-preset", "veryfast", |
| 20586 | "-crf", "20", |
| 20587 | "-c:a", "aac", |
| 20588 | "-b:a", "160k", |
| 20589 | str(seg_path) |
| 20590 | ], timeout=240) |
| 20591 | if proc_seg.returncode != 0 or not seg_path.exists(): |
| 20592 | raise RuntimeError(f"Segmento {idx+1} fallito ({s}-{e})") |
| 20593 | seg_files.append(seg_path) |
| 20594 | |
| 20595 | list_path = media_dir / f"{job_id}_{action_name}_concat_list.txt" |
| 20596 | with open(list_path, "w", encoding="utf-8") as fh: |
| 20597 | for seg in seg_files: |
| 20598 | escaped_seg = str(seg).replace("'", "'\\''") |
| 20599 | fh.write(f"file '{escaped_seg}'\n") |
| 20600 | out_path = media_dir / f"{job_id}_{action_name}_concat.mp4" |
| 20601 | proc = _run_ffmpeg([ |
| 20602 | ffmpeg_bin_local, |
| 20603 | "-y", |
| 20604 | "-f", "concat", |
| 20605 | "-safe", "0", |
| 20606 | "-i", str(list_path), |
| 20607 | "-c:v", "copy", |
| 20608 | "-c:a", "aac", |
| 20609 | str(out_path) |
| 20610 | ], timeout=360) |
| 20611 | if proc.returncode != 0 or not out_path.exists(): |
| 20612 | proc2 = _run_ffmpeg([ |
| 20613 | ffmpeg_bin_local, |
| 20614 | "-y", |
| 20615 | "-f", "concat", |
| 20616 | "-safe", "0", |
| 20617 | "-i", str(list_path), |
| 20618 | "-c:v", "libx264", |
| 20619 | "-preset", "veryfast", |
| 20620 | "-crf", "20", |
| 20621 | "-c:a", "aac", |
| 20622 | "-b:a", "160k", |
| 20623 | str(out_path) |
| 20624 | ], timeout=360) |
| 20625 | if proc2.returncode != 0 or not out_path.exists(): |
| 20626 | raise RuntimeError("Concat fallita") |
no test coverage detected