Skip to content

Commit

Permalink
creation: trim audio based on measured duration (will fix #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeff committed Jun 17, 2020
1 parent 33649fc commit 840b337
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,30 +166,19 @@ def extract_mp3_metadata():
metadata.to_csv('mp3_metadata.csv')


def convert_duration(x):
times = x.split(':')
seconds = int(times[-1])
minutes = int(times[-2])
try:
minutes += 60 * int(times[-3])
except IndexError:
pass
return seconds + 60 * minutes


def trim_audio(dst_dir):

dst_dir = os.path.abspath(dst_dir)
fma_full = os.path.join(dst_dir, 'fma_full')
fma_large = os.path.join(dst_dir, 'fma_large')
tracks = pd.read_csv('raw_tracks.csv', index_col=0)
tracks = pd.read_csv('mp3_metadata.csv', index_col=0)
_create_subdirs(fma_large, tracks)

not_found = pickle.load(open('not_found.pickle', 'rb'))
not_found['clips'] = []

for tid in tqdm(tracks.index):
duration = convert_duration(tracks.at[tid, 'track_duration'])
for tid, track in tqdm(tracks.iterrows(), total=len(tracks)):
duration = track['samples'] / track['sample_rate']
src = utils.get_audio_path(fma_full, tid)
dst = utils.get_audio_path(fma_large, tid)
if tid in not_found['audio']:
Expand All @@ -199,7 +188,7 @@ def trim_audio(dst_dir):
elif duration <= 30:
shutil.copyfile(src, dst)
else:
start = duration // 2 - 15
start = int(duration // 2 - 15)
command = ['ffmpeg', '-i', src,
'-ss', str(start), '-t', '30',
'-acodec', 'copy', dst]
Expand Down

0 comments on commit 840b337

Please sign in to comment.