def tags_to_wav(media_path,out_dir,tag_pairs):
basename=os.path.splitext(os.path.basename(media_path))[0]
wav_source=True
if media_path.lower()[-4:] not in ('.mp3','.wav'): # Creates a temporary WAV
wav_source=False # if input is MP4
temp_filename=media_path.split('/')[-1]+'_temp.wav'
audio_path='/var/tmp/'+temp_filename # Pathname for temp WAV
subprocess.call(['ffmpeg', '-y', '-i', media_path, audio_path]) # '-y' option overwrites existing file if present
else:
audio_path=media_path
try:
if audio_path[-4:].lower()=='.mp3':
song = AudioSegment.from_mp3(audio_path)
else:
song = AudioSegment.from_wav(audio_path)
except Exception as inst:
print(inst)
sys.exit(2)
for pair in tag_pairs:
start = pair[0]
duration = pair[1]-pair[0]
clip_pathname=os.path.join(out_dir,basename+"_start_"+str(start)+"_dur_"+str(duration)+".wav")
start_msec = float(start) * 1000.0
duration_msec = float(duration) * 1000
if not os.path.exists(clip_pathname):
clip_data = song[start_msec:start_msec+duration_msec]
clip_data=clip_data.set_channels(1)
clip_data.export(clip_pathname, format="wav")
评论列表
文章目录