def convert_sound_compat(in_fname, out_fname, duration=None, rate=None, codec='pcm_s32le', ffmpeg_flags=''): """ Probably there is a better way to do this... """ duration_flag = ('-t %.7f' % duration if duration is not None else '') assert out_fname.endswith('.wav') # with ut.temp_file('.mp3') as tmp_file: # print tmp_file # ok = (0 == ut.sys_print('ffmpeg -i "%s" %s -y "%s"' % (in_fname, duration_flag, tmp_file))) # ok = ok and (0 == os.system('ffmpeg -i "%s" -y -ac 1 -acodec pcm_s32le "%s"' % (tmp_file, out_fname))) # print 'writing', out_fname, ok # #ok = ok and (0 == ut.sys_print('ffmpeg -i "%s" -y -f s16le -acodec pcm_s16le -ac 1 "%s"' % (tmp_file, out_fname))) #asdf #with ut.temp_file('.mp3') as mp3_tmp, ut.temp_file('.wav') as wav_tmp: with ut.temp_file('.mp3') as mp3_tmp, ut.temp_file('.wav') as wav_tmp: ok = (0 == ut.sys_print( 'ffmpeg -i "%s" %s -y %s "%s"' % (in_fname, duration_flag, ffmpeg_flags, mp3_tmp))) #ok = ok and (0 == os.system('ffmpeg -i "%s" -y -ac 1 -acodec pcm_s32le "%s"' % (mp3_tmp, wav_tmp))) ar_str = '-ar %d' % rate if rate is not None else '' ok = ok and (0 == os.system( 'ffmpeg -i "%s" -y -ac 1 -acodec %s %s "%s"' % (mp3_tmp, codec, ar_str, wav_tmp))) if ok: load_sound(wav_tmp).save(out_fname) print 'writing', out_fname, ok #ok = ok and (0 == ut.sys_print('ffmpeg -i "%s" -y -f s16le -acodec pcm_s16le -ac 1 "%s"' % (tmp_file, out_fname))) return ok
def play(samples_or_snd, sr=None, compress=True): if sr is None: samples = samples_or_snd.samples sr = samples_or_snd.rate else: samples = samples_or_snd snd = Sound(None, sr, samples) # .unnormalized('int16') path = ut.pjoin(imtable.get_www_path(), 'sounds') if compress: with ut.temp_file('.wav') as wav_fname: fname = ut.make_temp('.mp3', dir=path) #scipy.io.wavfile.write(wav_fname, snd.rate, snd.samples) snd.save(wav_fname) os.system('ffmpeg -loglevel error -y -i "%s" "%s"' % (wav_fname, fname)) else: fname = ut.make_temp('.wav', dir=path) scipy.io.wavfile.write(fname, snd.rate, snd.samples) os.system('chmod a+rwx %s' % fname) #url = ut.pjoin(imtable.PUBLIC_URL, 'sounds', os.path.split(fname)[1]) url = ut.pjoin(imtable.get_url(), 'sounds', os.path.split(fname)[1]) print(url) return url
def make_video(out_fname, ims, fps, tmp_ext='.ppm', sound=None): # Compressed images if type(ims[0]) == type(''): #tmp_ext = '.jpg' tmp_ext = '.png' assert tmp_ext.startswith('.') in_dir = ut.make_temp_dir_big() ut.parmap(make_video_helper, [(i, x, in_dir, tmp_ext) for i, x in enumerate(ims)]) with ut.temp_file('.wav') as tmp_wav: if sound is None: sound_flags = '' else: if type(sound) != type(''): sound.save(tmp_wav) sound = tmp_wav sound_flags = '-i "%s" -shortest' % sound # removed aac flag from above, for compatibility with some wav files cmd = ( 'ffmpeg -loglevel warning -f image2 -r %f -i %s/ao-video-frame%%05d%s %s ' '-pix_fmt yuv420p -vcodec h264 -strict -2 -y %s') % ( fps, in_dir, tmp_ext, sound_flags, out_fname) print cmd if 0 == os.system(cmd): for x in glob.glob(ut.pjoin(in_dir, 'ao-video-frame*%s' % tmp_ext)): os.remove(x) os.rmdir(in_dir) else: raise RuntimeError()