Spaces:
Build error
Build error
Commit ·
36a336c
1
Parent(s): 7493194
try yt-dlp
Browse files- hls_download.py +51 -27
- requirements.txt +2 -1
hls_download.py
CHANGED
|
@@ -1,35 +1,59 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
output_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}.mp4")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
'
|
| 13 |
-
'
|
| 14 |
-
'
|
| 15 |
-
'
|
| 16 |
-
'
|
| 17 |
-
'
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
'-y', # Overwrite output file if it exists
|
| 24 |
-
output_file
|
| 25 |
-
]
|
| 26 |
|
| 27 |
-
print([f'{x} ' for x in ffmpeg_cmd])
|
| 28 |
-
|
| 29 |
try:
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return output_file
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
print(f"
|
| 35 |
return None
|
|
|
|
| 1 |
+
import yt_dlp
|
| 2 |
import os
|
| 3 |
+
import subprocess
|
| 4 |
|
| 5 |
+
def download_clips_yt_dlp(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 |
+
# Calculate the duration we want to download
|
| 9 |
+
duration = end_time - start_time + 4 # Add 4 seconds for 2 second padding on each side
|
| 10 |
+
|
| 11 |
+
# Configure yt-dlp options
|
| 12 |
+
ydl_opts = {
|
| 13 |
+
'format': 'best',
|
| 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)}', # Start 2 seconds earlier, but not before 0
|
| 21 |
+
f'-t {duration}' # Download only the duration we need
|
| 22 |
+
],
|
| 23 |
+
}
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
+
# Download the specific portion of the video
|
| 27 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 28 |
+
info = ydl.extract_info(stream_url, download=True)
|
| 29 |
+
downloaded_file = ydl.prepare_filename(info)
|
| 30 |
+
|
| 31 |
+
# Use FFmpeg to process the downloaded file if resizing is needed
|
| 32 |
+
if resize:
|
| 33 |
+
processed_file = os.path.join(out_dir, 'temp_processed.mp4')
|
| 34 |
+
ffmpeg_cmd = [
|
| 35 |
+
'ffmpeg',
|
| 36 |
+
'-i', downloaded_file,
|
| 37 |
+
'-c:v', 'libx264',
|
| 38 |
+
'-preset', 'veryfast',
|
| 39 |
+
'-crf', '23',
|
| 40 |
+
'-maxrate', '2M',
|
| 41 |
+
'-bufsize', '4M',
|
| 42 |
+
'-vf', f"scale=-2:360",
|
| 43 |
+
'-c:a', 'aac',
|
| 44 |
+
'-b:a', '128k',
|
| 45 |
+
'-y',
|
| 46 |
+
processed_file
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
subprocess.run(ffmpeg_cmd, check=True, capture_output=True, text=True)
|
| 50 |
+
os.remove(downloaded_file)
|
| 51 |
+
os.rename(processed_file, output_file)
|
| 52 |
+
else:
|
| 53 |
+
os.rename(downloaded_file, output_file)
|
| 54 |
+
|
| 55 |
return output_file
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"An error occurred: {e}")
|
| 59 |
return None
|
requirements.txt
CHANGED
|
@@ -9,4 +9,5 @@ opencv-python-headless==4.7.0.68
|
|
| 9 |
# openvino-dev==2022.3.0
|
| 10 |
torch
|
| 11 |
torchvision
|
| 12 |
-
onnxruntime-gpu
|
|
|
|
|
|
| 9 |
# openvino-dev==2022.3.0
|
| 10 |
torch
|
| 11 |
torchvision
|
| 12 |
+
onnxruntime-gpu
|
| 13 |
+
yt-dlp
|