-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathiframe_extract.py
More file actions
171 lines (133 loc) · 5.2 KB
/
Copy pathiframe_extract.py
File metadata and controls
171 lines (133 loc) · 5.2 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'''
iframe_extract.py - download video and ffmpeg i-frame extraction
Usage:
(ex) python iframe_extract.py -u https://www.youtube.com/watch?v=dP15zlyra3c
This code does two things:
1. Download using youtube-dl
cmd = ['youtube-dl', '-f', videoSize, '-k', '-o', video_out, download_url]
2. Extract i-frames via ffmpeg
cmd = [ffmpeg,'-i', inFile,'-f', 'image2','-vf',
"select='eq(pict_type,PICT_TYPE_I)'",'-vsync','vfr', imgFilenames]
'''
from __future__ import unicode_literals
import youtube_dl
import sys
import os
import subprocess
import argparse
import glob
if sys.platform == "win32":
FFMPEG_BIN = "ffmpeg.exe"
MOVE = "move "
MKDIR = "md "
elif sys.platform == 'linux' or sys.platform == 'linux2':
FFMPEG_BIN = "ffmpeg"
MOVE = "mv "
MKDIR = "mkdir "
elif sys.platform == 'darwin':
FFMPEG_BIN = "ffmpeg"
MOVE = "mv "
MKDIR = "mkdir "
def iframe_extract(inFile):
# ffmpeg -i inFile -f image2 -vf \
# "select='eq(pict_type,PICT_TYPE_I)'" -vsync vfr oString%03d.png
# infile : video file name
# (ex) 'FoxSnowDive-Yellowstone-BBCTwo.mp4'
imgPrefix = inFile.split('.')[0]
# imgPrefix : image file
# start extracting i-frames
home = os.path.expanduser("~")
ffmpeg = home + '/bin/ffmpeg'
imgFilenames = imgPrefix + '%03d.png'
cmd = [ffmpeg,'-i', inFile,'-f', 'image2','-vf',
"select='eq(pict_type,PICT_TYPE_I)'",'-vsync','vfr', imgFilenames]
# create iframes
print("creating iframes ....")
subprocess.call(cmd)
# Move the extracted iframes to a subfolder
# imgPrefix is used as a subfolder name that stores iframe images
cmd = MKDIR + '-p ' + imgPrefix
os.system(cmd)
print("make subdirectoy=%s" %cmd)
mvcmd = MOVE + imgPrefix + '*.png ' + imgPrefix
print("moving images to subdirectoy %s" %mvcmd)
os.system(mvcmd)
def get_info_and_download(download_url):
# Get video meta info and then download using youtube-dl
ydl_opts = {}
# get meta info from the video
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
meta = ydl.extract_info(download_url, download=False)
# renaming the file
# remove special characters from the file name
print('meta[title]=%s' %meta['title'])
out = ''.join(c for c in meta['title'] if c.isalnum() or c =='-' or c =='_' )
print('out=%s' %out)
extension = meta['ext']
video_out = out + '.' + extension
print('video_out=%s' %video_out)
videoSize = 'bestvideo[height<=540]+bestaudio/best[height<=540]'
cmd = ['youtube-dl', '-f', videoSize, '-k', '-o', video_out, download_url]
print('cmd=%s' %cmd)
# download the video
subprocess.call(cmd)
# Sometimes output file has format code in name such as 'out.webm'
# So, when the best output format happens to be mp4, for example, 'out.webm.mp4'
# which is the input file for iframe_extract(). But there is no 'out.webm.mp4' any more.
# The following will reset the input as the newly merged video output, 'out.webm.mp4'
found = False
extension_list = ['mkv', 'mp4', 'webm']
for e in extension_list:
glob_str = '*.' + e
for f in glob.glob(glob_str):
if out in f:
if os.path.isfile(f):
video_out = f
found = True
break
if found:
break
# call iframe-extraction : ffmpeg
print('before iframe_extract() video_out=%s' %video_out)
iframe_extract(video_out)
return meta
def check_arg(args=None):
# Command line options
# Currently, only the url option is used
parser = argparse.ArgumentParser(description='download video')
parser.add_argument('-u', '--url',
help='download url',
required='True')
parser.add_argument('-i', '--infile',
help='input to iframe extract')
parser.add_argument('-o', '--outfile',
help='output name for iframe image')
results = parser.parse_args(args)
return (results.url,
results.infile,
results.outfile)
# Usage sample:
# syntax: python iframe_extract.py -u url
# (ex) python iframe_extract.py -u https://www.youtube.com/watch?v=dP15zlyra3c
if __name__ == '__main__':
u,i,o = check_arg(sys.argv[1:])
meta = get_info_and_download(u)
'''
-- Test Cases --
# Fox BBC
python iframe_extract.py -u https://www.youtube.com/watch?v=dP15zlyra3c
# If the Moon were replaced with some of our planets
python iframe_extract.py -u https://www.youtube.com/watch?v=usYC_Z36rHw
# CERN News - New boson spotted at the LHC: is it the Higgs?
python iframe_extract.py -u https://www.youtube.com/watch?v=I4L2XirSJw0
# The Large Hadron Collider Returns in the Hunt for New Physics
python iframe_extract.py -u https://www.youtube.com/watch?v=2wCgpdeQWZA
# Cosmic Distance Ladder: Cepheid Variables
python iframe_extract.py -u https://www.youtube.com/watch?v=iyisAjHdhas
# Orion - a brief tour
python iframe_extract.py -u https://www.youtube.com/watch?v=GqV7KMjw9jY
# Amazing Golden Gate Bridge- San Francisco, CA (From The Above)
python iframe_extract.py -u https://www.youtube.com/watch?v=Yb2okDst0oc
# Grand Teton
python iframe_extract.py -u https://www.youtube.com/watch?v=NCTd1IHChug
'''