DEV Community

loading...
Cover image for Download Youtube video to mp3 with Python

Download Youtube video to mp3 with Python

stokry profile image Stokry ・1 min read

I often need to download videos from Youtube in mp3 format. There are many websites where you can convert Youtube videos to mp3, but I am giving you a simple Python script that does the same job.

I am using youtube_dl. Command-line program to download videos from YouTube.com and other video sites. It requires the Python interpreter, version 2.6, 2.7, or 3.2+, and it is not platform-specific. It should work on your Unix box, on Windows, or on macOS.

Let's jump to the code:

import youtube_dl
def run():
    video_url = input("please enter youtube video url:")
    video_info = youtube_dl.YoutubeDL().extract_info(
        url = video_url,download=False
    )
    filename = f"{video_info['title']}.mp3"
    options={
        'format':'bestaudio/best',
        'keepvideo':False,
        'outtmpl':filename,
    }

    with youtube_dl.YoutubeDL(options) as ydl:
        ydl.download([video_info['webpage_url']])

    print("Download complete... {}".format(filename))

if __name__=='__main__':
    run()
Enter fullscreen mode Exit fullscreen mode

Just enter the URL of the song and the script will download that song in mp3 format, cool isn't it?

Thank you all.

Discussion (16)

Collapse
bubblicious profile image
bubblicious-a

Thanks. I will try this out.

Collapse
stokry profile image
Collapse
mraffi30 profile image
mohammed raffi

Tried this code and, mind full that I am a beginner! Running this in Jupyter Notebook i didn't get prompt to add the URL?

Collapse
stokry profile image
Stokry Author

I will test this out and let you know.

Collapse
mraffi30 profile image
mohammed raffi

Thanks....awaiting your feedback.

Collapse
rowemore profile image
Rowe Morehouse

why not pytube?

pypi.org/project/pytube/

or pytube3, pytubeX

Collapse
tankerguy1917 profile image
tankerguy1917

Really cool. I tried it out and it works really fast, and flawlessly.

Collapse
stokry profile image
Collapse
langtv profile image
Tran Van Lang

it is really userfully

Collapse
stokry profile image
Collapse
jonrandy profile image
Jon Randy • Edited

Seriously, most of these "How to do X in language Y" type 'articles' should be renamed "How to call a function in language Y"

Collapse
stokry profile image
Stokry Author

Maybe, but I disagree with you.

Collapse
langtv profile image
Tran Van Lang

Help me how to convert and download to mp4 file.

Collapse
stokry profile image
Stokry Author

You can use pytube. It's lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.

Collapse
mbcrump profile image
Michael Crump

What do I get out of this vs just running the program itself.

Collapse
michaelcurrin profile image
Michael • Edited

The CLI might be sufficient for the average use case.

The docs explain how to use the CLI with options.

github.com/ytdl-org/youtube-dl#des...

I guess the downside is that you have to pass those on the command-line each time, so you can write a shell script or Python script which effectively holds all that config logic and asks for user input and runs the library.

The CLI options look comprehensive so I'd guess the features are the same. And for a simple case like download a single video, the CLI way could fit on one or two lines and save having to setup a Python script of 10 or 20 lines.

But maybe using the library in a Python script helps for a level of abstraction. Like if you want to build a web or GUI interface on top or have more control on what to download and where it is saved and making directories or printing a progress report or retrying bad URLs etc.

Forem Open with the Forem app