Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加在注释直播间路径后自动转码 #852

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ language(zh_cn/en) = zh_cn
录制空间剩余阈值(gb) = 1.0
视频分段时间(秒) = 1800
录制完成后自动转为mp4格式 = 是
录制完成后是否转移到指定目录(主播目录下增加转码完成目录) = 是
追加格式后删除原文件 = 是
生成时间字幕文件 = 否
是否录制完成后执行自定义脚本 = 否
Expand Down
89 changes: 55 additions & 34 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,29 @@ def segment_video(converts_file_path: str, segment_save_file_path: str, segment_
def converts_mp4(converts_file_path: str, is_original_delete: bool = True) -> None:
try:
if os.path.exists(converts_file_path) and os.path.getsize(converts_file_path) > 0:
file_path=converts_file_path.rsplit('.', maxsplit=1)[0] + ".mp4"
_output = subprocess.check_output([
"ffmpeg", "-i", converts_file_path,
"-c:v", "copy",
"-c:a", "copy",
"-f", "mp4", converts_file_path.rsplit('.', maxsplit=1)[0] + ".mp4",
"-f", "mp4", file_path,
], stderr=subprocess.STDOUT, startupinfo=get_startup_info(os_type))
if after_converts_to_mp4_move:
if os.path.exists(file_path):
# 获取当前文件的上一级目录
parent_dir = os.path.dirname(os.path.dirname(converts_file_path))
# 定义目标路径,即上一级目录下的"完成"文件夹
completed_folder = os.path.join(parent_dir, '完成')
# 如果"完成"文件夹不存在,则创建它
if not os.path.exists(completed_folder):
os.makedirs(completed_folder)
# 移动文件到目标路径
try:
shutil.move(file_path, completed_folder)
print(f'成功将 {file_path} 移动至 {completed_folder}')
except Exception as e:
print(f'移动失败: {e}')

if is_original_delete:
time.sleep(1)
if os.path.exists(converts_file_path):
Expand Down Expand Up @@ -382,52 +399,55 @@ def check_subprocess(record_name: str, record_url: str, ffmpeg_command: list, sa
color_obj.print_colored(f"[{record_name}]录制时已被注释,本条线程将会退出", color_obj.YELLOW)
clear_record_info(record_name, record_url)
process.terminate()
stop_time = time.strftime('%Y-%m-%d %H:%M:%S')
print("因注释开启转码")
after_do(record_name, save_file_path, save_type, script_command, stop_time)
process.wait()
return True
time.sleep(1)

return_code = process.returncode
stop_time = time.strftime('%Y-%m-%d %H:%M:%S')
if return_code == 0:
if converts_to_mp4 and save_type == 'TS':
if split_video_by_time:
file_paths = utils.get_file_paths(os.path.dirname(save_file_path))
prefix = os.path.basename(save_file_path).rsplit('_', maxsplit=1)[0]
for path in file_paths:
if prefix in path:
threading.Thread(target=converts_mp4, args=(path, delete_origin_file)).start()
else:
threading.Thread(target=converts_mp4, args=(save_file_path, delete_origin_file)).start()
print(f"\n{record_name} {stop_time} 直播录制完成\n")

if script_command:
logger.debug("开始执行脚本命令!")
if "python" in script_command:
params = [
f'--record_name "{record_name}"',
f'--save_file_path "{save_file_path}"',
f'--save_type {save_type}'
f'--split_video_by_time {split_video_by_time}',
f'--converts_to_mp4 {converts_to_mp4}',
]
else:
params = [
f'"{record_name.split(" ", maxsplit=1)[-1]}"',
f'"{save_file_path}"',
save_type,
f'split_video_by_time:{split_video_by_time}',
f'converts_to_mp4:{converts_to_mp4}'
]
script_command = script_command.strip() + ' ' + ' '.join(params)
run_script(script_command)
logger.debug("脚本命令执行结束!")

after_do(record_name, save_file_path, save_type, script_command, stop_time)
else:
color_obj.print_colored(f"\n{record_name} {stop_time} 直播录制出错,返回码: {return_code}\n", color_obj.RED)

recording.discard(record_name)
return False

def after_do(record_name, save_file_path, save_type, script_command, stop_time):
if converts_to_mp4 and save_type == 'TS':
if split_video_by_time:
file_paths = utils.get_file_paths(os.path.dirname(save_file_path))
prefix = os.path.basename(save_file_path).rsplit('_', maxsplit=1)[0]
for path in file_paths:
if prefix in path:
threading.Thread(target=converts_mp4, args=(path, delete_origin_file)).start()
else:
threading.Thread(target=converts_mp4, args=(save_file_path, delete_origin_file)).start()
print(f"\n{record_name} {stop_time} 直播录制完成\n")
if script_command:
logger.debug("开始执行脚本命令!")
if "python" in script_command:
params = [
f'--record_name "{record_name}"',
f'--save_file_path "{save_file_path}"',
f'--save_type {save_type}'
f'--split_video_by_time {split_video_by_time}',
f'--converts_to_mp4 {converts_to_mp4}',
]
else:
params = [
f'"{record_name.split(" ", maxsplit=1)[-1]}"',
f'"{save_file_path}"',
save_type,
f'split_video_by_time:{split_video_by_time}',
f'converts_to_mp4:{converts_to_mp4}'
]
script_command = script_command.strip() + ' ' + ' '.join(params)
run_script(script_command)
logger.debug("脚本命令执行结束!")

def clean_name(input_text):
cleaned_name = re.sub(rstr, "_", input_text.strip()).strip('_')
Expand Down Expand Up @@ -1607,6 +1627,7 @@ def read_config_value(config_parser: configparser.RawConfigParser, section: str,
disk_space_limit = float(read_config_value(config, '录制设置', '录制空间剩余阈值(gb)', 1.0))
split_time = str(read_config_value(config, '录制设置', '视频分段时间(秒)', 1800))
converts_to_mp4 = options.get(read_config_value(config, '录制设置', '录制完成后自动转为mp4格式', "否"), False)
after_converts_to_mp4_move = options.get(read_config_value(config, '录制设置', '录制完成后是否转移到指定目录(主播目录下增加转码完成目录)', "否"), False)
delete_origin_file = options.get(read_config_value(config, '录制设置', '追加格式后删除原文件', "否"), False)
create_time_file = options.get(read_config_value(config, '录制设置', '生成时间字幕文件', "否"), False)
is_run_script = options.get(read_config_value(config, '录制设置', '是否录制完成后执行自定义脚本', "否"), False)
Expand Down