+
Skip to content
Merged
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
21 changes: 17 additions & 4 deletions src/autoslice/slice_video.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Copyright (c) 2024 bilive.

import base64
import subprocess
from src.autoslice.calculate_density import extract_dialogues, calculate_density, format_time
from src.config import Your_API_KEY, SLICE_DURATION
import base64
from zhipuai import ZhipuAI
from src.autoslice.calculate_density import extract_dialogues, calculate_density, format_time
from src.log.logger import scan_log

def zhipu_glm_4v_plus_generate_title(video_path, artist):
with open(video_path, 'rb') as video_file:
Expand Down Expand Up @@ -44,7 +45,13 @@ def inject_metadata(video_path, generate_title, output_path):
'-c:a', 'copy',
output_path
]
subprocess.run(command)
try:
result = subprocess.run(command, check=True, capture_output=True, text=True)
scan_log.debug(f"FFmpeg output: {result.stdout}")
if result.stderr:
scan_log.debug(f"FFmpeg debug: {result.stderr}")
except subprocess.CalledProcessError as e:
scan_log.error(f"Error: {e.stderr}")

def slice_video(video_path, start_time, output_path, duration=f'00:00:{SLICE_DURATION}'):
"""Slice the video using ffmpeg."""
Expand All @@ -58,4 +65,10 @@ def slice_video(video_path, start_time, output_path, duration=f'00:00:{SLICE_DUR
'-c:a', 'copy',
output_path
]
subprocess.run(command)
try:
result = subprocess.run(command, check=True, capture_output=True, text=True)
scan_log.debug(f"FFmpeg output: {result.stdout}")
if result.stderr:
scan_log.debug(f"FFmpeg debug: {result.stderr}")
except subprocess.CalledProcessError as e:
scan_log.error(f"Error: {e.stderr}")
120 changes: 0 additions & 120 deletions src/burn/only_render.py

This file was deleted.

61 changes: 61 additions & 0 deletions src/burn/render_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright (c) 2024 bilive.

import os
import subprocess
from src.config import GPU_EXIST
from src.log.logger import scan_log

def render_command(in_video_path, out_video_path, in_subtitle_font_size, in_subtitle_margin_v):
"""Burn the danmakus and subtitles into the videos
Args:
in_video_path: str, the path of video
out_video_path: str, the path of rendered video
in_subtitle_font_size: str, the font size of subtitles
in_subtitle_margin_v: str, the bottom margin of subtitles
"""
in_ass_path = in_video_path[:-4] + '.ass'
if GPU_EXIST:
in_srt_path = in_video_path[:-4] + '.srt'
if os.path.isfile(in_ass_path):
scan_log.info("Current Mode: GPU with danmaku")
command = [
'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path,
'-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
]
try:
result = subprocess.run(command, check=True, capture_output=True, text=True)
scan_log.debug(f"FFmpeg output: {result.stdout}")
if result.stderr:
scan_log.debug(f"FFmpeg debug: {result.stderr}")
except subprocess.CalledProcessError as e:
scan_log.error(f"Error: {e.stderr}")

