Improve ffprobe executions

When running ffprobe, use `subprocess.run` rather than
`subprocess.Popen`.  This simplifies the handling that is needed to run
and process the outputs.  Here, filename parsing is also simplified by
explicitly removing the file extension with `os.path.splitext` and
forcing a single split into the camera name and the formatted date.
This commit is contained in:
Sean Vig
2021-05-21 23:35:25 -04:00
committed by Blake Blackshear
parent ebb6d348a3
commit a4e6d9ed9a
3 changed files with 48 additions and 62 deletions

View File

@@ -66,28 +66,23 @@ class RecordingMaintainer(threading.Thread):
if f in files_in_use:
continue
camera = "-".join(f.split("-")[:-1])
start_time = datetime.datetime.strptime(
f.split("-")[-1].split(".")[0], "%Y%m%d%H%M%S"
)
basename = os.path.splitext(f)[0]
camera, date = basename.rsplit("-", maxsplit=1)
start_time = datetime.datetime.strptime(date, "%Y%m%d%H%M%S")
ffprobe_cmd = " ".join(
[
"ffprobe",
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
f"{os.path.join(RECORD_DIR,f)}",
]
)
p = sp.Popen(ffprobe_cmd, stdout=sp.PIPE, shell=True)
(output, err) = p.communicate()
p_status = p.wait()
if p_status == 0:
duration = float(output.decode("utf-8").strip())
ffprobe_cmd = [
"ffprobe",
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
f"{os.path.join(RECORD_DIR, f)}",
]
p = sp.run(ffprobe_cmd, capture_output=True)
if p.returncode == 0:
duration = float(p.stdout.decode().strip())
else:
logger.info(f"bad file: {f}")
os.remove(os.path.join(RECORD_DIR, f))