Update hls_download.py
Browse files- hls_download.py +39 -38
hls_download.py
CHANGED
|
@@ -1,39 +1,40 @@
|
|
| 1 |
-
import subprocess
|
| 2 |
-
import os
|
| 3 |
-
|
| 4 |
-
def download_clips(stream_url, out_dir, start_time, end_time, resize=True, use_60fps=False):
|
| 5 |
-
# remove all .mp4 files in out_dir to avoid confusion
|
| 6 |
-
if len(os.listdir(out_dir)) > 5:
|
| 7 |
-
for f in os.listdir(out_dir):
|
| 8 |
-
if f.endswith('.mp4'):
|
| 9 |
-
os.remove(os.path.join(out_dir, f))
|
| 10 |
-
output_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}.mp4")
|
| 11 |
-
if resize: # resize and convert to 30 fps
|
| 12 |
-
ffmpeg_cmd = [
|
| 13 |
-
'ffmpeg',
|
| 14 |
-
#'-hwaccel', 'cuda',
|
| 15 |
-
'-
|
| 16 |
-
'-
|
| 17 |
-
'-
|
| 18 |
-
'-
|
| 19 |
-
'-
|
| 20 |
-
'-
|
| 21 |
-
'-
|
| 22 |
-
'-
|
| 23 |
-
'-
|
| 24 |
-
'-
|
| 25 |
-
'-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
print(f"
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
return output_file
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def download_clips(stream_url, out_dir, start_time, end_time, resize=True, use_60fps=False):
|
| 5 |
+
# remove all .mp4 files in out_dir to avoid confusion
|
| 6 |
+
if len(os.listdir(out_dir)) > 5:
|
| 7 |
+
for f in os.listdir(out_dir):
|
| 8 |
+
if f.endswith('.mp4'):
|
| 9 |
+
os.remove(os.path.join(out_dir, f))
|
| 10 |
+
output_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}.mp4")
|
| 11 |
+
if resize: # resize and convert to 30 fps
|
| 12 |
+
ffmpeg_cmd = [
|
| 13 |
+
'ffmpeg',
|
| 14 |
+
#'-hwaccel', 'cuda',
|
| 15 |
+
'-allowed_extensions', 'ALL',
|
| 16 |
+
'-i', stream_url,
|
| 17 |
+
'-c:v', 'libx264',
|
| 18 |
+
'-crf', '23',
|
| 19 |
+
'-r', '30' if not use_60fps else '60',
|
| 20 |
+
'-maxrate', '2M',
|
| 21 |
+
'-bufsize', '4M',
|
| 22 |
+
'-vf', f"scale=-2:300",
|
| 23 |
+
'-ss', start_time] + (['-to', end_time] if end_time != '' else []) + [
|
| 24 |
+
'-c:a', 'aac',
|
| 25 |
+
'-b:a', '128k',
|
| 26 |
+
'-y',
|
| 27 |
+
output_file
|
| 28 |
+
]
|
| 29 |
+
print(' '.join(ffmpeg_cmd))
|
| 30 |
+
try:
|
| 31 |
+
subprocess.run(ffmpeg_cmd, check=True, capture_output=True, text=True)
|
| 32 |
+
except subprocess.CalledProcessError as e:
|
| 33 |
+
print(f"Error occurred: {e}")
|
| 34 |
+
print(f"ffmpeg output: {e.output}")
|
| 35 |
+
return output_file
|
| 36 |
+
# else:
|
| 37 |
+
# os.rename(tmp_file, output_file)
|
| 38 |
+
print(f"Converted {output_file}")
|
| 39 |
+
|
| 40 |
return output_file
|