目录
imageio替代pyav
imageio替代pyav
import imageio
import numpy as np
import torch# 创建一个随机的图像张量,形状为 (N, C, H, W)
# 这里 N = 30(帧数),C = 3(通道数),H = 64(高度),W = 64(宽度)
N, C, H, W = 30, 3, 64, 64
tensor_data = torch.rand(N, C, H, W) # 随机生成数据# 将张量转换为 NumPy 数组,并调整维度为 (N, H, W, C)
tensor_data = tensor_data.permute(0, 2, 3, 1).numpy()# 设置输出视频文件路径和帧率
images_path = 'output_video.mp4'
fps = 8# 使用 imageio 写入视频
with imageio.get_writer(images_path, fps=fps, codec='h264', ffmpeg_log_level='error') as writer:for frame in tensor_data:writer.append_data((frame * 255).astype(np.uint8)) # 将数据转换为 uint8 类型print(f"视频已保存至 {images_path}")