-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.py
More file actions
50 lines (43 loc) · 1.52 KB
/
audio.py
File metadata and controls
50 lines (43 loc) · 1.52 KB
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
from pydub import AudioSegment
import requests
import string
def download_file(mp3_file_name, file_type):
"""
This check if is a url and donwload the file
in files directory with podcast.mp3 filename.
"""
remotefile = requests.get(mp3_file_name, headers={"User-Agent":"Wget/1.19.4 (linux-gnu)"})
# Set different file name if is jingle or podcast file.
result_file="files/{}.mp3".format(file_type)
with open(result_file,'wb') as output:
output.write(remotefile.content)
return result_file
def load_mp3(mp3_file_name, file_type):
"""
This tries to load the audio from a named mp3 file.
It checks the filename has mp3 extension.
"""
if mp3_file_name.startswith('https://') or mp3_file_name.startswith('http://'):
mp3_file_name = download_file(mp3_file_name, file_type)
if not mp3_file_name.lower().endswith('.mp3'):
raise SystemExit(
'Incorrect audio file format. The file must have .mp3 extension'
)
return AudioSegment.from_mp3(mp3_file_name)
def get_jingles(song_file_name):
"""
This function returns both starting and ending
jingles.
"""
song = load_mp3(song_file_name, "jingle")
return song[:20000], song[-40000:]
def glue_tracks(tracks):
"""
This functions glues all tracks in a single one,
using the specified fade for each track, and
returns the resulting audio.
"""
final = tracks[0][0]
for audio, fade in tracks[1:]:
final = final.append(audio, crossfade=fade)
return final