-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframe.py
54 lines (34 loc) · 1.54 KB
/
frame.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
50
51
52
53
#created by Memoona Tahira on 25 september 2019 on 15:28
# you need to have ffmpeg instaled and on the path.
import os
import subprocess as sp
import sys
from sys import platform as _platform
#check the platform to detremine how to call ffmpeg library
if _platform == "linux" or _platform == "linux2" or _platform == "darwin":
FFMPEG_BIN = "fmpeg"
elif _platform == "win32" or _platform == "win64":
FFMPEG_BIN = "ffmpeg.exe"
else:
sys.exit("unknown Operating system. Terminating program")
# Create directory for storing frames called 'frames'
DIR = os.path.abspath(os.path.dirname(__file__))
FRAMEDIR=os.path.join(DIR, 'frames')
if not os.path.isdir(FRAMEDIR):
os.mkdir(FRAMEDIR)
# path to directory containing the videos:
VIDDIR= os.path.join(DIR, 'Videos')
if not os.path.isdir(VIDDIR):
raise Exception("ERROR: no video directory found! (expecting '%s')" % VIDDIR)
#read video folder contents one by one, generate frames using ffmpeg and store as frames in the FRAMEDIR
#folder with 3 digit serial extension. eg 001_002
vidlist = sorted(os.listdir(VIDDIR))
vidlist_wo = sorted([os.path.splitext(filename)[0] for filename in os.listdir(VIDDIR)])
for num in range (len(vidlist)):
vidname= vidlist[num]
vidpath = os.path.join(VIDDIR, "%s" % (vidname))
vidname_wo= vidlist_wo[num]
framepath= os.path.join(FRAMEDIR, "%s" % (vidname_wo))
# change extension to .png or .bmp if required.
command = 'ffmpeg -i {vidpath} {framepath}_%03d.jpg'.format(vidpath=vidpath, framepath=framepath)
sp.call(command, shell=True)