-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow downloading to buffer #3298
Comments
The filename is what is passed around b/w the various classes/functions instead of a file object, with special handling for |
Indeed. See also ytdl-org/youtube-dl#30815. |
Assuming single threaded code, contextlib.redirect_stdout can be used as a workaround |
I tried the from yt_dlp import YoutubeDL
from contextlib import redirect_stdout
from pathlib import Path
youtube_id = "some-video-id"
ctx = {
"outtmpl": "-",
'logtostderr': True
}
buffer = io.BytesIO()
with redirect_stdout(buffer), YoutubeDL(ctx) as foo:
foo.download([youtube_id])
# write out the buffer for demonstration purposes
Path(f"{youtube_id}.mp4").write_bytes(buffer.getvalue()) Edit: A caveat is that this only seems to work on linux. On Windows the redirect to stdout seems to break and I get a |
This should now work in windows too |
@pukkandan, I was trying the above workaround and it works great. But If I want to download the video in a specific format
By the time it reaches the print statement the buffer is closed. Traceback (most recent call last): But this only happens when the field 'format' is present in the ctx object. Would you happen to know why this is happening and how I can fix it ? |
You can overwrite the old_buffer_close = buffer.close
buffer.close = lambda *_: ...
# YoutubeDL code
old_buffer_close() # Close when done |
Any idea on how to do this for subtitles? This is what I got so far: import io
from contextlib import *
from yt_dlp import *
from pathlib import *
youtube_id = 'https://youtube.com/watch?v=0nwbzoIP14E'
ctx = {
"outtmpl": "-",
'logtostderr': True,
'skip_download': True,
'writesubtitles': True,
'sub_langs': 'en.*',
}
buffer = io.BytesIO()
with redirect_stdout(buffer), YoutubeDL(ctx) as foo:
foo.download([youtube_id])
print(buffer.getvalue()) It's still writing the subtitles to a file and the buffer seems to be empty. |
You can't #3554 |
Checklist
Description
Although for the command line interface, almost all the time it is desirable to save the file to a local path. However when using ytdl as a module using the io library to save files to buffers may be desirable.
My use case is a bunch of chatbots which download a video from an URL and then uploads the file back to the user, allowing a interface for people with no access to a CLI or in an environment where it would be difficult to use (such as a locked smartphone).
It would be more efficient if the script can download the file to a io.BufferIO object then upload it without having to make a temporary file in the disk, as it already does with other kind of media (for example with a qr code generator).
The text was updated successfully, but these errors were encountered: