|
@@ -30,34 +30,87 @@ from utils import (
|
|
|
warn,
|
|
|
error_msg,
|
|
|
expand_dir,
|
|
|
+ mark_end_of_recording,
|
|
|
)
|
|
|
from input import get_cachefile_content, validate_cd_record_config
|
|
|
import config as const
|
|
|
|
|
|
|
|
|
-def start_cd_recording() -> int:
|
|
|
+def start_cd_recording() -> None:
|
|
|
+ cachefile_content = get_cachefile_content(const.CD_RECORD_CACHEFILE)
|
|
|
date = get_yyyy_mm_dd_date()
|
|
|
- filename = path.join(
|
|
|
- const.CD_RECORD_OUTPUT_BASEDIR,
|
|
|
- date,
|
|
|
- "{}.wav".format(date),
|
|
|
- )
|
|
|
+ cd_num = int(cachefile_content[5].strip())
|
|
|
|
|
|
- log("starting cd recording...")
|
|
|
- cmd = "ffmpeg -y {} -ar 44100 -t {} {}".format(
|
|
|
- const.CD_RECORD_FFMPEG_INPUT_ARGS,
|
|
|
- const.CD_RECORD_MAX_SECONDS,
|
|
|
- filename,
|
|
|
- )
|
|
|
- process = Popen(split(cmd))
|
|
|
- return process.pid
|
|
|
+ ensure_output_dir_exists(date)
|
|
|
+
|
|
|
+ while cachefile_content[1].strip() != "9001":
|
|
|
+ filename = path.join(
|
|
|
+ const.CD_RECORD_OUTPUT_BASEDIR,
|
|
|
+ date,
|
|
|
+ f"{date}-{cd_num:0{const.CD_RECORD_FILENAME_ZFILL}}.wav",
|
|
|
+ )
|
|
|
+
|
|
|
+ unix_milis = get_unix_milis()
|
|
|
+
|
|
|
+ log(f"starting cd #{cd_num} recording...")
|
|
|
+ cmd = "ffmpeg -y {} -ar 44100 -t {} {}".format(
|
|
|
+ const.CD_RECORD_FFMPEG_INPUT_ARGS,
|
|
|
+ const.CD_RECORD_MAX_SECONDS,
|
|
|
+ filename,
|
|
|
+ )
|
|
|
+ process = Popen(split(cmd))
|
|
|
+
|
|
|
+ cachefile = expand_dir(const.CD_RECORD_CACHEFILE)
|
|
|
+ log("updating active ffmpeg pid")
|
|
|
+ try:
|
|
|
+ with open(
|
|
|
+ cachefile, mode="w+", encoding="utf-8-sig"
|
|
|
+ ) as file_writer:
|
|
|
+ file_writer.write(cachefile_content[0].strip() + "\n")
|
|
|
+ # reset marker to 1
|
|
|
+ file_writer.write("1\n")
|
|
|
+ file_writer.write(f"{process.pid}\n")
|
|
|
+ file_writer.write(f"{unix_milis}\n")
|
|
|
+ file_writer.write(f"{unix_milis}\n")
|
|
|
+ file_writer.write(f"{cd_num}\n")
|
|
|
+ except (FileNotFoundError, PermissionError, IOError) as error:
|
|
|
+ error_msg(
|
|
|
+ "Failed to write to cachefile '{}'. Reason: {}".format(
|
|
|
+ cachefile, error
|
|
|
+ )
|
|
|
+ )
|
|
|
+ fresh_cachefile_content = get_cachefile_content(
|
|
|
+ const.CD_RECORD_CACHEFILE
|
|
|
+ )
|
|
|
+ update_cue_sheet(
|
|
|
+ fresh_cachefile_content, date, unix_milis, initial_run=True
|
|
|
+ )
|
|
|
+
|
|
|
+ _ = process.communicate()[0] # wait for subprocess to end
|
|
|
+ cachefile_content = get_cachefile_content(const.CD_RECORD_CACHEFILE)
|
|
|
+ cd_num += 1
|
|
|
+ if process.returncode not in [255, 0]:
|
|
|
+ mark_end_of_recording(cachefile_content)
|
|
|
+ error_msg(f"ffmpeg terminated with exit code {process.returncode}")
|
|
|
+
|
|
|
+
|
|
|
+def ensure_output_dir_exists(date):
|
|
|
+ cue_sheet_dir = path.join(expand_dir(const.CD_RECORD_OUTPUT_BASEDIR), date)
|
|
|
+ try:
|
|
|
+ if not path.exists(cue_sheet_dir):
|
|
|
+ mkdir(cue_sheet_dir)
|
|
|
+ except (FileNotFoundError, PermissionError, IOError) as error:
|
|
|
+ error_msg(
|
|
|
+ "Failed to create to cue sheet directory '{}'. Reason: {}".format(
|
|
|
+ cue_sheet_dir, error
|
|
|
+ )
|
|
|
+ )
|
|
|
|
|
|
|
|
|
def create_cachefile_for_marker(
|
|
|
cachefile_content: list,
|
|
|
yyyy_mm_dd: str,
|
|
|
unix_milis: int,
|
|
|
- *ffmpeg_recording_pid: int,
|
|
|
initial_run=False,
|
|
|
) -> None:
|
|
|
cachefile = expand_dir(const.CD_RECORD_CACHEFILE)
|
|
@@ -78,15 +131,19 @@ def create_cachefile_for_marker(
|
|
|
log("writing cd marker {} to cachefile...".format(marker))
|
|
|
try:
|
|
|
with open(cachefile, mode="w+", encoding="utf-8-sig") as file_writer:
|
|
|
- file_writer.write(yyyy_mm_dd + "\n")
|
|
|
- file_writer.write(str(marker) + "\n")
|
|
|
+ file_writer.write(f"{yyyy_mm_dd}\n")
|
|
|
+ file_writer.write(f"{marker}\n")
|
|
|
if initial_run:
|
|
|
- file_writer.write("{}\n".format(ffmpeg_recording_pid[0]))
|
|
|
- file_writer.write(str(unix_milis) + "\n")
|
|
|
+ file_writer.write("000\n") # fake pid, gets overriden later
|
|
|
+ file_writer.write(f"{unix_milis}\n")
|
|
|
else:
|
|
|
- file_writer.write(cachefile_content[2].strip() + "\n")
|
|
|
- file_writer.write(cachefile_content[3].strip() + "\n")
|
|
|
- file_writer.write(str(unix_milis) + "\n")
|
|
|
+ file_writer.write(f"{cachefile_content[2].strip()}\n")
|
|
|
+ file_writer.write(f"{cachefile_content[3].strip()}\n")
|
|
|
+ file_writer.write(f"{unix_milis}\n")
|
|
|
+ if initial_run:
|
|
|
+ file_writer.write("1\n")
|
|
|
+ else:
|
|
|
+ file_writer.write(f"{cachefile_content[5].strip()}\n")
|
|
|
except (FileNotFoundError, PermissionError, IOError) as error:
|
|
|
error_msg(
|
|
|
"Failed to write to cachefile '{}'. Reason: {}".format(
|
|
@@ -101,8 +158,13 @@ def update_cue_sheet(
|
|
|
cue_sheet_dir = path.join(
|
|
|
expand_dir(const.CD_RECORD_OUTPUT_BASEDIR), yyyy_mm_dd
|
|
|
)
|
|
|
- cue_sheet_path = path.join(cue_sheet_dir, "sheet.cue")
|
|
|
- wave_path = path.join(cue_sheet_dir, "{}.wav".format(yyyy_mm_dd))
|
|
|
+ # use current cachefile data for here cd_num only
|
|
|
+ fresh_cachefile_content = get_cachefile_content(const.CD_RECORD_CACHEFILE)
|
|
|
+ cd_num = (
|
|
|
+ fresh_cachefile_content[5].strip().zfill(const.CD_RECORD_FILENAME_ZFILL)
|
|
|
+ )
|
|
|
+ cue_sheet_path = path.join(cue_sheet_dir, f"sheet-{cd_num}.cue")
|
|
|
+ wave_path = path.join(cue_sheet_dir, f"{yyyy_mm_dd}-{cd_num}.wav")
|
|
|
if initial_run:
|
|
|
log("updating cue sheet...")
|
|
|
try:
|
|
@@ -111,7 +173,7 @@ def update_cue_sheet(
|
|
|
with open(
|
|
|
cue_sheet_path, mode="w+", encoding="utf-8-sig"
|
|
|
) as file_writer:
|
|
|
- file_writer.write('FILE "{}" WAVE\n'.format(wave_path))
|
|
|
+ file_writer.write(f'FILE "{wave_path}" WAVE\n')
|
|
|
file_writer.write(" TRACK 01 AUDIO\n")
|
|
|
file_writer.write(" INDEX 01 00:00:00\n")
|
|
|
except (FileNotFoundError, PermissionError, IOError) as error:
|
|
@@ -172,11 +234,9 @@ def set_cd_marker() -> None:
|
|
|
create_cachefile_for_marker(*cachefile_and_time_data)
|
|
|
update_cue_sheet(*cachefile_and_time_data)
|
|
|
else:
|
|
|
- pid = start_cd_recording()
|
|
|
- create_cachefile_for_marker(
|
|
|
- *cachefile_and_time_data, pid, initial_run=True
|
|
|
- )
|
|
|
+ create_cachefile_for_marker(*cachefile_and_time_data, initial_run=True)
|
|
|
update_cue_sheet(*cachefile_and_time_data, initial_run=True)
|
|
|
+ start_cd_recording()
|
|
|
|
|
|
|
|
|
def main() -> None:
|