Sukhada Deshpande
Sukhada Deshpande

Reputation: 1

How do I integrate the FFMPEG commands with Python Script?

I am trying to extract the frames when the scene changes in an .mp4 video. The package that I am using is FFMPEG. FFMPEG predominantly works on the CLI and I am trying to integrate it with Python3.x

The command I am using in the CLI is:

ffmpeg -i {0} -vf  "select=gt(scene\,0.5), scale=640:360" -vsync vfr frame%d.png

The output comes out just fine with the CLI execution.

But I want to use same command in a Python script, how do I do that and what should be the code?

Being an amateur in the field, currently grappling with this!

Upvotes: 0

Views: 1147

Answers (3)

Sukhada Deshpande
Sukhada Deshpande

Reputation: 1

Thanks for the help! This is the snippet of code I'm currently using and it gives the results as I require. I have added a functionality for timestamp generation of the frames in addition to the frame formation using scene change detection

===========================================================================

>     # FFMPEG Package call through script
>     # need to change the location in the cmd post -vsync vfr to the location where the frames are to be stored 
>     # the location should be same as where the videos are located

============================================================================

inputf = []
for filename in os.listdir(path):
    file= filename.split('.')[0]             # Splits the file at the extension and stores it without .mp4 extension
    input_file = path + filename
    inputf.append(input_file)                # Creates a list of all the files read
    for x in range (0, len(inputf)):
        cmd = f'ffmpeg -i {inputf[x]} -filter_complex "select=gt(scene\,0.2), scale=640:360, metadata=print:file=time_{file}.txt" -vsync vfr {path where the videos are located}\\{file}_frame%d.jpg'
        os.system(cmd)
        x=x+1
    print("Done")                            # Takes time will loop over all the videos

Upvotes: 0

Christoph Rackwitz
Christoph Rackwitz

Reputation: 15510

I would recommend PyAV. it's a proper wrapper around ffmpeg's libraries.

the other mentioned packages use the "subprocess" approach, which is limited and inefficient. these libraries may be more convenient than plain ffmpeg APIs.

Upvotes: 2

user9706
user9706

Reputation:

You could execute that command from Python via subprocess module, of course, but it would better to use library like https://github.com/kkroening/ffmpeg-python

Upvotes: 1

Related Questions