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

Choosing output file name in Python API #5192

Closed
Zulko opened this issue Mar 13, 2015 · 8 comments
Closed

Choosing output file name in Python API #5192

Zulko opened this issue Mar 13, 2015 · 8 comments

Comments

@Zulko
Copy link

@Zulko Zulko commented Mar 13, 2015

Hi there, I am trying to download a video using the python API:

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])

I can't figure what to put in the options to be able to set the output filename. Am I missing something obvious ? Thanks in advance.

@phihag
Copy link
Contributor

@phihag phihag commented Mar 13, 2015

Use the outtmpl parameter:

from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
    'outtmpl': '/tmp/foo_%(title)s-%(id)s.%(ext)s'
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
@phihag phihag closed this Mar 13, 2015
@Zulko
Copy link
Author

@Zulko Zulko commented Mar 13, 2015

Thanks, but that doesn't work for me. To be more precise: at the command line i can write

youtube-dl Nh11A41klL4 pigs.mp4

In the Python version I did not manage to force pigs.mp4 as an outputname . If my name is not a proper template like foo_%(title)s-%(id)s.%(ext)s I get an assertion error. Any idea ? Thanks for your time.

@phihag
Copy link
Contributor

@phihag phihag commented Mar 13, 2015

youtube-dl Nh11A41klL4 pigs.mp4 will try to download pigs.mp4:

$ youtube-dl Nh11A41klL4 pigs.mp4
[youtube] Nh11A41klL4: Downloading webpage
[youtube] Nh11A41klL4: Extracting video information
[youtube] Nh11A41klL4: Downloading DASH manifest
[download] Destination: Looney Tunes - Pigs in a Polka 1943-Nh11A41klL4.mp4
[download] 100% of 36.02MiB in 00:04
ERROR: u'pigs.mp4' is not a valid URL. Set --default-search "ytsearch" (or run  youtube-dl "ytsearch:pigs.mp4" ) to search YouTube

Please do post the error. Without it, we cannot help you.

@phihag
Copy link
Contributor

@phihag phihag commented Mar 13, 2015

One more note: If you are still using Python 2.x, you must make sure to pass in unicode and not byte objects. The simplest way to do this is to add from __future__ import unicode_literals to the top of your file.

phihag added a commit that referenced this issue Mar 13, 2015
Also adapt the embedding examples for those poor souls still using 2.x.
@Zulko
Copy link
Author

@Zulko Zulko commented Mar 13, 2015

Sorry for that, I meant

youtube-dl Nh11A41klL4 -o pigs.mp4

This command works and downloads the video as pig.mp4. Then, when I try the Python API version with this code:

import youtube_dl
def download_webfile(url, filename=None, **ydl_opts):
    if filename is not None:
        ydl_opts["outtmpl"]= filename
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])

# This will work and import the video under its youtube name
#download_webfile("Nh11A41klL4")

# When I try to specify a filename using "outtmpl" it crashes with Assertion Error
download_webfile("Nh11A41klL4", "pigs.mp4")

I get this error. Importing unicode_literals did not solve the problem.

@phihag
Copy link
Contributor

@phihag phihag commented Mar 13, 2015

Can you post the error you get when you add the from __future__ import unicode_literals line?

@phihag phihag reopened this Mar 13, 2015
@Zulko
Copy link
Author

@Zulko Zulko commented Mar 13, 2015

Wait, it works ! My mistake with unicode_literal was that I imported it in the file where my function download_webfile is defined, while I should have imported it in the file where I call download_webfile("Nh11A41klL4", "pigs.mp4") (dumb me).

Also, in python2 simply writing ydl_opts["outtmpl"]= unicode(filename) seems to solve the problem.

I believe it's solved. Thanks for your time !

@jaimeMF jaimeMF closed this Mar 13, 2015
@BiTinerary
Copy link

@BiTinerary BiTinerary commented Jan 15, 2017

ydl_opts = {'outtmpl' : unicode(customName)}
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([URLofVideo])

In terms of renaming the downloaded video, this also worked for me.

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
4 participants
You can’t perform that action at this time.