-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffmpeg.py
49 lines (44 loc) · 1.63 KB
/
ffmpeg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# python wrapper for basic ffmpeg operations
# resize video, check if a video is corrupted, etc.
import subprocess, re, os
# provide your own ffmpeg here
ffmpeg = 'ffmpeg'
# resize videoName to 320x240 and store in resizedName
# if succeed return True
def resize(videoName, resizedName):
if not os.path.exists(videoName):
print '%s does not exist!' % videoName
return False
# call ffmpeg and grab its stderr output
p = subprocess.Popen([ffmpeg, "-i", videoName], stderr=subprocess.PIPE)
out, err = p.communicate()
# search resolution info
if err.find('differs from') > -1:
return False
reso = re.findall(r'Video.*, ([0-9]+)x([0-9]+)', err)
if len(reso) < 1:
return False
# call ffmpeg again to resize
subprocess.call([ffmpeg, '-i', videoName, '-s', '320x240', resizedName])
return check(resizedName)
# check if the video file is corrupted or not
def check(videoName):
if not os.path.exists(videoName):
return False
p = subprocess.Popen([ffmpeg, "-i", videoName], stderr=subprocess.PIPE)
out, err = p.communicate()
if err.find('Invalid') > -1:
return False
return True
def extract_frame(videoName,frameName):
"""Doc
Extracts the first frame from the input video (videoName)
and saves it at the location (frameName)
"""
#forces extracted frames to be 320x240 dim.
if not os.path.exists(videoName):
print '%s does not exist!' % videoName
return False
# call ffmpeg and grab its stderr output
p = subprocess.call('ffmpeg -i %s -r 1 -s qvga -t 1 -f image2 %s' % (videoName,frameName), shell=True)
return p