一、应用场景
测试数据生成:生成不同分辨率和格式的视频用于兼容性测试
以下是使用 Python 生成常见视频格式(MP4/AVI/MOV/FLV)的脚本,支持自定义分辨率、帧率、持续时间和视频内容(以纯色渐变为例)
from moviepy.editor import VideoClip
from moviepy.video.io.ImageSequenceClip import ImageSequenceClip
import numpy as np
import os
def generate_custom_video(
output_path: str,
resolution: tuple = (640, 480), # 宽x高
fps: int = 30,
duration: float = 10.0, # 视频时长(秒)
format: str = "mp4", # 支持格式:mp4/avi/mov/flv
color_start: tuple = (255, 0, 0), # 起始颜色(RGB)
color_end: tuple = (0, 255, 0) # 结束颜色(RGB)
):
"""
生成自定义参数的视频文件
:param output_path: 输出文件路径(含扩展名)
:param resolution: 视频分辨率(宽, 高)
:param fps: 帧率(帧/秒)
:param duration: 视频时长(秒)
:param format: 输出格式(mp4/avi/mov/flv)
:param color_start: 起始颜色(RGB元组,0-255)
:param color_end: 结束颜色(RGB元组,0-255)
"""
width, height = resolution
total_frames = int(duration * fps)
# 生成渐变帧的函数
def make_frame(t):
frame_index = int(t * fps)
# 计算颜色渐变(线性插值)
r = int(color_start[0] + (color_end[0] - color_start[0]) * frame_index / total_frames)
g = int(color_start[1] + (color_end[1] - color_start[1]) * frame_index / total_frames)
b = int(color_start[2] + (color_end[2] - color_start[2]) * frame_index / total_frames)
return np.full((height, width, 3), (b, g, r), dtype=np.uint8) # RGB转BGR
# 创建视频剪辑
video_clip = VideoClip(make_frame, duration=duration)
# 保存视频(自动处理格式)
video_clip.write_videofile(
output_path,
fps=fps,
codec="libx264"if format == "mp4"elseNone, # MP4使用H.264编码
verbose=False,
logger=None
)
print(f"视频生成完成:{output_path}")
print(f"参数:分辨率={width}x{height}, 帧率={fps}fps, 时长={duration}s")
# 示例用法
if __name__ == "__main__":
# 生成参数配置
configs = [
{
"output_path": "output_video.mp4",
"resolution": (1280, 720), # 720P
"fps": 25,
"duration": 15.0,
"format": "mp4"
},
{
"output_path": "output_video.avi",
"resolution": (640, 480), # 标清
"fps": 30,
"duration": 10.0,
"format": "avi"
},
{
"output_path": "output_video.mov",
"resolution": (1920, 1080), # 1080P
"fps": 60,
"duration": 5.0,
"format": "mov"
},
{
"output_path": "output_video.flv",
"resolution": (480, 360), # 低分辨率
"fps": 15,
"duration": 20.0,
"format": "flv"
}
]
for config in configs:
generate_custom_video(**config)
二、功能特性
1. 多分辨率支持
常见分辨率:240p(320x240)、360p(480x360)、720p(1280x720)、1080p(1920x1080)、4K(3840x2160)
自定义分辨率:可扩展添加任意 (宽,高) 组合
2. 多格式输出
主流格式:MP4、AVI、MOV、FLV(可扩展支持 MKV、WebM 等)
编解码器 MP4:H.264(libx264)、AVI:MPEG-4、MOV:QuickTime 格式(H.264 编码)、FLV:Sorenson H.263
3. 可控参数
参数名 | 说明 |
---|---|
fps | 帧率(常见值:24/25/30/60) |
duration | 视频时长(秒,控制文件大小,建议 5-10 秒用于测试) |
bitrate | 码率(如 "2000k",数值越大文件越大,默认 2000kbps) |
三、环境准备
安装依赖
pip install moviepy imageio ffmpeg-python
安装 FFmpeg:
自动安装:首次运行脚本时,moviepy 会提示下载 FFmpeg(约 30MB)
手动安装:从 FFmpeg 官网 下载并添加到系统环境变量
四、扩展功能(高级用法)
1. 生成渐变颜色视频(用于视觉测试)
def generate_gradient_video(resolution, output_path, duration=5.0):
"""生成颜色渐变视频(红→绿→蓝)"""
width, height = resolution
def make_frame(t):
r = int(255 * abs(np.sin(t * 2))) # 红色通道渐变
g = int(255 * abs(np.sin(t * 2 + 2.094))) # 绿色通道相位偏移
b = int(255 * abs(np.sin(t * 2 + 4.188))) # 蓝色通道相位偏移
return np.full((height, width, 3), (b, g, r), dtype=np.uint8)
video_clip = VideoClip(make_frame, duration=duration)
video_clip.write_videofile(output_path, fps=30)
2. 生成带文本的测试视频(用于字幕兼容性测试)
from moviepy.video.components.TextClip import TextClip
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
def generate_text_video(resolution, output_path):
"""生成带测试文本的视频"""
width, height = resolution
text = f"Resolution: {width}x{height}\nFormat: {format}\nFPS: {fps}"
text_clip = TextClip(
text,
fontsize=24,
color="white",
bg_color="black",
font="Arial",
method="caption"
).set_duration(5).set_pos(("center", "center"))
bg_clip = ColorClip(resolution, color=(0, 0, 0)).set_duration(5)
final_clip = CompositeVideoClip([bg_clip, text_clip])
final_clip.write_videofile(output_path, fps=30)
3. 控制文件大小(通过码率调整)
# 低码率(适合移动设备测试)
generate_test_video(bitrate="500k")
# 高码率(适合高清设备测试)
generate_test_video(bitrate="10000k")
五、兼容性测试场景建议
设备兼容性:
在手机(低分辨率)、平板(中分辨率)、PC(全高清)、4K 电视上播放生成的视频
测试不同设备对帧率的支持(如 60fps 在老旧设备上的播放流畅度)
播放器测试:
系统自带播放器(Windows Media Player、QuickTime)
第三方播放器(VLC、PotPlayer)
浏览器内置播放器(Chrome、Firefox 对 MP4/FLV 的支持)
平台兼容性:
Windows、macOS、Linux 系统下的文件识别
流媒体平台(如 YouTube 对 MOV 格式的支持)
编解码测试:
测试设备是否支持视频的编解码器(如 H.264、MPEG-4)
检查是否出现花屏、无法播放等问题
以上就是用Ai赋能测试使用脚本可生成覆盖主流分辨率和格式的测试视频,适用于设备兼容性、播放器兼容性、编解码支持等多种测试场景。通过调整参数配置,可快速生成符合特定需求的测试数据集。
领取全栈软件测试工程师学习资料
添加下方小编微信备注"资料"
分享、点赞支持持续输出