Skip to content

Commit 89ecbdc

Browse files
authored
feat: introduce bilitool (#246)
* feat: introduce bilitool * fix: remove config file * fix: remove abundant dependencies
1 parent d599541 commit 89ecbdc

File tree

15 files changed

+153
-198
lines changed

15 files changed

+153
-198
lines changed

.dockerignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ pnpm-lock.yaml
77
test/*
88
Videos/*
99
settings-production.toml
10-
startRecord-production.sh
11-
src/utils/cookies.json
10+
startRecord-production.sh

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,4 @@ cookies.json
381381
src/subtitle/models/base.pt
382382
src/subtitle/models/small.pt
383383
src/burn/mergevideo.txt
384-
src/upload/upload.yaml
385-
src/upload/uploadVideoQueue.txt
386384
src/db/data.db

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "src/autoslice/auto_slice_video"]
55
path = src/autoslice/auto_slice_video
66
url = https://github.com/timerring/auto-slice-video.git
7+
[submodule "src/upload/bilitool"]
8+
path = src/upload/bilitool
9+
url = https://github.com/timerring/bilitool.git

src/burn/render_then_merge.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
from src.upload.extract_video_info import get_video_info
1111
from src.log.logger import scan_log
1212
from db.conn import insert_upload_queue
13-
from src.upload.generate_yaml import generate_yaml_template
14-
from uuid import uuid4
1513

1614
def normalize_video_path(filepath):
1715
"""Normalize the video path to upload
@@ -92,11 +90,6 @@ def render_then_merge(video_path_list):
9290

9391
merge_command(output_video_path, title, artist, date, merge_list)
9492
subprocess.run(['rm', '-r', tmp])
95-
96-
yaml_template = generate_yaml_template(output_video_path)
97-
template_path = os.path.join(VIDEOS_DIR, f'upload_conf/{uuid4()}.yaml')
98-
with open(template_path, 'w', encoding='utf-8') as f:
99-
f.write(yaml_template)
10093

101-
if not insert_upload_queue(output_video_path, template_path):
102-
scan_log('插入待上传条目失败')
94+
if not insert_upload_queue(output_video_path):
95+
scan_log.error('Cannot insert the video to the upload queue')

src/burn/render_video.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
from src.upload.extract_video_info import get_video_info
1414
from src.log.logger import scan_log
1515
from db.conn import insert_upload_queue
16-
from src.upload.generate_yaml import generate_yaml_template, generate_slice_yaml_template
17-
from uuid import uuid4
1816

1917
def normalize_video_path(filepath):
2018
"""Normalize the video path to upload
@@ -72,13 +70,8 @@ def render_video(video_path):
7270
slice_video_flv_path = slice_path[:-4] + '.flv'
7371
inject_metadata(slice_path, glm_title, slice_video_flv_path)
7472
os.remove(slice_path)
75-
slice_yaml_template = generate_slice_yaml_template(slice_video_flv_path)
76-
slice_template_path = os.path.join(VIDEOS_DIR, f'upload_conf/{uuid4()}.yaml')
77-
with open(slice_template_path, 'w', encoding='utf-8') as f:
78-
f.write(slice_yaml_template)
79-
80-
if not insert_upload_queue(slice_video_flv_path, slice_template_path):
81-
scan_log('插入待上传条目失败')
73+
if not insert_upload_queue(slice_video_flv_path):
74+
scan_log.error('Cannot insert the video to the upload queue')
8275
except Exception as e:
8376
scan_log.error(f"Error in {slice_path}: {e}")
8477

@@ -91,10 +84,5 @@ def render_video(video_path):
9184
# test_path = original_video_path[:-4]
9285
# os.rename(original_video_path, test_path)
9386

94-
yaml_template = generate_yaml_template(format_video_path)
95-
template_path = os.path.join(VIDEOS_DIR, f'upload_conf/{uuid4()}.yaml')
96-
with open(template_path, 'w', encoding='utf-8') as f:
97-
f.write(yaml_template)
98-
99-
if not insert_upload_queue(format_video_path, template_path):
100-
scan_log('插入待上传条目失败')
87+
if not insert_upload_queue(format_video_path):
88+
scan_log.error('Cannot insert the video to the upload queue')

src/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737

3838
if not os.path.exists(SRC_DIR + '/db/data.db'):
39-
print("初始化数据库")
39+
print("Initialize the database")
4040
create_table()
4141

4242
if not os.path.exists(VIDEOS_DIR):

src/db/conn.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def create_table():
1313
db = connect()
1414
cursor = db.cursor()
1515
sql = [
16-
"create table upload_queue (id integer primary key autoincrement, video_path text, config_path text, locked integer default 0);",
16+
"create table upload_queue (id integer primary key autoincrement, video_path text, locked integer default 0);",
1717
"create unique index idx_video_path on upload_queue(video_path);",
1818
]
1919
for s in sql:
@@ -28,17 +28,26 @@ def create_table():
2828
def get_single_upload_queue():
2929
db = connect()
3030
cursor = db.cursor()
31-
cursor.execute("select video_path, config_path from upload_queue where locked = 0 limit 1;")
31+
cursor.execute("select video_path from upload_queue where locked = 0 limit 1;")
3232
row = cursor.fetchone()
33-
result = {'video_path': row[0], 'config_path': row[1]} if row else None
33+
result = {'video_path': row[0]} if row else None
3434
db.close()
3535
return result
3636

37-
def insert_upload_queue(video_path: str, config_path: str):
37+
def get_all_upload_queue():
38+
db = connect()
39+
cursor = db.cursor()
40+
cursor.execute("select video_path from upload_queue;")
41+
rows = cursor.fetchall()
42+
result = [{'video_path': row[0]} for row in rows]
43+
db.close()
44+
return result
45+
46+
def insert_upload_queue(video_path: str):
3847
try:
3948
db = connect()
4049
cursor = db.cursor()
41-
cursor.execute("insert into upload_queue (video_path, config_path) values (?, ?);", (video_path, config_path))
50+
cursor.execute("insert into upload_queue (video_path) values (?);", (video_path,))
4251
db.commit()
4352
db.close()
4453
return True
@@ -76,12 +85,16 @@ def update_upload_queue_lock(video_path: str, locked: int):
7685
# Create Table
7786
create_table()
7887
# Insert Test Data
79-
insert_upload_queue('test.mp4', 'config.yaml')
88+
insert_upload_queue('')
8089
# Insert again to check the unique index
81-
print(insert_upload_queue('test.mp4', 'config.yaml'))
82-
# Get the single upload queue, shold be {'video_path': 'test.mp4', 'config_path': 'config.yaml'}
83-
print(get_single_upload_queue())
90+
# print(insert_upload_queue(''))
91+
# Get the single upload queue, shold be {'video_path': 'test.mp4'}
92+
# print(get_single_upload_queue())
93+
# Get all upload queue
94+
print(get_all_upload_queue())
95+
# unlock the upload queue
96+
update_upload_queue_lock('test.mp4', 0)
8497
# Delete the upload queue
85-
delete_upload_queue('test.mp4')
98+
delete_upload_queue('')
8699
# Get the single upload queue after delete, should be None
87100
print(get_single_upload_queue())

src/upload/__init__.py

Lines changed: 4 additions & 1 deletion
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 .bilitool.bilitool import UploadController, FeedController
7+
8+
__all__ = ['UploadController', 'FeedController']

src/upload/bilitool

Submodule bilitool added at facd698

src/upload/extract_video_info.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from datetime import datetime
88
from src.upload.query_search_suggestion import get_bilibili_suggestions
99
from src.config import GPU_EXIST, TITLE, DESC
10+
from src.log.logger import upload_log
1011

1112
def get_video_info(video_file_path):
1213
"""get the title, artist and date of the video file via ffprobe
@@ -15,28 +16,35 @@ def get_video_info(video_file_path):
1516
Returns:
1617
str: the title of the video file, if failed, return None
1718
"""
18-
19-
command = [
20-
"ffprobe",
21-
"-v", "quiet",
22-
"-print_format", "json",
23-
"-show_format",
24-
video_file_path
25-
]
26-
output = subprocess.check_output(command, stderr=subprocess.STDOUT).decode('utf-8')
27-
parsed_output = json.loads(output)
28-
title_value = parsed_output["format"]["tags"]["title"]
29-
artist_value = parsed_output["format"]["tags"]["artist"]
30-
date_value = parsed_output["format"]["tags"]["date"]
31-
if len(date_value) > 8:
32-
dt = datetime.fromisoformat(date_value)
33-
new_date = dt.strftime('%Y%m%d')
34-
else:
35-
new_date = date_value
36-
return title_value, artist_value, new_date
19+
try:
20+
command = [
21+
"ffprobe",
22+
"-v", "quiet",
23+
"-print_format", "json",
24+
"-show_format",
25+
video_file_path
26+
]
27+
output = subprocess.check_output(command, stderr=subprocess.STDOUT).decode('utf-8')
28+
parsed_output = json.loads(output)
29+
title_value = parsed_output["format"]["tags"]["title"]
30+
artist_value = parsed_output["format"]["tags"]["artist"]
31+
date_value = parsed_output["format"]["tags"]["date"]
32+
if len(date_value) > 8:
33+
dt = datetime.fromisoformat(date_value)
34+
new_date = dt.strftime('%Y%m%d')
35+
else:
36+
new_date = date_value
37+
return title_value, artist_value, new_date
38+
except Exception as e:
39+
# Log the exception if needed
40+
upload_log.error(f"Error occurred in get_video_info: {e}")
41+
return None, None, None
3742

3843
def generate_title(video_path):
3944
title, artist, date = get_video_info(video_path)
45+
if title is None:
46+
upload_log.error(f"Error occurred in generate_title: {title}")
47+
return None
4048
source_link = generate_source(video_path)
4149
prefix = "【弹幕+字幕】" if GPU_EXIST else "【弹幕】"
4250
formatted_title = TITLE.format(artist=artist, date=date, title=title, source_link=source_link)

0 commit comments

Comments
 (0)