REMOVE abstraction of save()
This commit is contained in:
@@ -1,12 +1,28 @@
|
|||||||
import os, subprocess, datetime
|
import os, subprocess, datetime, json
|
||||||
|
|
||||||
def combine_ts_to_mp4(ts_files, output_file):
|
from rewind.paths import get_state_file
|
||||||
# FFMPEG requires a text file with the list of files to concatenate
|
|
||||||
|
STATE_FILE = get_state_file()
|
||||||
|
|
||||||
|
def save(seconds, output_file):
|
||||||
|
ts_files = STATE_FILE.get("files", [])
|
||||||
|
ts_files[-1]["duration"] = get_duration(ts_files[-1]["path"])
|
||||||
|
|
||||||
|
total_duration = 0.0
|
||||||
|
files_to_include = []
|
||||||
|
|
||||||
|
while ts_files and total_duration < seconds:
|
||||||
|
ts_file = ts_files.pop()
|
||||||
|
files_to_include.append(ts_file["path"])
|
||||||
|
total_duration += ts_file["duration"]
|
||||||
|
|
||||||
|
files_to_include.reverse()
|
||||||
with open("file_list.txt", "w") as f:
|
with open("file_list.txt", "w") as f:
|
||||||
for ts in ts_files:
|
for file_path in files_to_include:
|
||||||
f.write(f"file '{ts}'\n")
|
f.write(f"file '{file_path}'\n")
|
||||||
|
|
||||||
subprocess.run(["ffmpeg", "-y",
|
subprocess.run(["ffmpeg", "-y",
|
||||||
|
"-ss", str(max(0, total_duration - seconds)),
|
||||||
"-f", "concat", "-safe", "0", "-i",
|
"-f", "concat", "-safe", "0", "-i",
|
||||||
"file_list.txt",
|
"file_list.txt",
|
||||||
"-c", "copy",
|
"-c", "copy",
|
||||||
@@ -24,10 +40,12 @@ def clean_old_ts_files(record_dir, max_age_seconds=60*60*3):
|
|||||||
os.remove(file_path)
|
os.remove(file_path)
|
||||||
print(f"Deleted old file: {file_path}")
|
print(f"Deleted old file: {file_path}")
|
||||||
|
|
||||||
def save(record_dir, output_file_name):
|
def get_duration(file_path):
|
||||||
# Gather all ts files and combine them into a single mp4
|
result = subprocess.run(
|
||||||
ts_files = [f for f in os.listdir(record_dir) if f.endswith(".ts")]
|
["ffprobe", "-v", "error", "-show_entries",
|
||||||
ts_files.sort(key=lambda x: os.path.getmtime(os.path.join(record_dir, x)))
|
"format=duration", "-of",
|
||||||
|
"default=noprint_wrappers=1:nokey=1", file_path],
|
||||||
combine_ts_to_mp4([os.path.join(record_dir, f) for f in ts_files], output_file_name)
|
stdout=subprocess.PIPE,
|
||||||
clean_old_ts_files(record_dir)
|
stderr=subprocess.STDOUT
|
||||||
|
)
|
||||||
|
return float(result.stdout)
|
||||||
Reference in New Issue
Block a user