27

I would like to run the following command in a python script, I also want to make it loop over several videos in a folder. This is the command I want to run.

ffmpeg -i mymovie.avi -f image2 -vf fps=fps=1 output%d.png

I want to fit it in something like this:

import ffmpy
import os

path = './Videos/MyVideos/'
for filename in os.listdir(path):
    name = filename.replace('.avi','')
    os.mkdir(os.path.join(path,name))
    *ffmpeg command here*

I found a wrapper for ffmpeg called ffmpy, could this be a solution?

Leon
  • 12,013
  • 5
  • 36
  • 59
jawwe
  • 638
  • 1
  • 5
  • 13
  • And have you given any thought as to how you will do any of this that you can share? – Leon Feb 24 '17 at 12:16

4 Answers4

41

From a brief look at FFMPY, you could do this using ffmpy.FFmpeg, as that allows any and all FFMPEG command line options, including -f. -- Click the link for documentation.

You could do the FFMPEG command with os.system. You'll need to import OS anyway to iterate through the files.

You would need to iterate through all the files in a directory though. This would be the more challenging bit, it's quite easy with a for loop though.

for filename in os.listdir(path):
    if (filename.endswith(".mp4")): #or .avi, .mpeg, whatever.
        os.system("ffmpeg -i {0} -f image2 -vf fps=fps=1 output%d.png".format(filename))
    else:
        continue

The above code iterates through the directory at path, and uses command prompt to execute your given FFMPEG command, using the filename (if it's a video file) in place of mymovie.avi

ocelot
  • 1,067
  • 12
  • 16
4

Dont have reputation to comment, hence adding another response.

Another version of ocelot's answer with the more readable f-string syntax of python -

for filename in os.listdir(path):
    if (filename.endswith(".mp4")): #or .avi, .mpeg, whatever.
        os.system(f'ffmpeg -i {filename} -f image2 -vf fps=fps=1 output%d.png')
    else:
        continue
ssbayes
  • 81
  • 3
2

Try pydemux in https://github.com/Tee0125/pydemux. Pydemux module can extract video frames as in Pillow Image format

from PyDemux import Video

v = Video.open('video.mov')

i = 0
while True:
    im = v.get_frame()

    if im is None:
        break

    im.save('output%d.png'%i)
    i = i + 1
Tee Jung
  • 37
  • 1
  • What if I need to do something else while this main loop is going on? Whatever I am recording shall not be put on pause, so I'd have to have more than one thread or something. Sounds complicated but likely doable. How though? – Leonid Feb 23 '23 at 11:20
1

This is a way to use ffmpeg in a python script, e.g. for extracting the last 10 seconds of every video:

ffmpeg -sseof -10 -i input.mp4 output.mp4

To apply this to a whole folder of mp4 files:

from pathlib import Path
import os
suffix = ".mp4"
input_path= Path.home() / "Desktop/foo"
file_paths= [subp for subp in input_path.rglob('*') if  suffix == subp.suffix]
file_paths.sort()

output_path =  Path.home() / "Desktop/foo/new"
output_path.mkdir(parents=True, exist_ok=True)

for file_p in file_paths:
    input = str(file_p)
    output = str(  output_path / file_p.name  ) 
    command = f"ffmpeg -sseof -10 -i {input} {output}"
    print(command)
    os.system(command)
Kolibril
  • 1,096
  • 15
  • 19