0

I am using ffmpeg to extract a frame from a video. This works fine when I use ffmpeg from the command line, however, when I try to do the same thing using the python:

os.popen3('ffmpeg -i videoPath -an -ss 00:00:02 -an -r 1 -vframes 1 -y picturePath')

I have no idea on how to get the extracted image. So far, I get only text saying (ffmpeg version N-62039-gc00f368 Copyright (c) 2000....) which is what I see in the command line. Would you please guide through what I need to do to get the image extracted. Thank you.

5
  • How do you get the extracted image when you run ffmpeg from the command line?
    – jfs
    Apr 5, 2014 at 21:34
  • Yes, that what I want, if possible of course.. Apr 5, 2014 at 21:51
  • It is not a yes or no question :) What do you mean when you say: "this works fine when I use ffmpeg from the command line"? Unrelated: Making GIFs From Video Files With Python
    – jfs
    Apr 5, 2014 at 22:08
  • I can get video thumbnails using the ffmpeg from a command line, I find the PNG ina folder, but what I want to execute the ffmpeg from Python and get the PNG as a return value so I can use it further. Apr 6, 2014 at 3:51
  • Do you need help to run the command (to create a png file) or do you need help to load already saved png-file to be edited in Python? moviepy module I've linked above combines both.
    – jfs
    Apr 6, 2014 at 4:43

2 Answers 2

1

I typically use the following function for that sort of tasks. It includes a timeout parameter for automatically aborting excessively long running processes. That's often handy when it comes to user uploaded content:

import subprocess
from threading import Timer

def run_with_timeout(cmd, sec):
    def kill_proc(p, killed):
        killed['val'] = True
        p.kill()
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    killed = {'val': False}
    timer = Timer(sec, kill_proc, [p, killed])
    timer.start()
    stdout, stderr = p.communicate()
    timer.cancel()
    return p.returncode, stdout, stderr, killed['val']

With that, you can simply call any shell command with options, which is run synchronously. That means the function waits until the process is finished. Therefore, when the function is done, the thumbnail is either created or an error is returned:

videoPath = '/path/to/video/source/file.mp4'
picturePath = '/path/to/output.jpg'
result = run_with_timeout(['ffmpeg', '-i', videoPath, '-an', '00:00:02', '-r', '1', '-vframes', '1', '-y', picturePath], 30)
if result[0] != 0:
    print 'error'
else:
    print 'success'
    # do something with videoPath 

Works in current Python versions 2.7.x.

0

os is deprecated. You should use subprocess.

2
  • 2
    The os module is not deprecated, but os.popen*() and related functions are.
    – Brian Neal
    Apr 3, 2014 at 2:00
  • Still, doesn't answer the question of how to get the image back to code so I can use it further. I have tried using subprocess too.. Apr 3, 2014 at 11:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.