fix(core): temp file written to cwd
Now uses temp file module to prevent race conditions.
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
import os
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import tempfile
|
||||||
|
|
||||||
from rewind.state import load_state, add_marker_to_state, remove_marker_from_state
|
from rewind.state import load_state, add_marker_to_state, remove_marker_from_state
|
||||||
from rewind.config import Config
|
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
|
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:
|
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:
|
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"]
|
cmd = ["ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-nostats", "-progress", "pipe:1"]
|
||||||
if start_offset > 0:
|
if start_offset > 0:
|
||||||
cmd += ["-ss", str(start_offset)]
|
cmd += ["-ss", str(start_offset)]
|
||||||
if end_offset > 0:
|
if end_offset > 0:
|
||||||
cmd += ["-t", str(length)]
|
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)
|
cmd.append(output_file)
|
||||||
|
|
||||||
process = subprocess.Popen(
|
process = subprocess.Popen(
|
||||||
@@ -151,10 +154,9 @@ def _concat_ts_files(file_list: list[str], start_offset: float, end_offset: floa
|
|||||||
desc="Processing",
|
desc="Processing",
|
||||||
leave=True,
|
leave=True,
|
||||||
) as pbar:
|
) as pbar:
|
||||||
for line in process.stdout:
|
for line in process.stdout: # type: ignore[union-attr]
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
|
|
||||||
|
|
||||||
if line.startswith("out_time_ms="):
|
if line.startswith("out_time_ms="):
|
||||||
out_time_ms = int(line.split("=")[1])
|
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
|
break
|
||||||
|
|
||||||
ret = process.wait()
|
ret = process.wait()
|
||||||
os.remove("file_list.txt")
|
|
||||||
|
|
||||||
if ret != 0:
|
if ret != 0:
|
||||||
raise RuntimeError("ffmpeg failed")
|
raise RuntimeError("ffmpeg failed")
|
||||||
|
finally:
|
||||||
|
os.unlink(tmp_file.name)
|
||||||
|
|
||||||
def get_duration(file_path: str) -> float:
|
def get_duration(file_path: str) -> float:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
|
|||||||
Reference in New Issue
Block a user