dylan-plummer commited on
Commit
9078257
·
1 Parent(s): f854772

fix yt-dlp

Browse files
Files changed (1) hide show
  1. 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
- # 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)}',
21
- '-t', f'{duration}'
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',
@@ -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
- 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
 
 
 
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