swiftyy-mage
commited on
Formatting
Browse files- pytube/cli.py +17 -17
pytube/cli.py
CHANGED
@@ -229,7 +229,7 @@ def _download(
|
|
229 |
|
230 |
def unique_name(base: str, subtype: str, video_audio: str, target: Path) -> str:
|
231 |
"""
|
232 |
-
Given a base name, the file format, and the target directory, will generate
|
233 |
a filename unique for that directory and file format.
|
234 |
:param str base:
|
235 |
The given base-name.
|
@@ -267,14 +267,14 @@ def ffmpeg_process(
|
|
267 |
else:
|
268 |
target = Path(target)
|
269 |
|
270 |
-
if resolution
|
271 |
highest_quality = youtube.streams.filter(
|
272 |
progressive=False
|
273 |
-
|
274 |
|
275 |
video_stream = youtube.streams.filter(
|
276 |
progressive=False, subtype="mp4"
|
277 |
-
|
278 |
|
279 |
if highest_quality.resolution == video_stream.resolution:
|
280 |
ffmpeg_downloader(youtube=youtube, stream=video_stream, target=target)
|
@@ -283,13 +283,13 @@ def ffmpeg_process(
|
|
283 |
else:
|
284 |
video_stream = youtube.streams.filter(
|
285 |
progressive=False, resolution=resolution, subtype="mp4"
|
286 |
-
|
287 |
if video_stream is not None:
|
288 |
ffmpeg_downloader(youtube=youtube, stream=video_stream, target=target)
|
289 |
else:
|
290 |
video_stream = youtube.streams.filter(
|
291 |
progressive=False, resolution=resolution
|
292 |
-
|
293 |
if video_stream is None:
|
294 |
print(f"Could not find a stream with resolution: {resolution}")
|
295 |
print("Try one of these:")
|
@@ -301,8 +301,8 @@ def ffmpeg_process(
|
|
301 |
def ffmpeg_downloader(youtube: YouTube, stream: Stream, target: Path) -> None:
|
302 |
"""
|
303 |
Given a YouTube Stream object, finds the correct audio stream, downloads them both
|
304 |
-
giving them a unique name, them uses ffmpeg to create a new file with the audio
|
305 |
-
and video from the previously downloaded files. Then deletes the original adaptive
|
306 |
streams, leaving the combination.
|
307 |
|
308 |
:param YouTube youtube:
|
@@ -314,14 +314,14 @@ def ffmpeg_downloader(youtube: YouTube, stream: Stream, target: Path) -> None:
|
|
314 |
"""
|
315 |
audio_stream = youtube.streams.filter(
|
316 |
only_audio=True, subtype=stream.subtype
|
317 |
-
|
318 |
|
319 |
video_unique_name = unique_name(
|
320 |
safe_filename(stream.title), stream.subtype, "video", target=target
|
321 |
-
|
322 |
audio_unique_name = unique_name(
|
323 |
safe_filename(stream.title), stream.subtype, "audio", target=target
|
324 |
-
|
325 |
_download(stream=stream, target=str(target), filename=video_unique_name)
|
326 |
_download(stream=audio_stream, target=str(target), filename=audio_unique_name)
|
327 |
|
@@ -329,8 +329,8 @@ def ffmpeg_downloader(youtube: YouTube, stream: Stream, target: Path) -> None:
|
|
329 |
audio_path = Path(target) / f"{audio_unique_name}.{stream.subtype}"
|
330 |
final_path = Path(target) / f"{safe_filename(stream.title)}.{stream.subtype}"
|
331 |
|
332 |
-
subprocess.run([
|
333 |
-
f"{audio_path}", "-codec", "copy", f'{final_path}'
|
334 |
]
|
335 |
)
|
336 |
video_path.unlink()
|
@@ -436,9 +436,9 @@ def download_audio(
|
|
436 |
youtube: YouTube, filetype: str, target: Optional[str] = None
|
437 |
) -> None:
|
438 |
"""
|
439 |
-
Given a filetype, downloads the highest quality available audio stream for a
|
440 |
YouTube video.
|
441 |
-
|
442 |
:param YouTube youtube:
|
443 |
A valid YouTube object.
|
444 |
:param str filetype:
|
@@ -448,7 +448,7 @@ def download_audio(
|
|
448 |
"""
|
449 |
audio = youtube.streams.filter(
|
450 |
only_audio=True, subtype=filetype
|
451 |
-
|
452 |
|
453 |
if audio is None:
|
454 |
print(
|
@@ -459,7 +459,7 @@ def download_audio(
|
|
459 |
youtube.register_on_progress_callback(on_progress)
|
460 |
|
461 |
try:
|
462 |
-
_download(audio)
|
463 |
except KeyboardInterrupt:
|
464 |
sys.exit()
|
465 |
|
|
|
229 |
|
230 |
def unique_name(base: str, subtype: str, video_audio: str, target: Path) -> str:
|
231 |
"""
|
232 |
+
Given a base name, the file format, and the target directory, will generate
|
233 |
a filename unique for that directory and file format.
|
234 |
:param str base:
|
235 |
The given base-name.
|
|
|
267 |
else:
|
268 |
target = Path(target)
|
269 |
|
270 |
+
if resolution == "best":
|
271 |
highest_quality = youtube.streams.filter(
|
272 |
progressive=False
|
273 |
+
).order_by("resolution").desc().first()
|
274 |
|
275 |
video_stream = youtube.streams.filter(
|
276 |
progressive=False, subtype="mp4"
|
277 |
+
).order_by("resolution").desc().first()
|
278 |
|
279 |
if highest_quality.resolution == video_stream.resolution:
|
280 |
ffmpeg_downloader(youtube=youtube, stream=video_stream, target=target)
|
|
|
283 |
else:
|
284 |
video_stream = youtube.streams.filter(
|
285 |
progressive=False, resolution=resolution, subtype="mp4"
|
286 |
+
).first()
|
287 |
if video_stream is not None:
|
288 |
ffmpeg_downloader(youtube=youtube, stream=video_stream, target=target)
|
289 |
else:
|
290 |
video_stream = youtube.streams.filter(
|
291 |
progressive=False, resolution=resolution
|
292 |
+
).first()
|
293 |
if video_stream is None:
|
294 |
print(f"Could not find a stream with resolution: {resolution}")
|
295 |
print("Try one of these:")
|
|
|
301 |
def ffmpeg_downloader(youtube: YouTube, stream: Stream, target: Path) -> None:
|
302 |
"""
|
303 |
Given a YouTube Stream object, finds the correct audio stream, downloads them both
|
304 |
+
giving them a unique name, them uses ffmpeg to create a new file with the audio
|
305 |
+
and video from the previously downloaded files. Then deletes the original adaptive
|
306 |
streams, leaving the combination.
|
307 |
|
308 |
:param YouTube youtube:
|
|
|
314 |
"""
|
315 |
audio_stream = youtube.streams.filter(
|
316 |
only_audio=True, subtype=stream.subtype
|
317 |
+
).order_by("abr").desc().first()
|
318 |
|
319 |
video_unique_name = unique_name(
|
320 |
safe_filename(stream.title), stream.subtype, "video", target=target
|
321 |
+
)
|
322 |
audio_unique_name = unique_name(
|
323 |
safe_filename(stream.title), stream.subtype, "audio", target=target
|
324 |
+
)
|
325 |
_download(stream=stream, target=str(target), filename=video_unique_name)
|
326 |
_download(stream=audio_stream, target=str(target), filename=audio_unique_name)
|
327 |
|
|
|
329 |
audio_path = Path(target) / f"{audio_unique_name}.{stream.subtype}"
|
330 |
final_path = Path(target) / f"{safe_filename(stream.title)}.{stream.subtype}"
|
331 |
|
332 |
+
subprocess.run([
|
333 |
+
"ffmpeg", "-i", f"{video_path}", "-i", f"{audio_path}", "-codec", "copy", f'{final_path}'
|
334 |
]
|
335 |
)
|
336 |
video_path.unlink()
|
|
|
436 |
youtube: YouTube, filetype: str, target: Optional[str] = None
|
437 |
) -> None:
|
438 |
"""
|
439 |
+
Given a filetype, downloads the highest quality available audio stream for a
|
440 |
YouTube video.
|
441 |
+
|
442 |
:param YouTube youtube:
|
443 |
A valid YouTube object.
|
444 |
:param str filetype:
|
|
|
448 |
"""
|
449 |
audio = youtube.streams.filter(
|
450 |
only_audio=True, subtype=filetype
|
451 |
+
).order_by("abr").desc().first()
|
452 |
|
453 |
if audio is None:
|
454 |
print(
|
|
|
459 |
youtube.register_on_progress_callback(on_progress)
|
460 |
|
461 |
try:
|
462 |
+
_download(audio, target=target)
|
463 |
except KeyboardInterrupt:
|
464 |
sys.exit()
|
465 |
|