Youtube to MP3 Converter with Python

Youtube to MP3 Converter with Python

Converting Video to audio files is a smart tool to extract audio from video files. In this article, I will take you through a simple program to build a video to audio converter with Python programming language.

Video to Audio Converter with Python

Converting videos to audio files might seem like an odd decision, but it can come in handy in specific cases. It is most often used to record the soundtrack of videos or to extract other audio tracks from videos where you are only interested in the sound.

If you think you can benefit from this possibility, you need an application to convert video to audio. Most importantly, you need to make sure that you choose the right one. There are already apps that do this automatically for you. But creating such things that are already happening is what turns every coder on. Let’s see how to convert video to audio using Python programming language:

from pytube import YouTube
import pytube
import os

def main():
    video_url = input('Please enter YouTube video URL: ')

    if os.name == 'nt':
        path = os.getcwd() + '\\'
    else:
        path = os.getcwd() + '/'

    name = pytube.extract.video_id(video_url)
    YouTube(video_url).streams.filter(only_audio=True).first().download(filename=name)
    location = path + name + '.mp4'
    renametomp3 = path + name + '.mp3'

    if os.name == 'nt':
        os.system('ren {0} {1}'. format(location, renametomp3))
    else:
        os.system('mv {0} {1}'. format(location, renametomp3))
    
if __name__ == '__main__':
    main()

video-to-audio.py

I hope you liked this article on how to build a video to audio converter with Python programming language. Feel free to ask your valuable questions in the comments section below.


© 2023. All rights reserved.