fix(core): temp file written to cwd

Now uses temp file module to prevent race conditions.
This commit is contained in:
2026-04-27 23:13:23 +01:00
parent e32f214a03
commit d97f15233b

View File

@@ -2,6 +2,7 @@
import os
import datetime
import subprocess
import tempfile
from rewind.state import load_state, add_marker_to_state, remove_marker_from_state
from rewind.config import Config
@@ -120,16 +121,18 @@ def _get_ts_files(start_timestamp: float, end_timestamp: float) -> tuple[list[st
return selected_files, start_offset, end_offset
def _concat_ts_files(file_list: list[str], start_offset: float, end_offset: float, length: float, output_file: str) -> None:
with open("file_list.txt", "w") as f:
tmp_file = tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False)
try:
for file_path in file_list:
f.write(f"file '{file_path}'\n")
tmp_file.write(f"file '{file_path}'\n")
tmp_file.close()
cmd = ["ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-nostats", "-progress", "pipe:1"]
if start_offset > 0:
cmd += ["-ss", str(start_offset)]
if end_offset > 0:
cmd += ["-t", str(length)]
cmd += ["-f", "concat", "-safe", "0", "-i", "file_list.txt", "-c", "copy"]
cmd += ["-f", "concat", "-safe", "0", "-i", tmp_file.name, "-c", "copy"]
cmd.append(output_file)
process = subprocess.Popen(
@@ -151,10 +154,9 @@ def _concat_ts_files(file_list: list[str], start_offset: float, end_offset: floa
desc="Processing",
leave=True,
) as pbar:
for line in process.stdout:
for line in process.stdout: # type: ignore[union-attr]
line = line.strip()
if line.startswith("out_time_ms="):
out_time_ms = int(line.split("=")[1])
@@ -171,10 +173,10 @@ def _concat_ts_files(file_list: list[str], start_offset: float, end_offset: floa
break
ret = process.wait()
os.remove("file_list.txt")
if ret != 0:
raise RuntimeError("ffmpeg failed")
finally:
os.unlink(tmp_file.name)
def get_duration(file_path: str) -> float:
result = subprocess.run(