-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp3randomchopper.py
63 lines (48 loc) · 2.09 KB
/
mp3randomchopper.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
#############################################
#############################################
# RANDOM SONG SLICER ########################
#############################################
# Jeronimo Barbosa ##########################
# jeronimo.costa [at] mail.mcgill.ca ########
# August 25th 2015 ##########################
#############################################
# dependencies:
# Pydub - https://github.com/jiaaro/pydub
# please, note Pydub also depends on libav and ffmpeg
from pydub import AudioSegment
import os, math, random
#how long my excerpts are going to be (in seconds)?
duration = 30
#where my files are?
path = '/Users/jeronimo/Documents/sbcm 2015/mumt 621 - database'
#where my raw files are?
path_raw = path +'/raw';
#creating a file containg all random positions chosen
report=open('./mp3randomchopper-report.txt', 'w+')
for root, dirs, files in os.walk(path_raw, topdown=False):
#selects only mp3 files
files = [ fi for fi in files if fi.endswith(".mp3") ]
#iterates over all mp3 files
for name in files:
file = os.path.join(root, name)
print 'processing ' + file
#processinf strings
first_part = file[:len(path)+1]
last_part = file[len(path)+4:]
print 'loading file';
song = AudioSegment.from_mp3(file)
print 'extracting excerpt';
#pydub works in milisecs, thus
duration_in_ms = int(duration*1000)
#gets the last position possible for extracting an excerpt (30s before the end)
last_possible_start_position = math.floor(song.duration_seconds - 30)
#gets a random position
pos = random.uniform(0, last_possible_start_position) * 1000
#extracts the excerpt
report.write(last_part)
report.write(" duration: " + str(song.duration_seconds))
report.write(" random pos: " + str(pos/1000.0) + "\n")
excerpt = song[pos:pos+duration_in_ms]
print 'saving new file';
new_file = first_part + 'excerpts' + last_part;
excerpt.export(new_file, format='mp3')