gpt4 book ai didi

python - 如何使用 MoviePy 批量处理视频文件夹

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:00 25 4
gpt4 key购买 nike

我编写了一个 MoviePy 脚本,它接受输入视频,进行一些处理,然后输出视频文件。我想在整个视频文件夹中运行它。感谢任何帮助或指导。

这是我尝试过的...

for f in *; do python resize.py $f; done

和resize.py源代码在这里:

from moviepy.editor import *

clip = VideoFileClip(input)

clip1 = clip.rotate(270)

clip2 = clip1.crop(x_center=540,y_center=960,width=1080,height=608)

clip3 = clip2.resize(width=1920)

clip3.write_videofile(output,codec='libx264')

真的不确定在我的 .py 文件中为“输入”和“输出”添加什么。

谢谢,埃文

最佳答案

我知道你有答案 on Github ,但我会添加我自己的解决方案。

首先,您需要将代码放入函数中:

def process_video(input):
"""Parameter input should be a string with the full path for a video"""

clip = VideoFileClip(input, output)

clip1 = clip.rotate(270)

clip2 = clip1.crop(x_center=540,y_center=960,width=1080,height=608)

clip3 = clip2.resize(width=1920)

clip3.write_videofile(output,codec='libx264')

然后,您可以使用一个返回文件路径列表和最终文件名列表的函数来与上述函数一起使用(请注意,最终文件名将与原始文件名相同,但带有“输出”在前面):

import os
def get_video_paths(folder_path):
"""
Parameter folder_path should look like "Users/documents/folder1/"
Returns a list of complete paths
"""
file_name_list = os.listdir(folder_path)

path_name_list = []
final_name_list = []
for name in file_name_list:
# Put any sanity checks here, e.g.:
if name == ".DS_Store":
pass
else:
path_name_list.append(folder_path + name)
# Change the format of the output file names below
final_name_list.append(folder_path + "output" + name)
return path_name_list, final_name_list

最后,在底部,我们得到输入文件夹,并利用以上两个函数:

if __name__ == "__main__":
video_folder = input("What folder would you like to process? ")
path_list, final_name_list = get_video_paths(video_folder)
for path, name in zip(path_list, final_name_list):
process_video(path, name)
print("Finished")

请小心,因为如果文件夹中有任何无法作为电影读取的文件,这将会崩溃。例如,在Mac上,操作系统在每个文件夹中放置一个“.DS_Store”文件,这将使程序崩溃。我设置了一个区域进行健全性检查以忽略某些文件名。

完整代码:

import os

from moviepy.editor import *


def process_video(input, output):
"""Parameter input should be a string with the full path for a video"""

clip = VideoFileClip(input)

clip1 = clip.rotate(270)

clip2 = clip1.crop(x_center=540,y_center=960,width=1080,height=608)

clip3 = clip2.resize(width=1920)

clip3.write_videofile(output,codec='libx264')


def get_video_paths(folder_path):
"""
Parameter folder_path should look like "Users/documents/folder1/"
Returns a list of complete paths
"""
file_name_list = os.listdir(folder_path)

path_name_list = []
final_name_list = []
for name in file_name_list:
# Put any sanity checks here, e.g.:
if name == ".DS_Store":
pass
else:
path_name_list.append(folder_path + name)
final_name_list.append(folder_path + "output" + name)
return path_name_list, final_name_list

if __name__ == "__main__":
video_folder = input("What folder would you like to process? ")
path_list, final_name_list = get_video_paths(video_folder)
for path, name in zip(path_list, final_name_list):
process_video(path, name)
print("Finished")

关于python - 如何使用 MoviePy 批量处理视频文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43380783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com