简体   繁体   中英

Redirecting the output of ffmpeg in subprocess

I have a folder named video_files where I have stored a bunch of video files eg 100.mp4, 101.mp4... . I have written a python script that iterates over each video file. Using the subprocess to call ffmpeg to extract the frames and then save the frames to the output directory named as frames . Here is my sample code for the same:

def frame_extractor(video_files_path):
        video_files = sorted(glob.glob(video_files_path + "**/*.mp4", recursive=True))
        print("Number of video files found: ", len(video_files))
        for i, video in enumerate(video_files):
            subprocess.call(["ffmpeg", "-i", video, "%04d.png"]
        print("Extracted frames from all the videos") 

The problem is that it extracts the frames in the present directory, from where I run this script but I want the frames to be extracted in the frames folder.

PS: frames/%04d.png doesn't work.

Can anyone please tell me how to do this?

You could add a call after your function finishes to move all of the output files.

import os
import glob

for img in glob.glob(video_files_path + '**/*.png', recursive=True):
    img_out = os.path.join('frames', os.path.split(img)[-1])
    os.rename(img, img_out)

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