怎么提交网站,网站添加备案信息吗,开发公司宣传册,张槎网站制作文章目录 1. 项目简介2. 项目使用2.1 功能介绍2.2 使用Colab生成视频 3. 项目原理介绍 1. 项目简介
项目源码地址#xff1a;https://github.com/iioSnail/read_video_generation
我们经常在B站或其他视频网站上看到那种逐条读单词的视频#xff0c;但他们的视频多多少少和… 文章目录 1. 项目简介2. 项目使用2.1 功能介绍2.2 使用Colab生成视频 3. 项目原理介绍 1. 项目简介
项目源码地址https://github.com/iioSnail/read_video_generation
我们经常在B站或其他视频网站上看到那种逐条读单词的视频但他们的视频多多少少和我们的预期都不太一致。
然而网上很难找到和自己需求符合的视频。既然找不到那么我们就自己制作。虽然我不知道他们的视频是怎么做的但我们程序员可以用Python做。
使用Python生成的视频效果如下 【COCA20000词】词频背单词4001-4500 虽然目前有点丑但我会不断迭代 2. 项目使用
2.1 功能介绍
本项目可以很方便的生成读任何中、英文的视频若有需要也可以加入其他语言。用户只需要上传excel配置相应参数即可。 没有Python基础可参考 “2.2 节 使用Colab生成视频” 项目支持的详细参数如下 python gene_video.py -h
usage: gene_video.py [-h] [--filename FILENAME] [--repeat-times REPEAT_TIMES] [--interval INTERVAL] [--inner-interval INNER_INTERVAL] [--max-minutes MAX_MINUTES] [--video] [--no-video] [--background-color BACKGROUND_COLOR][--font-color FONT_COLOR] [--video-width VIDEO_WIDTH] [--video-height VIDEO_HEIGHT] [--max-font-size MAX_FONT_SIZE] [--cache-dir CACHE_DIR] [--output-dir OUTPUT_DIR]optional arguments:-h, --help show this help message and exit--filename FILENAME 单词文件的路径--repeat-times REPEAT_TIMES重复次数--interval INTERVAL 两个单词的间隔时间(ms)--inner-interval INNER_INTERVAL单词和释义的间隔时间(ms)--max-minutes MAX_MINUTES单个音频最大时长(分钟)--video 生成视频--no-video 不生成视频--add-volume 加减音量分贝。例如10是音量加10分贝-10是减10分贝--low-pass-filter 过滤高音部分护耳。例如8000表示过滤掉频率超过8k的频率--background-color BACKGROUND_COLOR视频背景色--font-color FONT_COLOR文字颜色--video-width VIDEO_WIDTH视频宽--video-height VIDEO_HEIGHT视频高--max-font-size MAX_FONT_SIZE最大字体大小--cache-dir CACHE_DIR生成的临时文件存放的目录--output-dir OUTPUT_DIR输出文件的目录假设我们的需求为① 读一遍中文、读一遍英文。② 屏幕上要显示例句但不读。③ 每个单词重复两遍。
假设我们需要生成读“单词例句”的视频我们需要遵循以下步骤
准备好我们的单词excel或csv例如
单词中文例句例句释义morning早上Good morning.早上好…………
配置好环境怕麻烦或不会弄可以参考2.2节使用Colab生成视频安装相关依赖。
pip install -r requirements.txt安装ffmpeg代码的部分库需要依赖它。 执行命令生成视频。
python gene_video.py --filename ./samples/xxx.xlsx \--read-columns 单词,中文 \--show-columns 单词,中文,例句,例句释义 \--repeat-times 22.2 使用Colab生成视频
相比本地部署我推荐使用Colab进行视频生成。 Colab是一个Google的AI训练平台提供Python运行环境。在不使用GPU的情况下基本上可以免费无限使用。 主要理由有
本地部署麻烦使用Colab可以免部署使用Colab可以挂载Google Drive方便保存视频。项目需要用到Google Translation进行文本转语音本地部署可能访问不了。
使用Colab只需要访问该链接然后更改你需要的参数运行即可。 需要想办法上Google。网络也要稳定中间如果断网较长时间则生成会被终止。 3. 项目原理介绍
本项目生成视频的流程如下 #mermaid-svg-1XyeWIyIV2iUU55h {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-1XyeWIyIV2iUU55h .error-icon{fill:#552222;}#mermaid-svg-1XyeWIyIV2iUU55h .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-1XyeWIyIV2iUU55h .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-1XyeWIyIV2iUU55h .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-1XyeWIyIV2iUU55h .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-1XyeWIyIV2iUU55h .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-1XyeWIyIV2iUU55h .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-1XyeWIyIV2iUU55h .marker{fill:#333333;stroke:#333333;}#mermaid-svg-1XyeWIyIV2iUU55h .marker.cross{stroke:#333333;}#mermaid-svg-1XyeWIyIV2iUU55h svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-1XyeWIyIV2iUU55h .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-1XyeWIyIV2iUU55h .cluster-label text{fill:#333;}#mermaid-svg-1XyeWIyIV2iUU55h .cluster-label span{color:#333;}#mermaid-svg-1XyeWIyIV2iUU55h .label text,#mermaid-svg-1XyeWIyIV2iUU55h span{fill:#333;color:#333;}#mermaid-svg-1XyeWIyIV2iUU55h .node rect,#mermaid-svg-1XyeWIyIV2iUU55h .node circle,#mermaid-svg-1XyeWIyIV2iUU55h .node ellipse,#mermaid-svg-1XyeWIyIV2iUU55h .node polygon,#mermaid-svg-1XyeWIyIV2iUU55h .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-1XyeWIyIV2iUU55h .node .label{text-align:center;}#mermaid-svg-1XyeWIyIV2iUU55h .node.clickable{cursor:pointer;}#mermaid-svg-1XyeWIyIV2iUU55h .arrowheadPath{fill:#333333;}#mermaid-svg-1XyeWIyIV2iUU55h .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-1XyeWIyIV2iUU55h .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-1XyeWIyIV2iUU55h .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-1XyeWIyIV2iUU55h .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-1XyeWIyIV2iUU55h .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-1XyeWIyIV2iUU55h .cluster text{fill:#333;}#mermaid-svg-1XyeWIyIV2iUU55h .cluster span{color:#333;}#mermaid-svg-1XyeWIyIV2iUU55h div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-1XyeWIyIV2iUU55h :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 读取Excel 生成图片 生成视频 文本转语音 生成音频 合成视频与音频 该流程中主要用到了以下库
pandas用于读取Excel、CSV等PIL内置库用于生成图片pydub用于处理音频cv2用于生成视频moviepy用于处理视频
读取文件关键代码
import pandas as pd# 使用pandas读取excel文件
if self.args.filename.endswith(.xlsx) or .endswith(.xls):data pd.read_excel(self.args.filename, dtypestr)
elif self.args.filename.endswith(.csv):data pd.read_csv(self.args.filename, dtypestr)# 逐行遍历将需要用到的数据放入data_list
data_list []
for i, row in data.iterrows():...生成图片关键代码
from PIL import Image, ImageDraw, ImageFontfont_file ./assets/font.TTF # 加载字体文件
font_size self._auto_font_size(text, font_file) # 自动尝试合适的字体大小详见源码
font ImageFont.truetype(font_file, font_size)width, height self.args.video_width, self.args.video_height
line_spacing 50 # 行间距
image Image.new(RGB, (width, height), self.args.background_color) # 新建图片
draw ImageDraw.Draw(image) # 绘制图片# 定义一个绘制多行文本的box框
bbox draw.multiline_textbbox((0, 0, width, height), text, fontfont)# 第一行文本的y坐标
text_y (height - bbox[3]) // len(lines)# 逐行绘制文字
lines text.split(\n)
for line in lines:line_bbox draw.textbbox((0, 0, width, height), line, font)line_width, line_height line_bbox[2], line_bbox[3]draw.text(((width - line_width) // 2, text_y), line, fontfont, fillself.args.font_color)text_y line_height line_spacing # Move to the next lineimage.save(cache_file)生成视频的关键代码
frame_size 10 # 每秒10帧
width 1920 # 视频尺寸为1920x1080
height 1080
video cv2.VideoWriter(str(video_file), cv2.VideoWriter_fourcc(m, p, 4, v), frame_size, (width, height))frame int(duration / 1000 * frame_size) # 计算对某张图片要写入的帧数image cv2.imread(str(image))
for _ in range(frame):video.write(image)video.release()文本转语音关键代码
from gtts import gTTSdef has_chinese(content: str):for char in content:if \u4e00 char \u9fff:return Truereturn Falsecontent hello # 要读的内容
if has_chinese(content): # 判断内容内容中是否包含中文lang zh
else:lang entts gTTS(content, langlang, tldus) # tld为地区即用哪个地区的口音默认用us
tts.save(XXX.mp3) # 保存的路径将若干单词音频合成音频并调节相关参数
from pydub import AudioSegment# 加载音频
audio AudioSegment.from_mp3(XXX.mp3)
# 在该单词的音频前后各增加100ms的渐进
audio audio.fade_in(100).fade_out(100)
# 增加一段无声的间隔用于插入两个单词之间
interval AudioSegment.silent(duration500, frame_rateaudio.frame_rate)
# XXX.mp3读两遍中间增加500ms的无声音频
audio audio interval audio
# 过滤掉3000hz以上的高频部分这样耳朵听起来舒服一些
audio audio.low_pass_filter(3000)
# 导出wav格式
audio.export(str(output.wav), format(wav))将视频和音频和起来的关键代码
from moviepy.editor import VideoFileClip, AudioFileClipvideo VideoFileClip(video) # 加载视频
audio AudioFileClip(audio) # 加载音频
video video.set_audio(audio) # 设置视频的音频
# 输出文件
video.write_videofile(str(output_path), fps10, threadscpu_count(), loggerNone)
video.close()详细代码请参考源码https://github.com/iioSnail/read_video_generation