Skip to content

Commit a714bee

Browse files
authored
refactor: revamp the process logic (#193)
fix #189 #188
1 parent 3563d75 commit a714bee

File tree

15 files changed

+228
-239
lines changed

15 files changed

+228
-239
lines changed

src/autoslice/slice_video.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Copyright (c) 2024 bilive.
22

3+
import base64
34
import subprocess
4-
from src.autoslice.calculate_density import extract_dialogues, calculate_density, format_time
55
from src.config import Your_API_KEY, SLICE_DURATION
6-
import base64
76
from zhipuai import ZhipuAI
7+
from src.autoslice.calculate_density import extract_dialogues, calculate_density, format_time
8+
from src.log.logger import scan_log
89

910
def zhipu_glm_4v_plus_generate_title(video_path, artist):
1011
with open(video_path, 'rb') as video_file:
@@ -44,7 +45,13 @@ def inject_metadata(video_path, generate_title, output_path):
4445
'-c:a', 'copy',
4546
output_path
4647
]
47-
subprocess.run(command)
48+
try:
49+
result = subprocess.run(command, check=True, capture_output=True, text=True)
50+
scan_log.debug(f"FFmpeg output: {result.stdout}")
51+
if result.stderr:
52+
scan_log.debug(f"FFmpeg debug: {result.stderr}")
53+
except subprocess.CalledProcessError as e:
54+
scan_log.error(f"Error: {e.stderr}")
4855

4956
def slice_video(video_path, start_time, output_path, duration=f'00:00:{SLICE_DURATION}'):
5057
"""Slice the video using ffmpeg."""
@@ -58,4 +65,10 @@ def slice_video(video_path, start_time, output_path, duration=f'00:00:{SLICE_DUR
5865
'-c:a', 'copy',
5966
output_path
6067
]
61-
subprocess.run(command)
68+
try:
69+
result = subprocess.run(command, check=True, capture_output=True, text=True)
70+
scan_log.debug(f"FFmpeg output: {result.stdout}")
71+
if result.stderr:
72+
scan_log.debug(f"FFmpeg debug: {result.stderr}")
73+
except subprocess.CalledProcessError as e:
74+
scan_log.error(f"Error: {e.stderr}")

src/burn/only_render.py

Lines changed: 0 additions & 120 deletions
This file was deleted.

