-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: auto slice videos fix #150 * refactor: add copyright * docs: update readme * refactor: adjust icons * style: adjust style * style: substitue icons
- Loading branch information
Showing
11 changed files
with
220 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright (c) 2024 bilive. | ||
|
||
import sys | ||
import os | ||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright (c) 2024 bilive. | ||
|
||
import re | ||
from collections import defaultdict | ||
from src.config import SLICE_DURATION | ||
|
||
def parse_time(time_str): | ||
"""Convert ASS time format to seconds with milliseconds.""" | ||
h, m, s = time_str.split(':') | ||
s, ms = s.split('.') | ||
return int(h) * 3600 + int(m) * 60 + int(s) + int(ms) / 100 | ||
|
||
def format_time(seconds): | ||
"""Format seconds to hh:mm:ss.xx.""" | ||
h = int(seconds // 3600) | ||
m = int((seconds % 3600) // 60) | ||
s = int(seconds % 60) | ||
ms = int((seconds - int(seconds)) * 100) | ||
return f"{h:02}:{m:02}:{s:02}.{ms:02}" | ||
|
||
def extract_dialogues(file_path): | ||
"""Extract dialogue start times from the ASS file.""" | ||
dialogues = [] | ||
with open(file_path, 'r', encoding='utf-8') as file: | ||
for line in file: | ||
if line.startswith('Dialogue:'): | ||
parts = line.split(',') | ||
start_time = parse_time(parts[1].strip()) | ||
dialogues.append(start_time) | ||
return dialogues | ||
|
||
def calculate_density(dialogues, window_size=SLICE_DURATION): | ||
"""Calculate the maximum density of dialogues in a given window size.""" | ||
time_counts = defaultdict(int) | ||
for time in dialogues: | ||
time_counts[time] += 1 | ||
|
||
max_density = 0 | ||
max_start_time = 0 | ||
|
||
# Use a sliding window to calculate density | ||
sorted_times = sorted(time_counts.keys()) | ||
for i in range(len(sorted_times)): | ||
start_time = sorted_times[i] | ||
end_time = start_time + window_size | ||
current_density = sum(count for time, count in time_counts.items() if start_time <= time < end_time) | ||
if current_density > max_density: | ||
max_density = current_density | ||
max_start_time = start_time | ||
|
||
return max_start_time, max_density |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright (c) 2024 bilive. | ||
|
||
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 | ||
|
||
def zhipu_glm_4v_plus_generate_title(video_path, artist): | ||
with open(video_path, 'rb') as video_file: | ||
video_base = base64.b64encode(video_file.read()).decode('utf-8') | ||
|
||
client = ZhipuAI(api_key=Your_API_KEY) | ||
response = client.chat.completions.create( | ||
model="glm-4v-plus", | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "video_url", | ||
"video_url": { | ||
"url" : video_base | ||
} | ||
}, | ||
{ | ||
"type": "text", | ||
"text": f"视频是{artist}的直播的切片,请根据该视频中的内容及弹幕信息,为这段视频起一个调皮并且吸引眼球的标题,注意标题中如果有“主播”请替换成{artist}。" | ||
} | ||
] | ||
} | ||
] | ||
) | ||
return response.choices[0].message.content.replace("《", "").replace("》", "") | ||
|
||
# https://stackoverflow.com/questions/64849478/cant-insert-stream-metadata-into-mp4 | ||
def inject_metadata(video_path, generate_title, output_path): | ||
"""Slice the video using ffmpeg.""" | ||
command = [ | ||
'ffmpeg', | ||
'-i', video_path, | ||
'-metadata:g', f'generate={generate_title}', | ||
'-c:v', 'copy', | ||
'-c:a', 'copy', | ||
output_path | ||
] | ||
subprocess.run(command) | ||
|
||
def slice_video(video_path, start_time, output_path, duration=f'00:00:{SLICE_DURATION}'): | ||
"""Slice the video using ffmpeg.""" | ||
command = [ | ||
'ffmpeg', | ||
'-ss', format_time(start_time), | ||
'-i', video_path, | ||
'-t', duration, | ||
'-map_metadata', '-1', | ||
'-c:v', 'copy', | ||
'-c:a', 'copy', | ||
output_path | ||
] | ||
subprocess.run(command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.