-
Notifications
You must be signed in to change notification settings - Fork 0
/
localify.py
116 lines (81 loc) · 3.3 KB
/
localify.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from flask import Flask, request, render_template, send_file
import yt_dlp
from pydub import AudioSegment
import eyed3
import tempfile
import os
import re
import zipfile
import glob
import requests
import string
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if request.form['conversion-type'] in ['YouTube Song', 'SoundCloud Song', 'TikTok Sound']:
url = request.form['link']
artist = request.form['artist-name']
song = request.form['song-name']
use_cover_art = request.form.get('use-cover-art') == 'true'
if use_cover_art and request.form['conversion-type'] == 'SoundCloud Song':
cover_art_url = get_soundcloud_thumbnail(url)
cover_data = get_image_data(cover_art_url)
else:
cover = request.files['cover-art']
cover_data = cover.read()
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': '%(title)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'verbose': True
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
audio_filename = ydl.prepare_filename(info_dict)
if not os.path.exists(audio_filename) or os.path.getsize(audio_filename) == 0:
return "Error: File not found or is empty", 500
with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as tmp_file:
tmp_filename = tmp_file.name
try:
audio = AudioSegment.from_file(audio_filename)
audio.export(tmp_filename, format='mp3')
except Exception as e:
return f"Error processing audio file: {e}", 500
audio_file = eyed3.load(tmp_filename)
audio_file.tag.title = song
audio_file.tag.album = song
audio_file.tag.album_artist = artist
audio_file.tag.artist = artist
audio_file.tag.images.set(3, cover_data, "image/jpeg")
audio_file.tag.save()
try:
response = send_file(
tmp_filename,
as_attachment=True,
mimetype='audio/mpeg'
)
response.headers['Content-Disposition'] = f"attachment; filename={song}.mp3"
return response
finally:
os.remove(audio_filename)
os.remove(tmp_filename)
# Similar code for handling 'YouTube Playlist' or 'SoundCloud Playlist'
return render_template('index.html')
def get_soundcloud_thumbnail(url):
soundcloud_api_url = f"https://soundcloud.com/oembed?url={url}&format=json"
response = requests.get(soundcloud_api_url)
response_data = response.json()
return response_data.get('thumbnail_url', '')
def get_image_data(url):
response = requests.get(url)
return response.content
def sanitize_filename(filename):
valid_chars = f"-_.() {string.ascii_letters}{string.digits}"
return ''.join(c for c in filename if c in valid_chars)
if __name__ == "__main__":
app.run()