前提:电脑本地部署gfpgan项目
利用moviepy将视频关键帧提取出来
def gif_to_png(_file_name, _output):
from moviepy.editor import VideoFileClip
# Load your gif
clip = VideoFileClip(_file_name)
print(f'Duration: {clip.duration} FPS: {clip.fps}')
# Loop over clip frames
for i, frame in enumerate(clip.iter_frames()):
from PIL import Image
img = Image.fromarray(frame)
img.save(f'{_output}/frame_{i}.png')
if __name__ == '__main__':
file_name = "E:\project\demo_1.mp4"
output = "E:\project\GFPGAN-master\GFPGAN-master\inputs\whole_imgs"
gif_to_png(file_name, output)
这里的gif可以改成MP4,已经自己试过了
然后就是将截取的帧图片放入gfpgan中进行超分
最后将帧图片合成为视频
from moviepy.editor import ImageSequenceClip, AudioFileClip
import os
from moviepy.video.io.VideoFileClip import VideoFileClip
def video_to_audio(video_path, audio_path):
# 从原始视频中提取音频
video = VideoFileClip(video_path)
video.audio.write_audiofile(audio_path)
video.close()
def images_to_video_with_audio(image_folder, audio_path, output_file, fps):
# 获取文件夹中所有的图片文件名
image_files = [img for img in os.listdir(image_folder) if img.endswith(".png") or img.endswith(".jpg")]
def sort_images_by_number(filename):
# 分离文件名和扩展名
name, ext = os.path.splitext(filename)
# 提取数字部分
number_part = name.split('_')[-1]
# 将数字部分转换为整数
return int(number_part)
# 对图片文件名进行排序
image_files.sort(key=sort_images_by_number)
# 创建一个 ImageSequenceClip 对象,该对象会按顺序加载图片并创建视频
clip = ImageSequenceClip([os.path.join(image_folder, img) for img in image_files], fps=fps)
# 加载音频文件
audio = AudioFileClip(audio_path)
# 设置视频的音频
clip = clip.set_audio(audio)
# 写入视频文件
clip.write_videofile(output_file)
if __name__ == '__main__':
# 图片所在的文件夹路径
input_dir = "E:/project/GFPGAN-master/GFPGAN-master/results/restored_imgs"
video_path = "E:/project/demo_1.mp4"
# 音频文件路径
audio_path = "E:/project/audio/background_music.mp3"
# 输出视频文件名
output = "outvideo.mp4"
# 帧率 (Frames Per Second),降低此值可以使视频播放更慢
fps = 24 # 例如,从 24 减到 12 可以使视频播放速度减半
# 提取音频
video_to_audio(video_path, audio_path)
# 合成视频
images_to_video_with_audio(input_dir, audio_path, output, fps)