Arts >> Music >> Music Basics

Can you show me a program to down load free music?

Here' a simple Python program to download free music legally from various sources like YouTube, SoundCloud and Bandcamp:

```python

import os

from pytube import YouTube

import requests

import soundcloud

from bandcamp import Bandcamp

from bs4 import BeautifulSoup

Download Video from YouTube

def YouTube_Downloader(url):

try:

# object creation using YouTube

# which was imported in the beginning

yt = Youtube(url)

# formats and resolutions that can be

# downloaded as per your system will show

# here

print("Available Resoultions: ")

all_res = yt.streams.all()

for i in all_res:

print(i)

# the highest resolution video to be

# downloaded is selected

ys = yt.streams.get_highest_resolution()

# path were all the videos will get saved

yt_path = os.getcwd()

# video gets downloaded to the specified

# location

ys.download(yt_path)

print("Download Completed : ", yt_path)

except Exception as e:

print(str(e))

Download Audio form Soundcloud

def Soundcloud_Downloader(url):

try:

# path were all the audios will get saved

sc_path = os.getcwd()

# object creation using soundcloud

client = soundcloud.Client()

# track = client.get(url) is an object

# but as we want only a streamable audio

# URL for downloading so 'stream_url'

audio = client.get(url, resolve = False)

stream_url = audio.stream_url

# get request to get the contents

response = requests.get(stream_url, stream = True)

# name the audio file

# here I am naming the audio file same as

# the track title with .mp3 as an extension

filename = sc_path + '/' + audio.title.replace("/", "") + ".mp3"

# open method in python opens the file in write

# mode to save the contents in the file. Here,

# the audio file will be stored in the system.

with open(filename, "wb") as f:

for chunk in response.iter_content(chunk_size=1024):

if chunk:

f.write(chunk)

print("Download Completed: ", filename)

except Exception as e:

print(str(e))

Download Album from Bandcamp

def Bandcamp_Downloader(url):

try:

# path were all the audios will get saved

bc_path = os.getcwd()

# the function takes url of the album as an

# argument and returns the object for that

# particular album

album = Bandcamp(url)

# the following for loops prints the track name

# and URL of all the tracks present in the

# album

for track in album.tracks():

print("Track: ", track.title, track.url)

# choose any download format here I choose

# "mp3-v0" format that is mp3 of 320k

format = "mp3-v0"

# function will download the song/audio with

# user entered name and downloads the audio in

# specified format

for track in album.tracks():

track.download(bc_path, track.title.replace("/","")+"."

+ format)

print("Download Completed: ", bc_path)

except Exception as e:

print(str(e))

def main():

while True:

print("Select an option:")

print("1. YouTube Downloader")

print("2. SoundCloud Downloader")

print("3. Bandcamp Downloader")

print("4. Exit")

choice = input("Enter your choice: ")

if choice == "1":

url = input("Enter YouTube URL: ")

YouTube_Downloader(url)

elif choice == "2":

url = input("Enter Soundcloud URL: ")

Soundcloud_Downloader(url)

elif choice == "3":

url = input("Enter Bandcamp URL: ")

Bandcamp_Downloader(url)

elif choice == "4":

print("Exiting...")

break

if __name__ == "__main__":

main()

```

Music Basics

Related Categories