|
209 | 209 | "import subprocess\n",
|
210 | 210 | "\n",
|
211 | 211 | "import youtube_dl\n",
|
| 212 | + "from youtube_dl.utils import (DownloadError, ExtractorError)\n", |
212 | 213 | "\n",
|
213 | 214 | "def download_video(url, start, dur, output):\n",
|
| 215 | + " output_tmp = os.path.join(\"/tmp\",os.path.basename(output))\n", |
214 | 216 | " 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", |
216 | 218 | " with youtube_dl.YoutubeDL({'format': 'best'}) as ydl:\n",
|
217 | 219 | " result = ydl.extract_info(url, download=False)\n",
|
218 | 220 | " video = result['entries'][0] if 'entries' in result else result\n",
|
219 | 221 | " \n",
|
220 | 222 | " 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", |
223 | 240 | " subprocess.call(cmd)\n",
|
224 | 241 | " return True\n",
|
225 | 242 | " \n",
|
226 |
| - " except:\n", |
| 243 | + " except (DownloadError, ExtractorError) as e:\n", |
227 | 244 | " print(\"Failed to download %s\" % output)\n",
|
228 | 245 | " return False\n",
|
229 | 246 | " \n",
|
|
252 | 269 | " if not os.path.exists(c_dir):\n",
|
253 | 270 | " os.makedirs(c_dir)\n",
|
254 | 271 | " \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", |
257 | 274 | " output = os.path.join(c_dir, \"%s_%s.mp4\" % (label.replace(\" \",\"_\"), fn))\n",
|
258 | 275 | " \n",
|
259 | 276 | " results = True\n",
|
260 | 277 | " 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", |
262 | 279 | " if result:\n",
|
263 | 280 | " classes_count[label] += 1\n",
|
264 | 281 | "\n",
|
|
0 commit comments