8000 prepend time to video to ensure keyframes · Milkigit/fiftyone-examples@07c133b · GitHub
[go: up one dir, main page]

Skip to content

Commit 07c133b

Browse files
committed
prepend time to video to ensure keyframes
1 parent 08966c8 commit 07c133b

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

examples/pytorchvideo_tutorial.ipynb

+24-7
Original file line numberDiff line numberDiff line change
@@ -209,21 +209,38 @@
209209
"import subprocess\n",
210210
"\n",
211211
"import youtube_dl\n",
212+
"from youtube_dl.utils import (DownloadError, ExtractorError)\n",
212213
"\n",
213214
"def download_video(url, start, dur, output):\n",
215+
" output_tmp = os.path.join(\"/tmp\",os.path.basename(output))\n",
214216
" try:\n",
215-
" # From https://stackoverflow.com/questions/57131049/is-it-possible-to-download-a-specific-part-of-a-file\n",
217+
" # From https://stackoverflow.com/questions/57131049/is-it-possible-to-download-a-specific-part-of-a-file\n",
216218
" with youtube_dl.YoutubeDL({'format': 'best'}) as ydl:\n",
217219
" result = ydl.extract_info(url, download=False)\n",
218220
" video = result['entries'][0] if 'entries' in result else result\n",
219221
" \n",
220222
" url = video['url']\n",
221-
" cmd = ['ffmpeg', '-i', url, '-ss', start, '-t', dur, '-c:v',\n",
222-
" 'copy', '-c:a', 'copy', output]\n",
223+
" if start < 5:\n",
224+
" offset = start\n",
8000
225+
" else:\n",
226+
" offset = 5\n",
227+
" start -= offset\n",
228+
" offset_dur = dur + offset\n",
229+
" start_str = str(timedelta(seconds=start)) \n",
230+
" dur_str = str(timedelta(seconds=offset_dur)) \n",
231+
"\n",
232+
" cmd = ['ffmpeg', '-i', url, '-ss', start_str, '-t', dur_str, '-c:v',\n",
233+
" 'copy', '-c:a', 'copy', output_tmp]\n",
234+
" subprocess.call(cmd)\n",
235+
"\n",
236+
" start_str_2 = str(timedelta(seconds=offset)) \n",
237+
" dur_str_2 = str(timedelta(seconds=dur)) \n",
238+
"\n",
239+
" cmd = ['ffmpeg', '-i', output_tmp, '-ss', start_str_2, '-t', dur_str_2, output]\n",
223240
" subprocess.call(cmd)\n",
224241
" return True\n",
225242
" \n",
226-
" except:\n",
243+
" except (DownloadError, ExtractorError) as e:\n",
227244
" print(\"Failed to download %s\" % output)\n",
228245
" return False\n",
229246
" \n",
@@ -252,13 +269,13 @@
252269
" if not os.path.exists(c_dir):\n",
253270
" os.makedirs(c_dir)\n",
254271
" \n",
255-
" start_str = str(timedelta(seconds=segment[0])) \n",
256-
" dur_str = str(timedelta(seconds=dur)) \n",
272+
"\n",
273+
" start = segment[0]\n",
257274
" output = os.path.join(c_dir, \"%s_%s.mp4\" % (label.replace(\" \",\"_\"), fn))\n",
258275
" \n",
259276
" results = True\n",
260277
" if not os.path.exists(output):\n",
261-
" result = download_video(url, start_str, dur_str, output)\n",
278+
" result = download_video(url, start, dur, output)\n",
262279
" if result:\n",
263280
" classes_count[label] += 1\n",
264281
"\n",

0 commit comments

Comments
 (0)
0