Why ffmpeg didn't respond to custom name as output?

42 Views Asked by At

Working:

import ffmpeg
import os

path = "K://Delete//"
dir_list = os.listdir(path)

for i in dir_list:
        ffmpeg.input(path+i).output("K://t//new%d.png", vf='fps=1/10').run()
        os.remove(path+i)

Not working:

import ffmpeg
import os
from datetime import datetime

path = "K://Delete//"
dir_list = os.listdir(path)

for i in dir_list:
        dt_string=datetime.now().strftime("%d%m%Y%H%M%S%f")
        ffmpeg.input(path+i).output("K://t//"+i+"%s.png"%dt_string, vf='fps=1/10').run()
           *#%d or %s both not working.*

        os.remove(path+i)

I tried to avoid same name files to be generated. Because that will raise error in future.

I tried to give all the files unique names that is appending datetime as a String to the file.

But ffmpeg is showing error that "Conversion Failed!"

Why is this happening and how can I fix it?

1

There are 1 best solutions below

1
Iman Mohammadi On

I think you want to append a unique timestamp to each frame extracted from the video files. I see that using %s and %d can be problematic when constructing file names with FFmpeg because FFmpeg uses %d as a placeholder for sequence numbers when generating output files.

To avoid this issue, you can construct the filename in Python first and then pass it to FFmpeg without using any % formatting:

import ffmpeg
import os
from datetime import datetime

path = "K://Delete//"
dir_list = os.listdir(path)

for i in dir_list:
    dt_string = datetime.now().strftime("%d%m%Y%H%M%S%f")
    output_filename = "K://t//" + i + dt_string + ".png"
    ffmpeg.input(path+i).output(output_filename, vf='fps=1/10').run()
    os.remove(path+i)

This way, the filename is already determined before it's passed to FFmpeg, and FFmpeg doesn't have to handle any formatting or placeholders in the filename itself.