-
Notifications
You must be signed in to change notification settings - Fork 10
/
converter.py
50 lines (38 loc) · 1.33 KB
/
converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pafy
import os
from pydub import AudioSegment
SERVER_URL = 'http://52.232.85.160/'
def download_audio(video_id):
if not video_id:
return {"status": False, "error": "Video id not specified"}
url = get_song(video_id)
if url:
return {"status": True, "url": url}
try:
video = pafy.new(str(video_id))
except ValueError as e:
print(e)
return {"status": False, "error": "Invalid video id specified"}
streams = video.audiostreams
if len(streams) > 0:
filename = "./dist/" + video_id + '.webm';
video.getbestaudio().download(filepath=filename, quiet=True)
convert_to_mp3(filename, video_id)
os.remove(filename)
song_url = SERVER_URL + video_id
return {"status": True, "url": song_url}
return {"status": False, "error": "No audio streams found"}
def get_audio_url(vId):
try:
video = pafy.new(vId)
url = video.getbestaudio(preftype='m4a').url
return {"status": True, "url": url}
except:
return {"status": False, "error": "No audio streams found"}
def get_song(vid):
path = './dist/' + vid + '.mp3'
if(os.path.exists(path)):
return SERVER_URL + vid
return None
def convert_to_mp3(path, vid):
AudioSegment.from_file(path).export("./dist/" + vid + ".mp3", format="mp3")