Spaces:
Sleeping
Sleeping
Commit ·
9078257
1
Parent(s): f854772
fix yt-dlp
Browse files- hls_download.py +28 -40
hls_download.py
CHANGED
|
@@ -1,39 +1,29 @@
|
|
| 1 |
-
import yt_dlp
|
| 2 |
-
import os
|
| 3 |
import subprocess
|
|
|
|
| 4 |
|
| 5 |
def download_clips(stream_url, out_dir, start_time, end_time, resize=True):
|
| 6 |
output_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}.mp4")
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
'outtmpl': os.path.join(out_dir, 'temp_download.%(ext)s'),
|
| 15 |
-
'nocheckcertificate': True,
|
| 16 |
-
'no_warnings': True,
|
| 17 |
-
'external_downloader': 'ffmpeg',
|
| 18 |
-
'external_downloader_args': [
|
| 19 |
-
'ffmpeg_i:',
|
| 20 |
-
f'-ss {max(0, start_time - 2)}',
|
| 21 |
-
'-t', f'{duration}'
|
| 22 |
-
],
|
| 23 |
-
}
|
| 24 |
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
ffmpeg_cmd = [
|
| 35 |
'ffmpeg',
|
| 36 |
-
'-i',
|
| 37 |
'-c:v', 'libx264',
|
| 38 |
'-preset', 'veryfast',
|
| 39 |
'-crf', '23',
|
|
@@ -43,17 +33,15 @@ def download_clips(stream_url, out_dir, start_time, end_time, resize=True):
|
|
| 43 |
'-c:a', 'aac',
|
| 44 |
'-b:a', '128k',
|
| 45 |
'-y',
|
| 46 |
-
|
| 47 |
]
|
| 48 |
-
|
| 49 |
subprocess.run(ffmpeg_cmd, check=True, capture_output=True, text=True)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
| 58 |
-
print(f"An error occurred: {e}")
|
| 59 |
-
return None
|
|
|
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
+
import os
|
| 3 |
|
| 4 |
def download_clips(stream_url, out_dir, start_time, end_time, resize=True):
|
| 5 |
output_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}.mp4")
|
| 6 |
+
tmp_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}_tmp.mp4")
|
| 7 |
+
yt_dlp_cmd = [
|
| 8 |
+
'yt-dlp',
|
| 9 |
+
'--download-sections', f'*{start_time}-{end_time}',
|
| 10 |
+
stream_url,
|
| 11 |
+
'-o', tmp_file
|
| 12 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
print(' '.join(yt_dlp_cmd))
|
| 15 |
+
|
| 16 |
try:
|
| 17 |
+
subprocess.run(yt_dlp_cmd, check=True, capture_output=True, text=True)
|
| 18 |
+
except subprocess.CalledProcessError as e:
|
| 19 |
+
print(f"Error occurred: {e}")
|
| 20 |
+
print(f"yt-dlp output: {e.output}")
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
if resize:
|
| 24 |
+
ffmpeg_cmd = [
|
|
|
|
| 25 |
'ffmpeg',
|
| 26 |
+
'-i', tmp_file,
|
| 27 |
'-c:v', 'libx264',
|
| 28 |
'-preset', 'veryfast',
|
| 29 |
'-crf', '23',
|
|
|
|
| 33 |
'-c:a', 'aac',
|
| 34 |
'-b:a', '128k',
|
| 35 |
'-y',
|
| 36 |
+
output_file
|
| 37 |
]
|
| 38 |
+
try:
|
| 39 |
subprocess.run(ffmpeg_cmd, check=True, capture_output=True, text=True)
|
| 40 |
+
except subprocess.CalledProcessError as e:
|
| 41 |
+
print(f"Error occurred: {e}")
|
| 42 |
+
print(f"ffmpeg output: {e.output}")
|
| 43 |
+
return None
|
| 44 |
+
else:
|
| 45 |
+
os.rename(tmp_file, output_file)
|
| 46 |
|
| 47 |
+
return output_file
|
|
|
|
|
|