src/burn/render_command.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) 2024 bilive.
2+
3+
import os
4+
import subprocess
5+
from src.config import GPU_EXIST
6+
from src.log.logger import scan_log
7+
8+
def render_command(in_video_path, out_video_path, in_subtitle_font_size, in_subtitle_margin_v):
9+
"""Burn the danmakus and subtitles into the videos
10+
Args:
11+
in_video_path: str, the path of video
12+
out_video_path: str, the path of rendered video
13+
in_subtitle_font_size: str, the font size of subtitles
14+
in_subtitle_margin_v: str, the bottom margin of subtitles
15+
"""
16+
in_ass_path = in_video_path[:-4] + '.ass'
17+
if GPU_EXIST:
18+
in_srt_path = in_video_path[:-4] + '.srt'
19+
if os.path.isfile(in_ass_path):
20+
scan_log.info("Current Mode: GPU with danmaku")
21+
command = [
22+
'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path,
23+
'-c:v', 'h264_nvenc', '-vf', f"subtitles={in_srt_path}:force_style='Fontsize={in_subtitle_font_size},MarginV={in_subtitle_margin_v}',subtitles={in_ass_path}", out_video_path
24+
]
25+
try:
26+
result = subprocess.run(command, check=True, capture_output=True, text=True)
27+
scan_log.debug(f"FFmpeg output: {result.stdout}")
28+
if result.stderr:
29+
scan_log.debug(f"FFmpeg debug: {result.stderr}")
30+
except subprocess.CalledProcessError as e:
31+
scan_log.error(f"Error: {e.stderr}")
32+
33+
else:
34+
scan_log.info("Current Mode: GPU without danmaku")
35+
command_no_danmaku = [
36+
'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path,
37+
'-c:v', 'h264_nvenc', '-vf', f"subtitles={in_srt_path}:force_style='Fontsize={in_subtitle_font_size},MarginV={in_subtitle_margin_v}'", out_video_path
38+
]
39+
try:
40+
result = subprocess.run(command_no_danmaku, check=True, capture_output=True, text=True)
41+
scan_log.debug(f"FFmpeg output: {result.stdout}")
42+
if result.stderr:
43+
scan_log.debug(f"FFmpeg debug: {result.stderr}")
44+
except subprocess.CalledProcessError as e:
45+
scan_log.error(f"Error: {e.stderr}")
46+
else:
47+
if os.path.isfile(in_ass_path):
48+
scan_log.info("Current Mode: CPU with danmaku")
49+
command_without_gpu = [
50+
'ffmpeg', '-y', '-i', in_video_path, '-vf', f'ass={in_ass_path}', '-preset', 'ultrafast', out_video_path
51+
]
52+
try:
53+
result = subprocess.run(command_without_gpu, check=True, capture_output=True, text=True)
54+
scan_log.debug(f"FFmpeg output: {result.stdout}")
55+
if result.stderr:
56+
scan_log.debug(f"FFmpeg debug: {result.stderr}")
57+
except subprocess.CalledProcessError as e:
58+
scan_log.error(f"Error: {e.stderr}")
59+
else:
60+
scan_log.info("Current Mode: CPU without danmaku")
61+
subprocess.run(['mv', in_video_path, out_video_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

src/burn/render_queue.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright (c) 2024 bilive.
2+
3+
import queue
4+
import time
5+
from src.subtitle.generate_subtitles import generate_subtitles
6+
from src.burn.render_video import render_video
7+
from src.log.logger import scan_log
8+
9+
class VideoRenderQueue:
10+
def __init__(self):
11+
self.render_queue = queue.Queue()
12+
13+
def pipeline_render(self, video_path):
14+
generate_subtitles(video_path)
15+
self.render_queue.put(video_path)
16+
17+
def monitor_queue(self):
18+
while True:
19+
if not self.render_queue.empty():
20+
video_path = self.render_queue.get()
21+
try:
22+
render_video(video_path)
23+
except Exception as e:
24+
scan_log.error(f"Error processing video {video_path}: {e}")
25+
else:
26+
time.sleep(1)
Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import os
55
import subprocess
66
from src.config import GPU_EXIST, SRC_DIR
7-
from src.burn.generate_danmakus import get_resolution, process_danmakus
8-
from src.burn.generate_subtitles import generate_subtitles
9-
from src.burn.render_video import render_video
7+
from src.danmaku.generate_danmakus import get_resolution, process_danmakus
8+
from src.subtitle.generate_subtitles import generate_subtitles
9+
from src.burn.render_command import render_command
1010
from src.upload.extract_video_info import get_video_info
1111
from src.log.logger import scan_log
1212

@@ -20,26 +20,26 @@ def normalize_video_path(filepath):
2020
new_date_time = f"{date_time_parts[0][:4]}-{date_time_parts[0][4:6]}-{date_time_parts[0][6:8]}-{date_time_parts[1]}-{date_time_parts[2]}"
2121
return filepath.rsplit('/', 1)[0] + '/' + parts[0] + '_' + new_date_time + '-.mp4'
2222

23-
def merge_videos(in_final_video, title, artist, date, merge_list):
23+
def merge_command(in_final_video, title, artist, date, merge_list):
2424
"""Merge the video segments and preserve the first video's metadata
2525
Args:
2626
in_final_video: str, the path of videos will be merged
2727
"""
28-
merge_command = [
28+
command = [
2929
'ffmpeg', '-f', 'concat', '-safe', '0', '-i', merge_list, '-metadata', f'title={title}', '-metadata', f'artist={artist}', '-metadata', f'date={date}', '-use_wallclock_as_timestamps', '1',
3030
'-c', 'copy', in_final_video
3131
]
3232
try:
3333
scan_log.info("Begin merging videos...")
34-
result = subprocess.run(merge_command, check=True, capture_output=True, text=True)
34+
result = subprocess.run(command, check=True, capture_output=True, text=True)
3535
scan_log.debug(f"FFmpeg output: {result.stdout}")
3636
if result.stderr:
3737
scan_log.debug(f"FFmpeg debug: {result.stderr}")
3838
except subprocess.CalledProcessError as e:
3939
scan_log.error(f"Error: {e.stderr}")
4040
subprocess.run(['rm', merge_list], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
4141

42-
def render_and_merge(video_path_list):
42+
def render_then_merge(video_path_list):
4343
title = ''
4444
artist = ''
4545
date = ''
@@ -72,7 +72,7 @@ def render_and_merge(video_path_list):
7272
if GPU_EXIST:
7373
generate_subtitles(original_video_path)
7474
# Burn danmaku or subtitles into the videos
75-
render_video(original_video_path, video_to_be_merged, subtitle_font_size, subtitle_margin_v)
75+
render_command(original_video_path, video_to_be_merged, subtitle_font_size, subtitle_margin_v)
7676
if not os.path.exists(merge_list):
7777
open(merge_list, 'w').close()
7878
with open(merge_list, 'a') as f:
@@ -87,15 +87,8 @@ def render_and_merge(video_path_list):
8787
# test_path = original_video_path[:-4]
8888
# os.rename(original_video_path, test_path)
8989

90-
merge_videos(output_video_path, title, artist, date, merge_list)
90+
merge_command(output_video_path, title, artist, date, merge_list)
9191
subprocess.run(['rm', '-r', tmp])
9292

9393
with open(f"{SRC_DIR}/upload/uploadVideoQueue.txt", "a") as file:
94-
file.write(f"{output_video_path}\n")
95-
96-
if __name__ == '__main__':
97-
parser = argparse.ArgumentParser(description='Danmaku burns')
98-
parser.add_argument('video_path', type=str, help='Path to the Video file')
99-
args = parser.parse_args()
100-
video_path = args.video_path
101-
render_and_merge(video_path)
94+
file.write(f"{output_video_path}\n")

0 commit comments

Comments
 (0)