Skip to content

Convert any video url to an MP3 file using Python and Flask

Notifications You must be signed in to change notification settings

codingstark-dev/videotomp3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Convert any video url to an MP3 file using Python and Flask

git clone https://github.com/codingstark-dev/videotomp3.git

The endpoint uses the moviepy library to extract the audio from the video at the provided URL, saves it as an MP3 file, reads the file as bytes, removes the file, and returns the bytes as a response with the MIME type audio/mpeg.

image

Here is a breakdown of the code:

from flask import Flask, request, Response
import moviepy.editor as mp
import io
import os

app = Flask(__name__)

@app.route('/convert', methods=['GET'])
def convert():
    video_url = request.args.get('url')
    # video_url = request.query_string['video_url']
    video = mp.VideoFileClip(video_url)
    audio = video.audio
    audio_path = os.path.join(os.getcwd(), 'audio.mp3')
    audio.write_audiofile(audio_path)
    with open(audio_path, 'rb') as f:
        audio_bytes = f.read()
    os.remove(audio_path)
    return Response(audio_bytes, mimetype="audio/mpeg")

if __name__ == '__main__':
    app.run()

About

Convert any video url to an MP3 file using Python and Flask

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages