https://github.com/codingstark-dev/videotomp3
Convert any video url to an MP3 file using Python and Flask
https://github.com/codingstark-dev/videotomp3
convert flask python videotomp3
Last synced: 2 months ago
JSON representation
Convert any video url to an MP3 file using Python and Flask
- Host: GitHub
- URL: https://github.com/codingstark-dev/videotomp3
- Owner: codingstark-dev
- Created: 2023-04-05T13:56:55.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-04-05T18:34:54.000Z (almost 3 years ago)
- Last Synced: 2025-01-16T00:22:47.857Z (about 1 year ago)
- Topics: convert, flask, python, videotomp3
- Language: Python
- Homepage:
- Size: 74.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Convert any video url to an MP3 file using Python and Flask
```bash
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.

Here is a breakdown of the code:
```python
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()
```