else:
scan_log.info("Current Mode: GPU without danmaku")
command_no_danmaku = [
'ffmpeg', '-y', '-hwaccel', 'cuda', '-c:v', 'h264_cuvid', '-i', in_video_path,
'-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
]
try:
result = subprocess.run(command_no_danmaku, check=True, capture_output=True, text=True)
scan_log.debug(f"FFmpeg output: {result.stdout}")
if result.stderr:
scan_log.debug(f"FFmpeg debug: {result.stderr}")
except subprocess.CalledProcessError as e:
scan_log.error(f"Error: {e.stderr}")
else:
if os.path.isfile(in_ass_path):
scan_log.info("Current Mode: CPU with danmaku")
command_without_gpu = [
'ffmpeg', '-y', '-i', in_video_path, '-vf', f'ass={in_ass_path}', '-preset', 'ultrafast', out_video_path
]
try:
result = subprocess.run(command_without_gpu, check=True, capture_output=True, text=True)
scan_log.debug(f"FFmpeg output: {result.stdout}")
if result.stderr:
scan_log.debug(f"FFmpeg debug: {result.stderr}")
except subprocess.CalledProcessError as e:
scan_log.error(f"Error: {e.stderr}")
else:
scan_log.info("Current Mode: CPU without danmaku")
subprocess.run(['mv', in_video_path, out_video_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
26 changes: 26 additions & 0 deletions src/burn/render_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 2024 bilive.

import queue
import time
from src.subtitle.generate_subtitles import generate_subtitles
from src.burn.render_video import render_video
from src.log.logger import scan_log

class VideoRenderQueue:
def __init__(self):
self.render_queue = queue.Queue()

def pipeline_render(self, video_path):
generate_subtitles(video_path)
self.render_queue.put(video_path)

def monitor_queue(self):
while True:
if not self.render_queue.empty():
video_path = self.render_queue.get()
try:
render_video(video_path)
except Exception as e:
scan_log.error(f"Error processing video {video_path}: {e}")
else:
time.sleep(1)
27 changes: 10 additions & 17 deletions src/burn/render_and_merge.py → src/burn/render_then_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import os
import subprocess
from src.config import GPU_EXIST, SRC_DIR
from src.burn.generate_danmakus import get_resolution, process_danmakus
from src.burn.generate_subtitles import generate_subtitles
from src.burn.render_video import render_video
from src.danmaku.generate_danmakus import get_resolution, process_danmakus
from src.subtitle.generate_subtitles import generate_subtitles
from src.burn.render_command import render_command
from src.upload.extract_video_info import get_video_info
from src.log.logger import scan_log

Expand All @@ -20,26 +20,26 @@ def normalize_video_path(filepath):
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]}"
return filepath.rsplit('/', 1)[0] + '/' + parts[0] + '_' + new_date_time + '-.mp4'

def merge_videos(in_final_video, title, artist, date, merge_list):
def merge_command(in_final_video, title, artist, date, merge_list):
"""Merge the video segments and preserve the first video's metadata
Args:
in_final_video: str, the path of videos will be merged
"""
merge_command = [
command = [
'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',
'-c', 'copy', in_final_video
]
try:
scan_log.info("Begin merging videos...")
result = subprocess.run(merge_command, check=True, capture_output=True, text=True)
result = subprocess.run(command, check=True, capture_output=True, text=True)
scan_log.debug(f"FFmpeg output: {result.stdout}")
if result.stderr:
scan_log.debug(f"FFmpeg debug: {result.stderr}")
except subprocess.CalledProcessError as e:
scan_log.error(f"Error: {e.stderr}")
subprocess.run(['rm', merge_list], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

def render_and_merge(video_path_list):
def render_then_merge(video_path_list):
title = ''
artist = ''
date = ''
Expand Down Expand Up @@ -72,7 +72,7 @@ def render_and_merge(video_path_list):
if GPU_EXIST:
generate_subtitles(original_video_path)
# Burn danmaku or subtitles into the videos
render_video(original_video_path, video_to_be_merged, subtitle_font_size, subtitle_margin_v)
render_command(original_video_path, video_to_be_merged, subtitle_font_size, subtitle_margin_v)
if not os.path.exists(merge_list):
open(merge_list, 'w').close()
with open(merge_list, 'a') as f:
Expand All @@ -87,15 +87,8 @@ def render_and_merge(video_path_list):
# test_path = original_video_path[:-4]
# os.rename(original_video_path, test_path)

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

with open(f"{SRC_DIR}/upload/uploadVideoQueue.txt", "a") as file:
file.write(f"{output_video_path}\n")

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Danmaku burns')
parser.add_argument('video_path', type=str, help='Path to the Video file')
args = parser.parse_args()
video_path = args.video_path
render_and_merge(video_path)
file.write(f"{output_video_path}\n")
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载