dylan-plummer commited on
Commit
36a336c
·
1 Parent(s): 7493194

try yt-dlp

Browse files
Files changed (2) hide show
  1. hls_download.py +51 -27
  2. requirements.txt +2 -1
hls_download.py CHANGED
@@ -1,35 +1,59 @@
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
 
7
- ffmpeg_cmd = [
8
- 'ffmpeg',
9
- '-f', 'hls',
10
- '-i', stream_url,
11
- '-ss', str(max(0, start_time - 2)), # Ensure start time is not negative
12
- '-to', str(end_time + 2),
13
- '-r', '30',
14
- '-c:v', 'libx264',
15
- '-preset', 'ultrafast', # Use fastest encoding preset
16
- '-crf', '23', # Slightly lower CRF for better quality
17
- '-maxrate', '1M', # Limit bitrate
18
- '-bufsize', '2M',
19
- '-vf', f"scale=-2:{360 if resize else 720}",
20
- '-c:a', 'aac', # Re-encode audio for better compatibility
21
- '-b:a', '128k',
22
- '-max_muxing_queue_size', '1024', # Increase muxing queue size
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
- subprocess.run(ffmpeg_cmd, check=True, capture_output=True, text=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  return output_file
32
- except subprocess.CalledProcessError as e:
33
- print(f"Error occurred: {e}")
34
- print(f"FFmpeg output: {e.output}")
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