Skip to content

Commit ed6715b

Browse files
authored
feat: redesign the log module (#190)
fix #183
1 parent 354a96d commit ed6715b

File tree

20 files changed

+197
-99
lines changed

20 files changed

+197
-99
lines changed

logs/burningLog/.gitkeep

Whitespace-only changes.

logs/mergeLog/.gitkeep

Whitespace-only changes.

logs/scanLog/.gitkeep

Whitespace-only changes.

logs/uploadLog/.gitkeep

Whitespace-only changes.

scan.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# kill the previous scanSegments process
22
kill -9 $(ps aux | grep 'src.burn.scan' | grep -v grep | awk '{print $2}')
33
# start the scanSegments process
4-
nohup python -m src.burn.scan > $BILIVE_PATH/logs/scanLog/scan-$(date +%Y%m%d-%H%M%S).log 2>&1 &
4+
python -m src.burn.scan
55
# Check if the last command was successful
66
if [ $? -eq 0 ]; then
77
echo "success"

src/burn/generate_danmakus.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from src.config import DanmakuFactory_PATH
66
import os
77
from src.utils.removeEmojis import *
8+
from src.log.logger import scan_log
89

910
def get_resolution(in_video_path):
1011
"""Return the resolution of video
@@ -23,10 +24,10 @@ def get_resolution(in_video_path):
2324
check=True
2425
)
2526
resolution = result.stdout.strip()
26-
print("The video resolution is " + resolution, flush=True)
27+
scan_log.info("The video resolution is " + resolution)
2728
return resolution
2829
except subprocess.CalledProcessError as e:
29-
print(f"Error: {e.stderr}", flush=True)
30+
scan_log.error(f"Error: {e.stderr}")
3031
return '1920x1080'
3132

3233
def process_danmakus(in_xml_path, resolution):
@@ -72,8 +73,14 @@ def process_danmakus(in_xml_path, resolution):
7273
subtitle_font_size = '16'
7374
subtitle_margin_v = '60'
7475
# Convert danmakus to ass file
75-
subprocess.run([DanmakuFactory_PATH, "-o", in_ass_path, "-i", in_xml_path, "--resolution", resolution, "--msgboxsize", boxsize, "--msgboxfontsize", boxfont, "-S", danmakufont, "--ignore-warnings"])
76+
try:
77+
result = subprocess.run(
78+
[DanmakuFactory_PATH, "-o", in_ass_path, "-i", in_xml_path, "--resolution", resolution, "--msgboxsize", boxsize, "--msgboxfontsize", boxfont, "-S", danmakufont, "--ignore-warnings"],
79+
check=True, capture_output=True, text=True)
80+
scan_log.debug(f"DanmakuFactory output: {result.stdout}")
81+
except subprocess.CalledProcessError as e:
82+
scan_log.error(f"Error: {e.stderr}")
7683
# Remove emojis from ass danmakus (the ffmpeg does not support emojis)
7784
remove_emojis(in_ass_path)
78-
print(f"The {in_ass_path} has been processed.", flush=True)
85+
scan_log.info(f"The emojis of {in_ass_path} has been removed.")
7986
return subtitle_font_size, subtitle_margin_v

src/burn/generate_subtitles.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Copyright (c) 2024 bilive.
22

33
import subprocess
4-
from src.config import WHISPER_LOG_PATH, SRC_DIR
4+
from src.config import SRC_DIR
5+
from src.log.logger import scan_log
56
import os
67

78
# Generate the srt file via whisper model
@@ -10,5 +11,10 @@ def generate_subtitles(in_video_path):
1011
Args:
1112
in_video_path: str, the path of video
1213
"""
13-
with open(WHISPER_LOG_PATH, 'a') as wlog:
14-
subprocess.run(['python', os.path.join(SRC_DIR, 'subtitle', 'generate.py'), in_video_path], stdout=wlog)
14+
try:
15+
subprocess.run(
16+
['python', os.path.join(SRC_DIR, 'subtitle', 'generate.py'), in_video_path],
17+
stdout=subprocess.DEVNULL
18+
)
19+
except subprocess.CalledProcessError as e:
20+
scan_log.error(f"Generate subtitles failed: {e.stderr}")

src/burn/only_render.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from src.autoslice.slice_video import slice_video, inject_metadata, zhipu_glm_4v_plus_generate_title
1111
from src.autoslice.calculate_density import extract_dialogues, calculate_density, format_time
1212
from src.upload.extract_video_info import get_video_info
13+
from src.log.logger import scan_log
1314
import queue
1415
import threading
1516
import time
@@ -31,7 +32,7 @@ def check_file_size(file_path):
3132

3233
def render_video_only(video_path):
3334
if not os.path.exists(video_path):
34-
print(f"File {video_path} does not exist.")
35+
scan_log.error(f"File {video_path} does not exist.")
3536
return
3637

3738
original_video_path = str(video_path)
@@ -47,9 +48,9 @@ def render_video_only(video_path):
4748
# Process the danmakus to ass and remove emojis
4849
subtitle_font_size, subtitle_margin_v = process_danmakus(xml_path, video_resolution)
4950
except TypeError as e:
50-
print(f"TypeError: {e} - Check the return value of process_danmakus")
51+
scan_log.error(f"TypeError: {e} - Check the return value of process_danmakus")
5152
except FileNotFoundError as e:
52-
print(f"FileNotFoundError: {e} - Check if the file exists")
53+
scan_log.error(f"FileNotFoundError: {e} - Check if the file exists")
5354

5455
# Generate the srt file via whisper model
5556
if GPU_EXIST:
@@ -58,7 +59,7 @@ def render_video_only(video_path):
5859

5960
# Burn danmaku or subtitles into the videos
6061
render_video(original_video_path, format_video_path, subtitle_font_size, subtitle_margin_v)
61-
print("complete danamku burning and wait for uploading!", flush=True)
62+
scan_log.info("Complete danamku burning and wait for uploading!")
6263

6364
if AUTO_SLICE:
6465
if check_file_size(format_video_path) > MIN_VIDEO_SIZE:
@@ -67,7 +68,7 @@ def render_video_only(video_path):
6768
dialogues = extract_dialogues(ass_path)
6869
max_start_time, max_density = calculate_density(dialogues)
6970
formatted_time = format_time(max_start_time)
70-
print(f"The 30-second window with the highest density starts at {formatted_time} seconds with {max_density} danmakus.", flush=True)
71+
scan_log.info(f"The 30-second window with the highest density starts at {formatted_time} seconds with {max_density} danmakus.")
7172
slice_video(format_video_path, max_start_time, slice_video_path)
7273
glm_title = zhipu_glm_4v_plus_generate_title(slice_video_path, artist)
7374
slice_video_flv_path = slice_video_path[:-4] + '.flv'
@@ -86,7 +87,7 @@ def render_video_only(video_path):
8687
with open(f"{SRC_DIR}/upload/uploadVideoQueue.txt", "a") as file:
8788
file.write(f"{format_video_path}\n")
8889
if AUTO_SLICE:
89-
print("complete slice video and wait for uploading!", flush=True)
90+
scan_log.info("Complete slice video and wait for uploading!")
9091
slice_video_path = format_video_path[:-4] + '_slice.mp4'
9192
slice_video_flv_path = slice_video_path[:-4] + '.flv'
9293
file.write(f"{slice_video_flv_path}\n")
@@ -106,7 +107,7 @@ def monitor_queue(self):
106107
try:
107108
render_video_only(video_path)
108109
except Exception as e:
109-
print(f"Error processing video {video_path}: {e}", flush=True)
110+
scan_log.error(f"Error processing video {video_path}: {e}")
110111
else:
111112
time.sleep(1)
112113

src/burn/render_and_merge.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import argparse
44
import os
55
import subprocess
6-
from src.config import GPU_EXIST, SRC_DIR, MERGE_LOG_PATH
6+
from src.config import GPU_EXIST, SRC_DIR
77
from src.burn.generate_danmakus import get_resolution, process_danmakus
88
from src.burn.generate_subtitles import generate_subtitles
99
from src.burn.render_video import render_video
1010
from src.upload.extract_video_info import get_video_info
11+
from src.log.logger import scan_log
1112

1213
def normalize_video_path(filepath):
1314
"""Normalize the video path to upload
@@ -28,9 +29,15 @@ def merge_videos(in_final_video, title, artist, date, merge_list):
2829
'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',
2930
'-c', 'copy', in_final_video
3031
]
31-
with open(MERGE_LOG_PATH, 'a') as mlog:
32-
subprocess.run(merge_command, stdout=mlog, stderr=subprocess.STDOUT)
33-
subprocess.run(['rm', merge_list])
32+
try:
33+
scan_log.info("Begin merging videos...")
34+
result = subprocess.run(merge_command, check=True, capture_output=True, text=True)
35+
scan_log.debug(f"FFmpeg output: {result.stdout}")
36+
if result.stderr:
37+
scan_log.debug(f"FFmpeg debug: {result.stderr}")
38+
except subprocess.CalledProcessError as e:
39+
scan_log.error(f"Error: {e.stderr}")
40+
subprocess.run(['rm', merge_list], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
3441

3542
def render_and_merge(video_path_list):
3643
title = ''
@@ -48,8 +55,8 @@ def render_and_merge(video_path_list):
4855
if output_video_path == '':
4956
title, artist, date = get_video_info(stripped_line)
5057
output_video_path = normalize_video_path(stripped_line)
51-
print("The output video is " + output_video_path)
52-
subprocess.run(['mkdir', tmp])
58+
scan_log.info("The output video is " + output_video_path)
59+
subprocess.run(['mkdir', tmp], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
5360

5461
video_to_be_merged = tmp + video_name
5562
original_video_path = stripped_line
@@ -70,7 +77,7 @@ def render_and_merge(video_path_list):
7077
open(merge_list, 'w').close()
7178
with open(merge_list, 'a') as f:
7279
f.write(f"file '{video_to_be_merged}'\n")
73-
print("complete danamku burning and wait for uploading!", flush=True)
80+
scan_log.info("Complete danamku burning and wait for uploading!")
7481

7582
for remove_path in [original_video_path, xml_path, ass_path, srt_path, jsonl_path]:
7683
if os.path.exists(remove_path):

src/burn/render_video.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Copyright (c) 2024 bilive.
22

33
import subprocess
4-
from src.config import GPU_EXIST, BURN_LOG_PATH
4+
from src.config import GPU_EXIST
5+
from src.log.logger import scan_log
56
import os
67

78
def render_video(in_video_path, out_video_path, in_subtitle_font_size, in_subtitle_margin_v):
@@ -16,29 +17,45 @@ def render_video(in_video_path, out_video_path, in_subtitle_font_size, in_subtit
1617
if GPU_EXIST:
1718
in_srt_path = in_video_path[:-4] + '.srt'
1819
if os.path.isfile(in_ass_path):
19-
print("wisper danmaku")
20+
scan_log.info("Current Mode: GPU with danmaku")
2021
command = [
2122
'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path,
2223
'-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
2324
]
24-
with open(BURN_LOG_PATH, 'a') as blog:
25-
subprocess.run(command, stdout=blog, stderr=subprocess.STDOUT)
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+
2633
else:
27-
print("wisper no danmaku")
34+
scan_log.info("Current Mode: GPU without danmaku")
2835
command_no_danmaku = [
2936
'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path,
3037
'-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
3138
]
32-
with open(BURN_LOG_PATH, 'a') as blog:
33-
subprocess.run(command_no_danmaku, stdout=blog, stderr=subprocess.STDOUT)
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}")
3446
else:
3547
if os.path.isfile(in_ass_path):
36-
print("no gpu danmaku")
48+
scan_log.info("Current Mode: CPU with danmaku")
3749
command_without_gpu = [
3850
'ffmpeg', '-y', '-i', in_video_path, '-vf', f'ass={in_ass_path}', '-preset', 'ultrafast', out_video_path
3951
]
40-
with open(BURN_LOG_PATH, 'a') as blog:
41-
subprocess.run(command_without_gpu, stdout=blog, stderr=subprocess.STDOUT)
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}")
4259
else:
43-
print("no gpu no danmaku")
44-
subprocess.run(['mv', in_video_path, out_video_path])
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)

0 commit comments

Comments
 (0)