Skip to content

Instantly share code, notes, and snippets.

@chrjxj
Forked from hiwonjoon/python-ffmpeg.py
Created January 26, 2020 11:42
Show Gist options
  • Save chrjxj/8a3449bc93f8fe662dfeb1ec6bb5867c to your computer and use it in GitHub Desktop.
Save chrjxj/8a3449bc93f8fe662dfeb1ec6bb5867c to your computer and use it in GitHub Desktop.
ffmpeg and ffprobe subprocess call in python; extract specific frame at some timepoint, extract duration of a video
def _get_duration(filename) :
command = ['ffprobe',
'-v', 'error',
'-show_entries', 'format=duration',
'-of', 'default=noprint_wrappers=1:nokey=1',
filename, '-sexagesimal']
ffmpeg = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout = subprocess.PIPE )
out, err = ffmpeg.communicate()
if(err) : print(err)
return out
def _read_frame(filename,time) :
command = ['ffmpeg',
'-loglevel', 'error',
'-ss', str(time), #from this time
'-i', filename,
'-vframes', str(1), #get single frame
'-f', 'image2pipe',
'-pix_fmt', 'rgb24',
'-vcodec', 'rawvideo', '-']
#print(command)
ffmpeg = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout = subprocess.PIPE )
out, err = ffmpeg.communicate()
if(err) : print(err)
image = np.fromstring(out, dtype='uint8').reshape((960,1280,3))
return image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment