一、视频转帧
新建python文件:video2images_demo.py
import cv2
import os
import argparse
def video2imgs(videoPath, imgPath):
if not os.path.exists(imgPath):
os.makedirs(imgPath) # 目标文件夹不存在,则创建
cap = cv2.VideoCapture(videoPath) # 获取视频
judge = cap.isOpened() # 判断是否能打开成功
print(judge)
fps = cap.get(cv2.CAP_PROP_FPS) # 帧率,视频每秒展示多少张图片
print('fps:',fps)
frames = 1 # 用于统计所有帧数
count = 1 # 用于统计保存的图片数量
while(judge):
flag, frame = cap.read() # 读取每一张图片 flag表示是否读取成功,frame是图片
if not flag:
print(flag)
print("Process finished!")
break
else:
# if frames % 10 == 0: # 每隔10帧抽一张
imgname = 'jpgs_' + str(count).rjust(3,'0') + ".jpg"
newPath = imgPath + imgname
print(imgname)
cv2.imwrite(newPath, frame, [cv2.IMWRITE_JPEG_QUALITY, 100])
# cv2.imencode('.jpg', frame)[1].tofile(newPath)
count += 1
frames += 1
cap.release()
print("共有 %d 张图片"%(count-1))
if __name__ == "__main__":
parser = argparse.ArgumentParser("Video-to-Images Demo", add_help=True)
parser.add_argument("--video_path", type=str, required=True, help="path to video file")
parser.add_argument(
"--image_path", type=str, required=True, help="path to image file"
)
args = parser.parse_args()
video2imgs(args.video_path,args.image_path)
执行:
python video2images_demo.py \
--video_path /Users/zhaoyongfei/work/MyProject/video.mp4 \
--image_path /Users/zhaoyongfei/work/MyProject/frames/
二、帧转视频
import os
import cv2
from PIL import Image
def image_to_video(image_path, media_path):
'''
图片合成视频函数
:param image_path: 图片路径
:param media_path: 合成视频保存路径
:return:
'''
# 获取图片路径下面的所有图片名称
image_names = [img for img in os.listdir(image_path) if not img.startswith(".")]
# 对提取到的图片名称进行排序
# image_names.sort(key=lambda x: int(x.split('_')[0][5:]))
image_names.sort(key=lambda x: int(x.split('_')[1].split('.')[0]))
# 设置写入格式
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
# 设置每秒帧数
fps = 30 # 由于图片数目较少,这里设置的帧数比较低
# 读取第一个图片获取大小尺寸,因为需要转换成视频的图片大小尺寸是一样的
image = Image.open(image_path + image_names[0])
# 初始化媒体写入对象
media_writer = cv2.VideoWriter(media_path, fourcc, fps, image.size)
# 遍历图片,将每张图片加入视频当中
for image_name in image_names:
im = cv2.imread(os.path.join(image_path, image_name))
media_writer.write(im)
print(image_name, '合并完成!')
# 释放媒体写入对象
media_writer.release()
print('无声视频写入完成!')
#save_file_list.sort(key=lambda x: int(x.split('_')[0][5:]))
# 图片路径
#image_path = "/root/autodl-tmp/stable-diffusion-webui/video4/"
image_path = "/Users/zhaoyongfei/work/MyProject/frames/"
# 视频保存路径+名称
media_path = "/Users/zhaoyongfei/work/MyProject/new_video.mp4"
# 调用函数,生成视频
image_to_video(image_path, media_path)
三、提取音频 + 音频视频合成
安装插件: pip install moviepy
提取音频
from moviepy.editor import VideoFileClip
def audio_extraction(video_path, audio_path):
video = VideoFileClip(video_path)
audio = video.audio
audio.write_audiofile (audio_path)
video_path = "/Users/zhaoyongfei/work/MyProject/video.mp4"
audio_path = "/Users/zhaoyongfei/work/MyProject/audio.mp3"
audio_extraction(video_path, audio_path)
音频与视频合成
from moviepy.editor import VideoFileClip, AudioFileClip
def video_audio_synthesis(video_path, audio_path, output_path):
# Open the video and audio
video_clip = VideoFileClip(video_path)
audio_clip = AudioFileClip(audio_path)
# Concatenate the video clip with the audio clip
final_clip = video_clip.set_audio(audio_clip)
print(video_clip.audio, 111)
# Export the final video with audio
final_clip.write_videofile(output_path, audio_codec="aac")
video_path = "/Users/zhaoyongfei/work/MyProject/new_video.mp4"
audio_path = "/Users/zhaoyongfei/work/MyProject/audio.mp3"
output_path = "/Users/zhaoyongfei/work/MyProject/merge_video.mp4"
video_audio_synthesis(video_path, audio_path, output_path)
提示:电脑端和安卓端可以正常播放,苹果手机和Mac没有声音
解决方法:改变下声道编码
在调用write_videofile函数时加上参数:audio_codec=“aac”