Skip to content
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

[Question] Downloading to io.BytesIO? #17379

Closed
hyarsan opened this issue Aug 30, 2018 · 4 comments
Closed

[Question] Downloading to io.BytesIO? #17379

hyarsan opened this issue Aug 30, 2018 · 4 comments

Comments

@hyarsan
Copy link

@hyarsan hyarsan commented Aug 30, 2018

Please follow the guide below

  • You will be asked some questions and requested to provide some information, please read them carefully and answer honestly
  • Put an x into all the boxes [ ] relevant to your issue (like this: [x])
  • Use the Preview tab to see what your issue will actually look like

Make sure you are using the latest version: run youtube-dl --version and ensure your version is 2018.08.28. If it's not, read this FAQ entry and update. Issues with outdated version will be rejected.

  • I've verified and I assure that I'm running youtube-dl 2018.08.28

Before submitting an issue make sure you have:

  • At least skimmed through the README, most notably the FAQ and BUGS sections
  • Searched the bugtracker for similar issues including closed ones
  • Checked that provided video/audio/playlist URLs (if any) are alive and playable in a browser

What is the purpose of your issue?

  • Bug report (encountered problems with youtube-dl)
  • Site support request (request for adding support for a new site)
  • Feature request (request for a new functionality)
  • Question
  • Other

The following sections concretize particular purposed issues, you can erase any section (the contents between triple ---) not applicable to your issue


Description of your issue, suggested solution and other information

How do you download a video to a filename / io.BytesIO using youtube-dl in Python 3.6?
What I'm trying to do is something like:

import youtube_dl
from io import BytesIO

outputBuffer = BytesIO()
with youtube_dl.YoutubeDL({"filename": outputBuffer}) as ydl:
    ydl.download(["https://www.youtube.com/watch?v=BaW_jenozKc"])
outputBuffer.seek(0)

but I can't get that to work.


@dstftw
Copy link
Collaborator

@dstftw dstftw commented Aug 30, 2018

You can't.

@dstftw dstftw closed this Aug 30, 2018
@hyarsan
Copy link
Author

@hyarsan hyarsan commented Aug 30, 2018

How would you download to a filename at least? @dstftw

@achalddave
Copy link

@achalddave achalddave commented Aug 15, 2019

I was able to download to a BytesIO with the following:

import logging
from contextlib import redirect_stdout
from io import BytesIO

import youtube_dl


def download_bytesio(url):
    ydl_opts = {
        'format': 'best[ext=mp4]',
        'outtmpl': '-',
        'logger': logging.getLogger()
    }

    video = BytesIO()
    with redirect_stdout(video):
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])
    return video

Example usage:

if __name__ == '__main__':
    # Example download
    video = download_bytesio('http://youtu.be/-86Te2-JCeg')
    video.seek(0)
    filename = 'tmp.mp4'
    with open(filename, 'wb') as f:
        f.write(video.read())
    print(f'Saved video to {filename}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.