Fix support for Windows systems

This commit is contained in:
harry
2024-03-14 13:49:02 +08:00
parent f55ea42a07
commit 9650def321
6 changed files with 26 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
import os import os
import socket
import tomli import tomli
from loguru import logger from loguru import logger
@@ -12,8 +12,7 @@ with open(config_file, mode="rb") as fp:
app = _cfg.get("app", {}) app = _cfg.get("app", {})
whisper = _cfg.get("whisper", {}) whisper = _cfg.get("whisper", {})
hostname = socket.gethostname()
hostname = os.uname().nodename
log_level = _cfg.get("log_level", "DEBUG") log_level = _cfg.get("log_level", "DEBUG")
listen_host = _cfg.get("listen_host", "0.0.0.0") listen_host = _cfg.get("listen_host", "0.0.0.0")
@@ -23,6 +22,10 @@ project_description = _cfg.get("project_description", "MoneyPrinterTurbo\n by
project_version = _cfg.get("project_version", "1.0.0") project_version = _cfg.get("project_version", "1.0.0")
reload_debug = False reload_debug = False
imagemagick_path = app.get("imagemagick_path", "")
if imagemagick_path and os.path.isfile(imagemagick_path):
os.environ["IMAGEMAGICK_BINARY"] = imagemagick_path
__cfg = { __cfg = {
"hostname": hostname, "hostname": hostname,
"listen_host": listen_host, "listen_host": listen_host,

View File

@@ -6,13 +6,13 @@ from timeit import default_timer as timer
from loguru import logger from loguru import logger
from app.config import config from app.config import config
from app.models import const
from app.utils import utils from app.utils import utils
model_size = config.whisper.get("model_size", "large-v3") model_size = config.whisper.get("model_size", "large-v3")
device = config.whisper.get("device", "cpu") device = config.whisper.get("device", "cpu")
compute_type = config.whisper.get("compute_type", "int8") compute_type = config.whisper.get("compute_type", "int8")
if config.app.get("subtitle_provider") == "whisper":
model = WhisperModel(model_size_or_path=model_size, device=device, compute_type=compute_type) model = WhisperModel(model_size_or_path=model_size, device=device, compute_type=compute_type)

View File

@@ -17,9 +17,7 @@ def get_bgm_file(bgm_name: str = "random"):
if bgm_name == "random": if bgm_name == "random":
suffix = "*.mp3" suffix = "*.mp3"
song_dir = utils.song_dir() song_dir = utils.song_dir()
# 使用glob.glob获取指定扩展名的文件列表
files = glob.glob(os.path.join(song_dir, suffix)) files = glob.glob(os.path.join(song_dir, suffix))
# 使用random.choice从列表中随机选择一个文件
return random.choice(files) return random.choice(files)
file = os.path.join(utils.song_dir(), bgm_name) file = os.path.join(utils.song_dir(), bgm_name)
@@ -154,11 +152,12 @@ def generate_video(video_path: str,
if not font_name: if not font_name:
font_name = "STHeitiMedium.ttc" font_name = "STHeitiMedium.ttc"
font_path = os.path.join(utils.font_dir(), font_name) font_path = os.path.join(utils.font_dir(), font_name)
if os.name == 'nt':
font_path = font_path.replace("\\", "/")
logger.info(f"using font: {font_path}") logger.info(f"using font: {font_path}")
# 自定义的生成器函数,包含换行逻辑
def generator(txt): def generator(txt):
# 应用自动换行
wrapped_txt = wrap_text(txt, max_width=video_width - 100, wrapped_txt = wrap_text(txt, max_width=video_width - 100,
font=font_path, font=font_path,
fontsize=fontsize) # 调整max_width以适应你的视频 fontsize=fontsize) # 调整max_width以适应你的视频
@@ -179,16 +178,14 @@ def generate_video(video_path: str,
clips = [ clips = [
VideoFileClip(video_path), VideoFileClip(video_path),
# subtitles.set_position(lambda _t: ('center', position_height))
] ]
# Burn the subtitles into the video
if subtitle_path and os.path.exists(subtitle_path): if subtitle_path and os.path.exists(subtitle_path):
subtitles = SubtitlesClip(subtitle_path, generator) subtitles = SubtitlesClip(subtitles=subtitle_path, make_textclip=generator, encoding='utf-8')
clips.append(subtitles.set_position(lambda _t: ('center', position_height))) clips.append(subtitles.set_position(lambda _t: ('center', position_height)))
result = CompositeVideoClip(clips) result = CompositeVideoClip(clips)
# Add the audio
audio = AudioFileClip(audio_path) audio = AudioFileClip(audio_path)
result = result.set_audio(audio) result = result.set_audio(audio)
@@ -210,10 +207,10 @@ def generate_video(video_path: str,
video_clip = video_clip.set_audio(comp_audio) video_clip = video_clip.set_audio(comp_audio)
video_clip = video_clip.set_fps(30) video_clip = video_clip.set_fps(30)
video_clip = video_clip.set_duration(original_duration) video_clip = video_clip.set_duration(original_duration)
# 编码为aac否则iPhone里面无法播放
logger.info(f"encoding audio codec to aac") logger.info(f"encoding audio codec to aac")
video_clip.write_videofile(output_file, audio_codec="aac", threads=threads) video_clip.write_videofile(output_file, audio_codec="aac", threads=threads)
# delete the temp file
os.remove(temp_output_file) os.remove(temp_output_file)
logger.success(f"completed") logger.success(f"completed")

View File

@@ -15,6 +15,11 @@
# If empty, the subtitle will not be generated # If empty, the subtitle will not be generated
subtitle_provider = "edge" subtitle_provider = "edge"
# Once you have installed it, ImageMagick will be automatically detected, except on Windows!
# On Windows, for example "C:\Program Files (x86)\ImageMagick-7.1.1-Q16-HDRI\magick.exe"
# Download from https://imagemagick.org/archive/binaries/ImageMagick-7.1.1-29-Q16-x64-static.exe
# imagemagick_path = "C:\\Program Files (x86)\\ImageMagick-7.1.1-Q16\\magick.exe"
[whisper] [whisper]
# Only effective when subtitle_provider is "whisper" # Only effective when subtitle_provider is "whisper"

View File

@@ -1,5 +1,5 @@
requests~=2.31.0 requests~=2.31.0
moviepy~=1.0.3 moviepy~=2.0.0.dev2
openai~=1.13.3 openai~=1.13.3
faster-whisper~=1.0.1 faster-whisper~=1.0.1
edge_tts~=6.1.10 edge_tts~=6.1.10

5
webui.bat Normal file
View File

@@ -0,0 +1,5 @@
@echo off
set CURRENT_DIR=%CD%
echo ***** Current directory: %CURRENT_DIR% *****
set PYTHONPATH=%CURRENT_DIR%;%PYTHONPATH%
streamlit run .\webui\Main.py