0

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!

2

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Like the os.system() documentation itself suggests, a better solution is the subprocess package. But of course, a native Python library would be better stil.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.