简体   繁体   中英

Python Iterate through every files in the folder to cut portion of video

I was cropping about hundreds of videos to crop the first 30 seconds of each clip. The code is working for one clip but when I put path, the code starts to not work.

I pasted the current code with what I tried below. Could I get an insight on how I could iterate every file in the folder?

Thank you

import os
import ffmpeg

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from moviepy.editor import *

def timeCrop():
    directory = "/Users/documents/lab/video/ProccessedVideo"
    count = 1

    for filename in os.listdir(directory):
        if filename.endswith(".mpg") or filename.endswith(".mp4"):
            path = os.path.join(directory, filename)
            input = ffmpeg.input(path)
            print(path, count)
            mice = ["022, 90"]

            ffmpeg_extract_subclip("{path}.mpg", 30, 120, targetname="{mice[0]}.mpg, {mice[1]}.mpg")
            count += 1
def timeCrop2():

    directory = "/Users/documents/lab/video/ProccessedVideo"

    clip = VideoFileClip("022.mpg").cutout(0, 30)
    clip.write_videofile("022e.mpg", codec="libx264")

#timeCrop()
timeCrop2()

You can iterate each files in the directory with filter for example (.mp4) using the code below

import os

directory = os.fsencode("D:\\Acroyoga")
  
for file in os.listdir(directory):
    filename = os.fsdecode(file)
    if filename.endswith(".mp4"):
        print(filename) #do your video process here
        continue
    else:
        continue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM