-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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