2017-04-12 37 views
0

我編寫了一個MoviePy腳本,它接受輸入視頻,進行一些處理並輸出視頻文件。我想通過整個視頻文件夾來運行它。任何幫助或方向表示讚賞。如何使用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文件。

感謝, 埃文

回答

0

我知道你有一個答案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") 
0

我回答你的Github issue #542,但我在這裏複製以供將來參考!

首先,下面的例子不是鐵的,但它應該做你需要的。 您可以通過像這樣實現的:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
"""Convert all media assets located in a specified directory.""" 
import glob 
import os 
from optparse import OptionParser 

from moviepy.editor import VideoFileClip 


def get_dir_files(dir_path, patterns=None): 
    """Get all absolute paths for pattern matched files in a directory. 

    Args: 
     dir_path (str): The path to of the directory containing media assets. 
     patterns (list of str): The list of patterns/file extensions to match. 

    Returns: 
     (list of str): A list of all pattern-matched files in a directory. 
    """ 
    if not patterns or type(patterns) != list: 
     print('No patterns list passed to get_dir_files, defaulting to patterns.') 
     patterns = ['*.mp4', '*.avi', '*.mov', '*.flv'] 

    files = [] 
    for pattern in patterns: 
     dir_path = os.path.abspath(dir_path) + '/' + pattern 
     files.extend(glob.glob(dir_path)) 

    return files 

def modify_clip(path, output): 
    """Handle conversion of a video file. 

    Args: 
     path (str): The path to the directory of video files to be converted. 
     output (str): The filename to associate with the converted file. 
    """ 
    clip = VideoFileClip(path) 
    clip = clip.rotate(270) 
    clip = clip.crop(x_center=540, y_center=960, width=1080, height=608) 
    clip = clip.resize(width=1920) 

    clip.write_videofile(output, codec='libx264') 
    print('File: {} should have been created.'.format(output)) 


if __name__ == '__main__': 
    status = 'Failed!' 
    parser = OptionParser(version='%prog 1.0.0') 
    parser.add_option('-p', '--path', action='store', dest='dir_path', 
         default='.', type='string', 
         help='the path of the directory of assets, defaults to .') 

    options, args = parser.parse_args() 
    print('Running against directory path: {}'.format(options.dir_path)) 
    path_correct = raw_input('Is that correct?').lower() 

    if path_correct.startswith('y'): 
     dir_paths = get_dir_files(options.dir_path) 
     for dir_path in dir_paths: 
      output_filename = 'converted_' + os.path.basename(dir_path) 
      modify_clip(path=dir_path, output=output_filename) 

     status = 'Successful!' 

    print('Conversion {}'.format(status)) 

通過上面的例子,你可以簡單地丟棄到資產要轉換和運行目錄:python this_file.py,它應該將文件轉換爲您在與名稱相同的目錄前綴爲:converted_

同樣,你可以在任何地方丟棄的文件和反對的絕對路徑運行它: python this_file.py -p /Users/thisguy/media,它將所有文件轉換與擴展:['*.mp4', '*.avi', '*.mov', '*.flv']

無論哪種方式,如果您有任何問題(或者如果這能解決您的問題),請告訴我,我會盡我所能幫助您!

感謝您使用影片!