使用“Type Byte🐧”创建动态文本视频
Type Byte工作原理
Type Byte通过用户友好的界面和强大的后端技术,简化了将文本(单词和短语)转换为视频的过程。以下是它的工作原理:在此处查看空间Type Byte。
1. 合成过程
Type Byte的核心是合成过程。该工具将音效(SFX)与文本结合,让您创建视觉和听觉元素的和谐融合。该过程涉及几个关键步骤
帧和文本颜色:您首先选择背景(帧)颜色和文本颜色。这确保您的文本脱颖而出并与您的品牌美学保持一致。
文本序列:Type Byte对您的文本进行动画处理,逐行或逐段呈现,创建引人注目的打字效果。
音效:添加音效可以增强观看体验,使您的视频更具沉浸感。
2. 自定义选项
Type Byte提供一系列自定义选项,确保您的视频根据您的特定需求量身定制。您可以从各种选项中选择
特性 | 描述 |
---|---|
文本格式 | - 段落:传统文本块。 - 编程风格:带有缩进的代码格式。 |
行间距 | 调整行间距以提高可读性。 |
字体 | 各种字体以匹配您的品牌。 |
帧和文本颜色 | 背景和文本均有多种颜色选项。 |
打字音效 | 不同的音效模拟各种打字声音。 |
下面是合成过程的视觉表示,说明了不同元素如何组合在一起创建打字视频。
3. 先决条件
在深入代码之前,请确保您已安装以下内容
- Python 3.x
- Gradio:`pip install gradio`
- OpenCV:`pip install opencv-python`
- Pillow:`pip install Pillow`
- MoviePy:`pip install moviepy`
moviepy.video.fx (vfx)
moviepy.video.fx模块重新组合了旨在与videoclip.fx()一起使用的函数。
对于所有其他修改,我们使用clip.fx和clip.fl。clip.fx旨在简化使用已编写的转换函数,而clip.fl则简化了编写新的转换函数。
此外,请确保您有一系列字体和音效可供视频使用。(或) 可从外部上传。
4. 环境设置
首先,我们创建一个Python脚本,设置Gradio界面和生成打字视频所需的函数。
import gradio as gr
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import textwrap
import moviepy.editor as mp
import moviepy.video.fx.all as vfx
5. 设计打字视频生成器
我们定义了一个函数`create_typing_video`,它接受多个输入,例如要显示的文本、格式选项、字体、视频尺寸和音频设置。此函数将处理视频帧的创建并应用任何所需的效果,包括声音和速度调整。
6. 自定义文本显示
def create_typing_video(code_text, format_choice,
line_spacing,
width_choice,
height_choice,
font_name="arial.ttf",
font_size=18, frame_rate=10,
sound_choice=None,
custom_audio=None,
background_color="black",
text_color="white",
enhance_quality=False,
video_speed="1.0"):
font_path = f"font/{font_name}"
font_size = int(font_size)
font = ImageFont.truetype(font_path, font_size)
video_frames = []
image_width, image_height = int(width_choice), int(height_choice)
max_width = image_width - 40 # Margin of 20 pixels on each side
current_text = ""
7. 创建视频帧
我们一次创建一个字符的帧,根据需要对文本进行换行,并动态调整字体大小以使文本适合视频帧。每个帧都以图像形式存储,并使用OpenCV转换为视频。
while True:
wrapped_lines = textwrap.wrap(code_text, width=max_width // font.getlength(' '))
text_height = sum([font.getbbox(line)[3] - font.getbbox(line)[1] for line in wrapped_lines])
if text_height <= image_height - 40:
break
font_size -= 1
font = ImageFont.truetype(font_path, font_size)
for char in code_text:
current_text += char
if format_choice == "Paragraph":
wrapped_lines = textwrap.wrap(current_text, width=max_width // font.getlength(' '))
else:
wrapped_lines = current_text.splitlines()
image = background.copy()
draw = ImageDraw.Draw(image)
y_position = 20
for line in wrapped_lines:
draw.text((20, y_position), line, font=font, fill=text_color)
line_height = font.getbbox(line)[3] - font.getbbox(line)[1]
y_position += line_height * line_spacing
frame = np.array(image)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
video_frames.append(frame)
8. 添加音频和增强功能
该函数允许您从预定义的打字声音中选择,或上传自定义音频文件以在视频上循环播放。我们还包含一个选项,可以通过调整大小和颜色校正来提高视频质量。
if sound_choice and sound_choice != "No Sound":
video = mp.VideoFileClip(video_filename)
audio = mp.AudioFileClip(f"type-sounds/{sound_choice}")
audio = audio.fx(mp.afx.audio_loop, duration=video.duration)
video = video.set_audio(audio)
video.write_videofile("typed_code_video_with_sound.mp4", codec="libx264")
video_filename = "typed_code_video_with_sound.mp4"
if custom_audio:
video = mp.VideoFileClip(video_filename)
audio = mp.AudioFileClip(custom_audio)
audio = audio.fx(mp.afx.audio_loop, duration=video.duration)
video = video.set_audio(audio)
video.write_videofile("typed_code_video_with_custom_audio.mp4", codec="libx264")
video_filename = "typed_code_video_with_custom_audio.mp4"
if enhance_quality:
video = mp.VideoFileClip(video_filename)
video = video.fx(vfx.resize, height=720)
video = video.fx(vfx.colorx, 1.2)
video.write_videofile("enhanced_" + video_filename, codec="libx264")
video_filename = "enhanced_" + video_filename
9. 构建Gradio界面
核心功能到位后,我们构建Gradio界面,为用户提供与应用程序交互的简单方式。我们允许自定义文本格式、行间距、字体、视频大小、速度和声音。
iface = gr.Interface(
fn=generate_video,
inputs=[
gr.Textbox(label="Enter Content", lines=10, placeholder="Enter the text to be displayed in the video..."),
format_choice,
line_spacing,
width_choice,
height_choice,
video_speed,
font_choice,
font_size,
sound_choice,
custom_audio,
background_color,
text_color,
enhance_quality,
],
outputs=gr.Video(label="Typing Video"),
title="Type Byte🐧",
css=css,
theme="bethecloud/storj_theme",
)
10. 示例
输入内容
Hugging Face, Inc. is an American company incorporated under the Delaware General
Corporation Law and based in New York City that develops computation tools for
building applications using machine learning.
MoviePy depends on the Python modules Numpy, imageio, Decorator, and tqdm, which will be
automatically installed during MoviePy’s installation. The software FFMPEG should be automatically
downloaded/installed (by imageio) during your first use of MoviePy (installation will take a few seconds).
If you want to use a specific version of FFMPEG, follow the instructions in config_defaults.py.
In case of trouble, provide feedback.
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
示例1
示例2
示例3
如果内容超出,请展开帧“高度”🎞️
11. 总结
通过遵循本文中概述的步骤,您已经创建了一个功能强大且可自定义的打字视频生成器。这个Gradio应用程序让您轻松创建专业质量的视频,非常适合展示您的内容。该应用程序的灵活性确保它可以根据各种用例进行定制,无论您是在编码、写作还是创建动态演示。
- 文章结束,感谢阅读 🤗!.
试用!
| 在线演示 | Type-Byte | | GitHub | Type-Byte | | Hugging Face | prithivMLmods |