Skip to content

Commit 2a7c09b

Browse files
authored
feat: introduce DanmakuConvert (#237)
* feat: introduce DanmakuConvert * fix: remove abundant config * docs: update docs
1 parent e8b8022 commit 2a7c09b

12 files changed

+52
-88
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "src/danmaku/DanmakuConvert"]
2+
path = src/danmaku/DanmakuConvert
3+
url = https://github.com/timerring/DanmakuConvert.git

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
- **速度快**:采用 `pipeline` 流水线处理视频,理想情况下录播与直播相差半小时以内,没下播就能上线录播,**目前已知 b 站录播最快版本**
3030
- **多房间**:同时录制多个直播间内容视频以及弹幕文件(包含普通弹幕,付费弹幕以及礼物上舰等信息)。
3131
- **占用小**:自动删除本地已上传的视频,极致节省空间。
32-
- **模版化**:无需复杂配置,开箱即用,( :tada: NEW)通过 b 站搜索建议接口自动抓取相关热门标签。
32+
- **模版化**:无需复杂配置,开箱即用,通过 b 站搜索建议接口自动抓取相关热门标签。
3333
- **检测片段并合并**:对于网络问题或者直播连线导致的视频流分段,能够自动检测合并成为完整视频。
34-
- **自动渲染弹幕**自动转换xml为ass弹幕文件并且渲染到视频中形成**有弹幕版视频**并自动上传。
34+
- **自动渲染弹幕**自动转换xml为ass弹幕文件,该转换工具库已经开源 [DanmakuConvert](https://github.com/timerring/DanmakuConvert) 并且渲染到视频中形成**有弹幕版视频**并自动上传。
3535
- **硬件要求极低**:无需GPU,只需最基础的单核CPU搭配最低的运存即可完成录制,弹幕渲染,上传等等全部过程,无最低配置要求,10年前的电脑或服务器依然可以使用!
3636
- **( :tada: NEW)自动渲染字幕**(如需使用本功能,则需保证有 Nvidia 显卡):采用 OpenAI 的开源模型 [`whisper`](https://github.com/openai/whisper),自动识别视频内语音并转换为字幕渲染至视频中。
3737
- **( :tada: NEW)自动切片上传**:根据弹幕密度计算寻找高能片段并切片,该自动切片工具库已开源 [auto-slice-video](https://github.com/timerring/auto-slice-video)
@@ -50,9 +50,9 @@ graph TD
5050
whisper[whisperASR模型] --生成字幕-->parameter[查询视频分辨率]
5151
subgraph 启动新进程
5252
parameter[查询分辨率] -->ifDanmaku{判断}
53-
ifDanmaku -->|有弹幕| DanmakuFactory[DanmakuFactory]
53+
ifDanmaku -->|有弹幕| DanmakuConvert[DanmakuConvert]
5454
ifDanmaku -->|无弹幕| ffmpeg1[ffmpeg]
55-
DanmakuFactory[DanmakuFactory] --根据分辨率转换弹幕--> ffmpeg1[ffmpeg]
55+
DanmakuConvert[DanmakuConvert] --根据分辨率转换弹幕--> ffmpeg1[ffmpeg]
5656
ffmpeg1[ffmpeg] --渲染弹幕及字幕 --> Video[视频文件]
5757
Video[视频文件] --计算弹幕密度并切片--> GLM[多模态视频理解模型]
5858
GLM[多模态视频理解模型] --生成切片信息--> slice[视频切片]
@@ -69,7 +69,7 @@ graph TD
6969
## 3. 测试硬件
7070
+ OS: Ubuntu 22.04.4 LTS
7171

72-
>尽量使用 22.04+ 的版本,更早版本的 ubuntu 自带 gcc 版本无法更新至 DanmakuFactory 以及 biliup-rs 所需版本,若使用较早版本,请参考 [version `GLIBC_2.34‘ not found简单有效解决方法](https://blog.csdn.net/huazhang_001/article/details/128828999)
72+
>尽量使用 22.04+ 的版本,更早版本的 ubuntu 自带 gcc 版本无法更新至 biliup-rs 所需版本,若使用较早版本,请参考 [version `GLIBC_2.34‘ not found简单有效解决方法](https://blog.csdn.net/huazhang_001/article/details/128828999)
7373
+ CPU:2核 Intel(R) Xeon(R) Platinum 85
7474
+ GPU:无
7575
+ 内存:2G

docs/getting-started.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ graph TD
3131
whisper[whisperASR模型] --生成字幕-->parameter[查询视频分辨率]
3232
subgraph 启动新进程
3333
parameter[查询分辨率] -->ifDanmaku{判断}
34-
ifDanmaku -->|有弹幕| DanmakuFactory[DanmakuFactory]
34+
ifDanmaku -->|有弹幕| DanmakuConvert[DanmakuConvert]
3535
ifDanmaku -->|无弹幕| ffmpeg1[ffmpeg]
36-
DanmakuFactory[DanmakuFactory] --根据分辨率转换弹幕--> ffmpeg1[ffmpeg]
36+
DanmakuConvert[DanmakuConvert] --根据分辨率转换弹幕--> ffmpeg1[ffmpeg]
3737
ffmpeg1[ffmpeg] --渲染弹幕及字幕 --> Video[视频文件]
3838
Video[视频文件] --计算弹幕密度并切片--> GLM[多模态视频理解模型]
3939
GLM[多模态视频理解模型] --生成切片信息--> slice[视频切片]

docs/install-questions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Reference: https://github.com/timerring/bilive/issues/106
1212

1313
Reference: https://blog.csdn.net/huazhang_001/article/details/128828999
1414

15-
尽量使用 22.04+ 的版本,更早版本的 ubuntu 自带 gcc 版本无法更新至 DanmakuFactory 以及 biliup-rs 所需版本。
15+
尽量使用 22.04+ 的版本,更早版本的 ubuntu 自带 gcc 版本无法更新至 biliup-rs 所需版本。
1616

1717
解决方案:手动更新版本,参照链接操作即可。
1818

docs/test-hardware.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
+ OS: Ubuntu 20.04.4 LTS
1919

2020
> [!WARNING]
21-
> 尽量使用 22.04+ 的版本,更早版本的 ubuntu 自带 gcc 版本无法更新至 DanmakuFactory 以及 biliup-rs 所需版本,若使用较早版本,请参考 [version `GLIBC_2.34‘ not found简单有效解决方法](https://blog.csdn.net/huazhang_001/article/details/128828999)
21+
> 尽量使用 22.04+ 的版本,更早版本的 ubuntu 自带 gcc 版本无法更新至 biliup-rs 所需版本,若使用较早版本,请参考 [version `GLIBC_2.34‘ not found简单有效解决方法](https://blog.csdn.net/huazhang_001/article/details/128828999)
2222
2323
+ CPU:Intel(R) Core(TM) i5-9300H CPU 8 核
2424
+ GPU:NVIDIA GeForce GTX 1650 显存 4GB

src/burn/render_video.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ def render_video(video_path):
4040
jsonl_path = original_video_path[:-4] + '.jsonl'
4141

4242
# Recoginze the resolution of video
43-
video_resolution = get_resolution(original_video_path)
43+
resolution_x, resolution_y = get_resolution(original_video_path)
4444
try:
4545
# Process the danmakus to ass and remove emojis
46-
subtitle_font_size, subtitle_margin_v = process_danmakus(xml_path, video_resolution)
46+
subtitle_font_size, subtitle_margin_v = process_danmakus(xml_path, resolution_x, resolution_y)
4747
except TypeError as e:
4848
scan_log.error(f"TypeError: {e} - Check the return value of process_danmakus")
4949
except FileNotFoundError as e:

src/config.py

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
BILIVE_DIR = str(Path(SRC_DIR).parent)
2323
LOG_DIR = os.path.join(BILIVE_DIR, 'logs')
2424
VIDEOS_DIR = os.path.join(BILIVE_DIR, 'Videos')
25-
DanmakuFactory_bin = os.path.join('utils', 'DanmakuFactory')
26-
DanmakuFactory_PATH = os.path.join(SRC_DIR, DanmakuFactory_bin)
2725

2826
def get_model_path():
2927
SRC_DIR = str(Path(os.path.abspath(__file__)).parent)

src/danmaku/DanmakuConvert

Submodule DanmakuConvert added at f129121

src/danmaku/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
import sys
44
import os
5-
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
5+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
6+
from .DanmakuConvert.dmconvert import convert_xml_to_ass
7+
8+
__all__ = ['convert_xml_to_ass']

src/danmaku/generate_danmakus.py

+33-43
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import os
44
import subprocess
5-
from src.config import DanmakuFactory_PATH
65
from src.log.logger import scan_log
76
from .adjust_price import update_danmaku_prices
8-
from .remove_emojis import remove_emojis
7+
from .DanmakuConvert.dmconvert import convert_xml_to_ass
98

109

1110
def get_resolution(in_video_path):
@@ -19,69 +18,60 @@ def get_resolution(in_video_path):
1918
# Use ffprobe to acquire the video resolution
2019
result = subprocess.run(
2120
['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width,height', '-of', 'csv=s=x:p=0', in_video_path],
22-
stdout=subprocess.PIPE,
23-
stderr=subprocess.PIPE,
21+
check=True,
2422
text=True,
25-
check=True
23+
capture_output=True
2624
)
25+
scan_log.debug(f"get_resolution FFmpeg output: {result.stdout}")
26+
if result.stderr:
27+
scan_log.debug(f"get_resolution FFmpeg debug: {result.stderr}")
2728
resolution = result.stdout.strip()
28-
scan_log.info("The video resolution is " + resolution)
29-
return resolution
29+
resolution_x, resolution_y = map(int, resolution.split('x'))
30+
scan_log.info(f"The video resolution x is {resolution_x} and y is {resolution_y}")
31+
return resolution_x, resolution_y
3032
except subprocess.CalledProcessError as e:
31-
scan_log.error(f"Error: {e.stderr}")
32-
return '1920x1080'
33+
scan_log.error(f"get_resolution Error: {e.stderr}")
34+
return 1920, 1080
3335

34-
def process_danmakus(in_xml_path, resolution):
36+
def process_danmakus(in_xml_path, resolution_x, resolution_y):
3537
"""Generate and process the danmakus according to different resolution.
3638
Args:
3739
in_xml_path: str, the xml path to generate ass file
38-
resolution: str, the resolution of the video
40+
resolution_x: int, the x resolution of the video
41+
resolution_y: int, the y resolution of the video
3942
Return:
4043
subtitle_font_size: str, the font size of subtitles
44+
subtitle_margin_v: str, the margin v of subtitles
4145
"""
4246
if os.path.isfile(in_xml_path):
4347
# Adjust the price of sc and guard
4448
update_danmaku_prices(in_xml_path)
45-
in_ass_path = in_xml_path[:-4] + '.ass'
46-
if resolution == '1280x720':
47-
boxsize = '500x720'
48-
boxfont = '23'
49-
danmakufont = '38'
49+
out_ass_path = in_xml_path[:-4] + '.ass'
50+
if resolution_x == 1280 and resolution_y == 720:
51+
boxfont = 30
52+
danmakufont = 38
5053
subtitle_font_size = '15'
5154
subtitle_margin_v = '20'
52-
elif resolution == '1920x1080':
53-
boxsize = '500x1080'
54-
boxfont = '50'
55-
danmakufont = '55'
56-
subtitle_font_size = '16'
57-
subtitle_margin_v = '60'
58-
elif resolution == '1080x1920':
59-
boxsize = '500x1920'
60-
boxfont = '55'
61-
danmakufont = '60'
55+
elif resolution_x == 720 and resolution_y == 1280:
56+
boxfont = 30
57+
danmakufont = 38
6258
subtitle_font_size = '8'
6359
subtitle_margin_v = '60'
64-
elif resolution == '720x1280':
65-
boxsize = '500x1280'
66-
boxfont = '28'
67-
danmakufont = '38'
60+
elif resolution_x == 1920 and resolution_y == 1080:
61+
boxfont = 42
62+
danmakufont = 42
63+
subtitle_font_size = '16'
64+
subtitle_margin_v = '60'
65+
elif resolution_x == 1080 and resolution_y == 1920:
66+
boxfont = 42
67+
danmakufont = 42
6868
subtitle_font_size = '8'
6969
subtitle_margin_v = '60'
7070
else:
71-
boxsize = '500x1080'
72-
boxfont = '38'
73-
danmakufont = '38'
71+
boxfont = 38
72+
danmakufont = 38
7473
subtitle_font_size = '16'
7574
subtitle_margin_v = '60'
7675
# Convert danmakus to ass file
77-
try:
78-
result = subprocess.run(
79-
[DanmakuFactory_PATH, "-o", in_ass_path, "-i", in_xml_path, "--resolution", resolution, "--msgboxsize", boxsize, "--msgboxfontsize", boxfont, "-S", danmakufont, "--ignore-warnings"],
80-
check=True, capture_output=True, text=True)
81-
scan_log.debug(f"DanmakuFactory output: {result.stdout}")
82-
except subprocess.CalledProcessError as e:
83-
scan_log.error(f"Error: {e.stderr}")
84-
# Remove emojis from ass danmakus (the ffmpeg does not support emojis)
85-
remove_emojis(in_ass_path)
86-
scan_log.info(f"The emojis of {in_ass_path} has been removed.")
76+
convert_xml_to_ass(danmakufont, boxfont, resolution_x, resolution_y, in_xml_path, out_ass_path)
8777
return subtitle_font_size, subtitle_margin_v

src/danmaku/remove_emojis.py

-31
This file was deleted.

src/utils/DanmakuFactory

-148 KB
Binary file not shown.

0 commit comments

Comments
 (0)