def record_button_clicked(self, widget, mode): audio_jack = AudioJack.get_thread() if mode == "start": if self.record_filename is None: filename = FileOp.choose_file(self, purpose="save_as", file_types="audio") if filename: self.set_record_filename(filename) self.record_clock.reset() else: self.record_pause_button.show() self.record_stop_button.show() self.record_start_button.hide() audio_jack.record = True self.record_clock.start() elif mode == "pause": self.record_pause_button.hide() self.record_stop_button.show() self.record_start_button.show() audio_jack.record = False self.record_clock.pause() elif mode == "stop": self.record_pause_button.hide() self.record_start_button.show() self.record_stop_button.hide() audio_jack.record = False self.record_clock.pause() if self.record_filename: mid_filename = self.record_filename + ".{0}.wav".format(time.time()) if audio_jack.save_record_file_as(mid_filename): clip = AudioFileClip(mid_filename) clip.write_audiofile(self.record_filename) os.remove(mid_filename) self.load_audio_samples(self.record_filename) self.set_filename(self.record_filename) self.set_record_filename("")
def read_file(self): start = self.num_calls * self.seq_length end = start + self.seq_length # Read label file, and get start-end timestamps labels, timestamps = self._read_label_file(start, end) clip = AudioFileClip(str(self.raw_file_path), fps=self.fps) num_seqs = self.num_seqs frames = [] for i, t in enumerate(timestamps[:-1]): start_time = timestamps[i] end_time = timestamps[i + 1] data_frame = np.array( list(clip.subclip(start_time, end_time).iter_frames())) data_frame = data_frame.mean(1)[:self.num_samples] if data_frame.shape[0] < self.num_samples: data_frame = np.pad(data_frame, (0, self.num_samples - data_frame.shape[0] % self.num_samples), 'constant') frames.append(data_frame.astype(np.float32)) frames = np.array(frames).astype(np.float32) labels = np.array(labels).astype(np.float32) return frames, labels
def Audio_Visual_Separation(self, Video_Path, Video_Name, Audio_Path=None, Audio_Name=None): """ 音频分离函数 首先检查地址是否存在,默认只需要视频地址和视频名称,音频默认存放之视频地址下 视频是MP4格式,音频默认是MP3格式 :param Video_Path:视频地址 :param Video_Name: 视频名 :param Audio_Path: 音频地址 :param Audio_Name: 音频名称 :return: 分离好的音频 """ self.Video_Path = Video_Path self.Video_Name = Video_Name if Audio_Path is not None: self.Audio_Path = Audio_Path else: self.Audio_Path = Video_Path if Audio_Name is not None: self.Audio_Name = Audio_Name else: self.Audio_Name = Video_Name.replace(".mp4", ".mp3") if self._path_check(self.Video_Path + self.Video_Name): if self._path_check(self.Audio_Path): __video_path = self.Video_Path + self.Video_Name __audio_path = self.Audio_Path + self.Audio_Name my_audio_clip = AudioFileClip(__video_path) my_audio_clip.write_audiofile(__audio_path) else: print('error! :audio file path cant find') else: print('error! :video file or file path cant find')
def play(file_name, temp_dir): """Plays a random audio sample of the file with the provided name""" audio = Audio(file_name) start = randint(0, floor(audio.duration - DURATION)) file_path = path.join(temp_dir, str(time()) + '.mp3') audio.subclip(start, start + DURATION).write_audiofile(file_path, verbose=False, progress_bar=False) terminal(file_path)
def vid_from_media(vid_path, media, song_path, titles, img_duration=8): clips = [] print "sequencing media..." for m in media: print m.path if good_file(m.path): try: if is_img(m.path): new_clip = ImageClip(m.path) new_clip.fps = 1.0 / img_duration new_clip.duration = img_duration else: new_clip = VideoFileClip(m.path) text = m.title if titles else None new_clip = format_clip(new_clip, text) clips.append(new_clip) except Exception as err: "COULDN'T CREAT CLIP BECAUSE: " + str(err) else: print 'CORRUPT FILE FOUND: ' + m.path + ', skipping.' vid = concatenate_videoclips(clips) print song_path audio = AudioFileClip(song_path) audio_loops = int(vid.duration / audio.duration) + 1 #times to loop audio audio = concatenate_audioclips([audio] * audio_loops) print audio.duration print vid.duration audio = audio.set_duration(vid.duration) vid = vid.set_audio(audio) print "writing video..." vid.write_videofile(vid_path, progress_bar=False, preset='ultrafast') return abspath(vid_path)
class VideoBarrier(object): """docstring for VideoBarrier""" CLIP_DIR = "clip" def __init__(self, input, output, audio_path): super(VideoBarrier, self).__init__() self.input = input self.filename, _ = os.path.splitext(os.path.basename(input)) self.output = os.path.join(output, VideoBarrier.CLIP_DIR) self.audio_path = audio_path def __enter__(self): self.v_clip = VideoFileClip(self.input, audio=False) self.a_clip = AudioFileClip(self.audio_path) return self def __exit__(self, exception_type, exception_value, traceback): self.v_clip.close() self.a_clip.close() def set_audio(self, t_start=0, t_end=None): self.v_clip = self.v_clip.set_audio(self.a_clip.subclip( t_start, t_end)) def save(self, start_time=0, duration=60): audio_fname, _ = os.path.splitext(os.path.basename(self.audio_path)) output_fname = "{}_{}.mp4".format(self.filename, audio_fname) output_path = os.path.join(self.output, output_fname) self.v_clip.set_duration(duration + 1).subclip( t_start=start_time, t_end=start_time + duration).write_videofile(output_path)
def separate_audio(file_path, save_path): audio_file = save_path + '\\tmp.wav' audio = AudioFileClip(file_path) audio.write_audiofile(audio_file, ffmpeg_params=['-ar', '16000', '-ac', '1'], logger=None) return audio_file
def create_video(self, effect): image = ImageClip(self.files['png_' + effect]) if effect == 'nightcore': sound = AudioFileClip(self.files['wav_speed']) if effect == 'chipmunks': sound = AudioFileClip(self.files['wav_pitch_up']) if effect == 'male': sound = AudioFileClip(self.files['wav_pitch_down']) if sound.duration > 600 or sound.duration < 60: return 'audio too short or too long' if effect == 'nightcore': image_pub = ImageClip('resources/backgrounds/nightcorizer.png') image_pub = image_pub.set_duration(20) image = image.set_duration(sound.duration - 20) final_video = concatenate_videoclips([image, image_pub], method="compose") else: image = image.set_duration(sound.duration) final_video = concatenate_videoclips([image], method="compose") final_video = final_video.set_audio(sound) final_video.write_videofile(self.create_file('_' + effect + '.mp4'), fps=20, preset='ultrafast', threads=4, progress_bar=False, verbose=False) self.files['mp4_' + effect] = self.create_file('_' + effect + '.mp4')
def getVideo(id): url = "http://coub.com/api/v2/coubs/" + str(id) r = requests.get(url) data = r.json()["file_versions"]["html5"] try: videoUrl = data["video"]["high"]["url"] if 'high' in data[ "video"] else data["video"]["med"]["url"] audioUrl = data["audio"]["high"]["url"] if 'high' in data[ "audio"] else data["audio"]["med"]["url"] except KeyError: return False video, videoName = getFile(videoUrl) audio, audioName = getFile(audioUrl) saveBinaryFile(videoName, video) saveBinaryFile(audioName, audio) videoclip = VideoFileClip(videoName) audioclip = AudioFileClip(audioName) videoclip = normalize(videoclip) videoclip = videoclip.set_audio(audioclip.subclip(0, videoclip.duration)) garbage.append(videoName) garbage.append(audioName) return videoclip
def divide_audio(audio_folder, num_parts, parts_folder, audio_part_prefix, lazy_update): get_subpart_path = lambda num_part: get_path( parts_folder, f"{audio_part_prefix}{num_parts}_part_", num_part, "mp3") if all( os.path.exists(get_subpart_path(i)) for i in range(1, num_parts + 1)) and lazy_update: print( f"All the divisions for {num_parts} num_parts already exist and lazy_update is set to True, so we are skipping this." ) return audio = AudioFileClip(audio_folder) total_duration = audio.duration part_duration = total_duration / num_parts for i in range(1, num_parts + 1): t_start, t_end = part_duration * (i - 1), part_duration * i #audio = None #need to create the object again because subclip updates audio_i = audio.coreader().subclip( t_start, t_end) #the coreader() creates a new copy, each one for each piece print(f"Trying to write part #{i}\n") print(f"audio goes from {t_start}s to {t_end}s\n") audio_i.write_audiofile(get_subpart_path(i)) #audio_i.close() print(f"Finished writing part #{i}") audio.close() print(f"Finish writing all parts in {parts_folder}")
def __init__(self, config): self.config = config self.input_locale = config.input_locale self.input_language = config.input_language self.target_locales = config.target_locales self.target_languages = config.target_languages # Production path contains final videos and audio self.full_path = config.prod_path # Dev path contains in between audio segments and transcriptions self.dev_path = config.dev_path self.client = config.client self.sentences = [] self.tracks = {} if not config.is_video(): self.audio_clip = AudioFileClip( os.path.join(config.prod_path, config.input_media_file)) self.save_original_audio_as_flac() if config.use_background_music: self.background_music, self.background_samplerate = sf.read( config.background_music_path, always_2d=True) if config.use_best_voices: self.client.load_best_voices(config.best_voices_path, self.target_locales)
def plotstft(audiopath, binsize=2**10, plotpath=None, colormap="jet"): audio = AudioFileClip(audiopath) samplerate, samples = audio.fps, audio.to_soundarray() s = stft(samples, binsize) sshow, freq = logscale_spec(s, factor=1.0, sr=samplerate) ims = 20.*np.log10(np.abs(sshow)/10e-6) # amplitude to decibel timebins, freqbins = np.shape(ims) plt.figure(figsize=(15, 7.5)) plt.imshow(np.transpose(ims), origin="lower", aspect="auto", cmap=colormap, interpolation="none") plt.colorbar() plt.xlabel("time (s)") plt.ylabel("frequency (hz)") plt.xlim([0, timebins-1]) plt.ylim([0, freqbins]) xlocs = np.float32(np.linspace(0, timebins-1, 5)) plt.xticks(xlocs, ["%.02f" % l for l in ((xlocs*len(samples)/timebins)+(0.5*binsize))/samplerate]) ylocs = np.int16(np.round(np.linspace(0, freqbins-1, 10))) plt.yticks(ylocs, ["%.02f" % freq[i] for i in ylocs]) if plotpath: plt.savefig(plotpath, bbox_inches="tight") else: plt.show() plt.clf()
def convert_to_wav(source_path): clip = AudioFileClip(source_path) wav_path = tempfile_path('.wav') # NOTE: -ac stands for 'audio channels' # Force mono since Google Cloud STT only accepts mono audio ffmpeg_params = ['-ac', '1'] clip.write_audiofile(wav_path, ffmpeg_params=ffmpeg_params) return wav_path
def extract_sound_from_video(video_path): #[s3_key]_[randomized_file_name] print('Extracting sound from video ['+video_path+']') # ./[input_name]_[random_string].[file_extension] -> ./[input_name]_[random_string].mp3 tmp_extracted_sound_path = './'+os.path.basename(video_path).split('.')[0]+'.mp3' audio = AudioFileClip(video_path) audio.write_audiofile(tmp_extracted_sound_path) print('Sound extracted and saved under ['+tmp_extracted_sound_path+']') return tmp_extracted_sound_path
def main(): file_name = '/home/uscc/USAI_Outsourcing/SBIR_fight_videos/ced_info_SBIR.csv' output_file1 = 'ucfInfo/ced_training.csv' output_file2 = 'ucfInfo/ced_testing.csv' v_ext = '.mp4' a_ext = '.mp3' subclips_info = pd.read_csv(file_name) # if os.path.exists(output_file1): # csv_writer = csv.writer(open(output_file1, 'a')) # else: # csv_writer = csv.writer(open(output_file1, 'w')) # csv_writer.writerow(['name', 'label', 'score']) # if os.path.exists(output_file2): # csv_writer2 = csv.writer(open(output_file2, 'a')) # else: # csv_writer2 = csv.writer(open(output_file2, 'w')) # csv_writer2.writerow(['name', 'label', 'score']) for index, row in subclips_info.iterrows(): # if index < 316: # continue print('[{}/{}] {}'.format(index, len(subclips_info), row.Name)) # print('Name: {row.Name}\t' # 'Label: {row.label}\t' # 'start: {row.start}\t' # 'end: {row.end}\t' # 'score: {row.score}'.format(row=row)) video_name = row.Name + v_ext audio_name = row.Name + a_ext output_name = row.Name[:-11] + 'v{:04d}_{}_{:03d}_n'.format( index, row.label, row.score) # if random() < 0.9: # csv_writer.writerow([output_name, row.label, row.score]) # else: # csv_writer2.writerow([output_name, row.label, row.score]) ## cut vedio and audio vedio_clip = VideoFileClip(video_name).subclip(row.start, row.end) audio_clip = AudioFileClip(audio_name).subclip(row.start, row.end) vedio_clip.write_videofile(output_name + v_ext) audio_clip.write_audiofile(output_name + a_ext) ## replace origional file os.remove(row.Name + v_ext) os.remove(row.Name + a_ext) os.rename( row.Name[:-11] + 'v{:04d}_{}_{:03d}_n'.format(index, row.label, row.score) + v_ext, row.Name[:-11] + row.Name[50:-12] + v_ext) os.rename( row.Name[:-11] + 'v{:04d}_{}_{:03d}_n'.format(index, row.label, row.score) + a_ext, row.Name[:-11] + row.Name[50:-12] + a_ext)
def video_render(txt_file,image_file,sound_file,save_file): from moviepy.editor import ImageClip from moviepy.editor import CompositeVideoClip from moviepy.editor import CompositeAudioClip from moviepy.editor import TextClip from moviepy.editor import AudioFileClip from moviepy.editor import concatenate from moviepy.config import change_settings change_settings({"IMAGEMAGICK_BINARY": "/usr/local/bin/convert"}) text=[] with open(txt_file,'r') as file: for lines in file: if lines!="\n": text.append(lines.rstrip('\n')) durs=[] for i in text: res = len(re.findall(r'\w+', i)) if res/2>3: durs.append(res/2) else: durs.append(3) total_duration=sum(durs) a_clip = AudioFileClip(sound_file) if a_clip.duration<total_duration: new_audioclip = CompositeAudioClip([a_clip, a_clip.set_start(a_clip.duration-1)]).set_duration(total_duration+3) else: new_audioclip=a_clip.set_duration(total_duration+3) screen=(1920,1080) clip_list = [] i=0 for string in text: duration=durs[i] i+=1 try: txt_clip = TextClip(string, fontsize = 70, color = 'white', method='caption',size=screen ).set_duration(duration).set_pos('center') clip_list.append(txt_clip) except UnicodeEncodeError: txt_clip = TextClip("Issue with text", fontsize = 70, color = 'white').set_duration(2) clip_list.append(txt_clip) final_text_clip = concatenate(clip_list, method = "compose").set_start(3) v_clip = ImageClip(image_file).set_duration(total_duration+3) video=CompositeVideoClip([v_clip, final_text_clip]) # video = video.set_audio(AudioFileClip('sound/Serenity (1).mp3')) video = video.set_audio(new_audioclip) video.write_videofile(save_file, codec='libx264', fps=10, threads=4, audio_codec='aac', temp_audiofile='temp-audio.m4a', remove_temp=True )
def tracks(): return concatenate_audioclips([ AudioFileClip(assets(MODULE_NAME, 'track1.mp3')) .fx(afx.volumex, 0.5), AudioFileClip(assets(MODULE_NAME, 'track2.mp3')) .fx(afx.volumex, 0.5), AudioFileClip(assets(MODULE_NAME, 'track3.mp3')) .fx(afx.volumex, 0.5), ])
def trim_original_audio_to_audio_subclips(input_video_file_name, matched_time_section, output_file_name): """ save edited audio where my bias appeared in video """ audio = AudioFileClip(input_video_file_name) audio_subclips = [audio.subclip(s, e) for (s, e) in matched_time_section] editted_audio = concatenate_audioclips(audio_subclips) editted_audio.write_audiofile(output_file_name)
def outro(): outroimg = pathlib.Path(RESOURCES + "/images/outputMoment.jpg") audio = AudioFileClip(pathlib.Path(RESOURCES + "/sounds/outroaud.wav")) music = AudioFileClip(pathlib.Path(RESOURCES + "/sounds/jazz_lounge.mp3")) final_audio = CompositeAudioClip([audio, music]) outro = ImageClip(outroimg) outro = outro.set_fps(24) outro = outro.set_audio(final_audio) outro = outro.set_duration(30) outro.write_videofile(pathlib.Path(RESOURCES + "/vids/outro.mp4"))
def main_path(source_path, download_yt_captions, convert_wav): if convert_wav: src = AudioFileClip(source_path) filename = os.path.basename(source_path) name, _ = os.path.splitext(filename) wav_path = os.path.join(tempdir_path(), name + '.wav') src.write_audiofile(wav_path) return wav_path else: return source_path
def covertMp4Towav(self, inputName, outputName): """convert mp4 file to wave Arguments: inputName {string} -- file name outputName {string} -- file name after converting """ audioclip = AudioFileClip(inputName) audioclip.write_audiofile(outputName)
def mp4(self): this_dir = os.listdir(self.thumbnail_dir) self.logging.log.info("Finding the pictures from {}".format(this_dir)) # finding all the pics with jpg extension filepaths = [ os.path.join(self.thumbnail_dir, fname) for fname in this_dir if fname.endswith("jpg") ] directory = {} for root, dirs, files in os.walk(self.thumbnail_dir): for fname in files: filepath = os.path.join(root, fname) try: key = fname.replace(".jpg", "") except: key = None if key != None: directory[key] = filepath new_path = [] for k in sorted(directory.keys()): filepath = directory[k] new_path.append(filepath) # Setting Image time duration self.logging.log.info("Setting start and end audio duration") pic_duration = (int(self.end_dur) - int(self.start_dur)) / len(new_path) clips = [ ImageClip(m).set_duration(pic_duration).crossfadein(0.5) for m in new_path ] # Concatinate all the videos concat_clip = concatenate_videoclips(clips, method="compose") # Find downloaded song from youtuebe & if not found pick any random from liberary self.logging.log.info("Fetching song ....") song = self.get_downloaded_song() self.logging.log.info("Setting background music") background_audio_clip = AudioFileClip(song) bg_music = background_audio_clip.subclip(self.start_dur, self.end_dur) self.logging.log.info("Preparing final video") final_clip = concat_clip.set_audio(bg_music) final_clip.write_videofile(os.path.join(self.output_video, "final.mp4"), codec='libx264', audio_codec="aac", fps=24) return self.output_video + "\\final.mp4"
def visualize(model_cls, input_data): os.environ["FFMPEG_BINARY"] = "ffmpeg" model = model_cls() output = model.encode(input_data) output = output.reshape(output.shape[0] * 512, 128) min_val = np.amin(output) max_val_normalized = np.amax(output) - min_val last_percentage = -1 figures = [] # (graph total duration / graph datapoint count) * (graph datapoint count / graph width) figure_snapshot_rate = 40 tick_to_sample_ratio = 32.87890625 # This is still off sync with the audio, 2:53 becomes 2:58 for some reason frame_duration = (figure_snapshot_rate * tick_to_sample_ratio) / 44100 for i in range(128): column = i % 16 row = int(i / 16) figures.append(Figure(60, 60, row, column, frame_duration)) print(f"Rendering output: {output.shape}") for index, entry in enumerate(output): should_snapshot = index % figure_snapshot_rate == 0 for plot_index, plot in enumerate(figures): plot.push((entry[plot_index] - min_val) / max_val_normalized) if should_snapshot: plot.snapshot() percentage = int(index / len(output) * 100) if percentage % 1 == 0 and last_percentage != percentage: last_percentage = percentage print(f"Capturing figures: {percentage}%...") print(f"{len(figures[0].figures)} figure frames rendered") clips = [FigureClip(figure) for figure in figures] audio_filename = f"vis/output.wav" output = model.predict_output(input_data).flatten() write_wav(audio_filename, output) del model backend.clear_session() audio = AudioFileClip(audio_filename) audio = audio.set_start(0) audio = audio.set_duration( min(audio.duration, frame_duration * len(figures[0].figures))) result = CompositeVideoClip(clips, size=(16 * 66 + 12, 8 * 66 + 12)) result = result.set_audio(audio) result.write_videofile("vis/output.mp4", fps=1 / frame_duration)
def getAudioData(path): """ Get audio data (sound, sr, fps, duration, andio) :param path: string - path of audio file source """ from moviepy.editor import AudioFileClip audio = AudioFileClip(path) fps = 24 sound = audio.to_soundarray() sr = int(sound.shape[0] / audio.duration) return sound, sr, fps, audio.duration, audio
def generate_audio_volume_array(audio_input): ''' Generate a list of volumes for an audio input audio_input : Either a AudioFileClip instance, or the name of a file ''' if not isinstance(audio_input, AudioFileClip): audio_input = AudioFileClip(audio_input) cut = lambda i: audio_input.subclip(i, i+1).to_soundarray(fps=44100) volume = lambda array: np.sqrt(((1.0*array)**2).mean()) volumes = [volume(cut(i)) for i in range(0, int(audio_input.duration-1))] return volumes
def set_up_audio_clip(audio_resource): """MBE MUSS NOCH VON MALTE GESCHRIEBEN WERDEN; WAS MACHT DAS DING??""" # Create an audio file of the audio resource tmp = AudioFileClip(audio_resource[0]) # Give the audio clip a property with all audios used to create the final one tmp.clips = audio_resource[1] # Return the audio clip return tmp
def generate_fingerprint(fname: str) -> List[int]: """return an audio fingerprint for a video file. """ audioclip = AudioFileClip(fname) audioclip.write_audiofile("temp.wav") duration, fp_encoded = acoustid.fingerprint_file("temp.wav") fingerprint, version = decode_fingerprint(fp_encoded) os.remove("temp.wav") return fingerprint
def add_audio_to_video(video_file_path, audio_file_path, target_file_path): videoclip = VideoFileClip(video_file_path) audioclip = AudioFileClip(audio_file_path) audioclip = audioclip.subclip(0, videoclip.duration) new_audioclip = CompositeAudioClip([audioclip]) # videoclip.audio = new_audioclip videoclip2 = videoclip.set_audio(new_audioclip) videoclip2.write_videofile(target_file_path, codec="libx264", audio_codec="aac")
def commercial_remove(input_file, output_file, non_commercial_intervals, verbose=True): '''Remove commercials from audio file input_file: audio file to remove commercials from output_file: output path of new audio file with no commercials audio_data: result from commercial_identify call ''' audio_clip = AudioFileClip(input_file) clips = [] for start, end in non_commercial_intervals: clips.append(audio_clip.subclip(start, end)) final = concatenate_audioclips(clips) final.write_audiofile(output_file, verbose=verbose)
def take(self, options: dict): if options["with_video"]: self._take_video(options["video_url"]) if options["with_audio"]: self._take_audio(options["audio_url"]) if options["with_video"]: video = VideoFileClip(self.__path_to_save_video) audio = AudioFileClip(self.__path_to_save_audio) video.audio = audio.set_duration(video.duration) os.remove(self.__path_to_save_video) os.remove(self.__path_to_save_audio) video.write_videofile(self.__path_to_save_video)
def get_samples(self, data_file): # time = self.dict_files[data_file]['time'] audio_clip = AudioFileClip(data_file) clip = audio_clip.set_fps(16000) # num_samples = int(clip.fps * time) data_frame = np.array(list(clip.subclip(0).iter_frames())) data_frame = data_frame.mean(1) chunk_size = 640 # split audio file to chuncks of 40ms audio = np.pad(data_frame, (0, chunk_size - data_frame.shape[0] % chunk_size), 'constant') audio = np.reshape(audio, (-1, chunk_size)).astype(np.float32) return audio
def add_background_audio(audio_clip): """ :param audio_clip: 최종 영상 오디오 클립 :return: 배경음 삽입된 오디오 클립 """ # Access audio file back_audio = AudioFileClip(settings.BASE_DIR + '/core/back_audios/' + get_selected_music() + '.wav') # New audio file new_audio_clip = CompositeAudioClip([audio_clip.fx(volumex, 7), back_audio.fx(volumex, 1)]) return new_audio_clip
def test_issue_470(): audio_clip = AudioFileClip("media/crunching.mp3") # t_end is out of bounds subclip = audio_clip.subclip(t_start=6, t_end=9) with pytest.raises(IOError): subclip.write_audiofile(os.path.join(TMP_DIR, "issue_470.wav"), write_logfile=True) # but this one should work.. subclip = audio_clip.subclip(t_start=6, t_end=8) subclip.write_audiofile(os.path.join(TMP_DIR, "issue_470.wav"), write_logfile=True)
def segment_audio_file(src_path, dest_dir, segment_duration=DEFAULT_AUDIO_SEGMENT_DURATION_SEC, zeroes_padding=DEFAULT_ZEROES_PADDING): """ For a given audio file at `src_path`, the audio is segmented into chunks of `segment_duration` seconds. Each segment is saved in `dest_dir` with a filename convention using the start and end time in seconds. src_path (str): absolute path to the audio file dest_dir (str): absolute path to the subdirectory segment_duration (int): the number of seconds for each segment zeroes_padding (int): the number of zeroes to pad each segment's filename Yields a generator; for each iteration, the path to a newly-created audio segment is returned, e.g. "./projects/myvideo/audio-segments/00000-00100.wav" """ src_basename, src_ext = splitext(src_path) # e.g. for a file, "myvideo/audio.wav", audio_ext is ".wav" audio = AudioFileClip(src_path) total_seconds = audio.duration x_sec = 0 while x_sec < total_seconds: y_sec = x_sec + segment_duration if y_sec > total_seconds: # when we've reached the end of the total duration # round to the next second y_sec = ceil(total_seconds) # turns out subclip does not like an endpoint bigger # than the clip's duration, so we leave off second argument segment = audio.subclip(x_sec) else: segment = audio.subclip(x_sec, y_sec) segment_basename = "%s-%s%s" % ( str(x_sec).rjust(zeroes_padding, "0"), str(y_sec).rjust(zeroes_padding, "0"), src_ext) segment_full_path = join(dest_dir, segment_basename) segment.write_audiofile(segment_full_path) yield segment_full_path # set x_sec to equal y_sec, so that the next clip start at y_sec x_sec = y_sec
all_square[i].is_triggered = False all_square[i].frame_tick = 0 else: all_square[i].image = all_square[i].image_empty all_square[i].is_triggered = False all_square[i].frame_tick = 0 #print(all_square[0].frame_tick) #initialisation score = 0 song = "myaudio.mp3" # Get the sound (we be an array with values between 0 and 1) audio = AudioFileClip(song) # could be a wav, ogg... 13553253 vlcplayer = vlc.MediaPlayer(song) sound_array = audio.to_soundarray() PlaybackThread = threading.Thread(target=vlcplayer.play) PlaybackThread.daemon = True GAME_WIDTH = 720 GAME_HEIGHT = 720 all_square = [] game_map = [] #initialisation de la sequence de pop à partir du son charger #sound_array a pour valeur -1 à +1 donc on on décale vers 0 à 2 for i in range (0,len(sound_array)):
idx = [i for i in range(len(time_sum)-1) if t<=time_sum[i]][0] # print "======", jpegs[idx/2], "======", idx delta_fade = time_sum[idx]-time_sum[idx-1] fade_to = (t-time_sum[idx-1])/delta_fade # fraction fade_from = 1-fade_to # fraction frame_for_time_t_BGR_frame0 = fade_from * cv2.imread(jpegs[idx/2],cv2.CV_LOAD_IMAGE_COLOR) frame_for_time_t_BGR_frame1 = fade_to * cv2.imread(jpegs[idx/2+1],cv2.CV_LOAD_IMAGE_COLOR) # BLENDED FRAME frame_for_time_t_BGR_frame01 = frame_for_time_t_BGR_frame0 + frame_for_time_t_BGR_frame1 frame_for_time_t_BGR_frame01 = frame_for_time_t_BGR_frame01.astype('uint8') # convert from float to uint8 frame_for_time_t = cv2.cvtColor(frame_for_time_t_BGR_frame01, cv2.COLOR_BGR2RGB) # BGR-RGB COLOR return frame_for_time_t clip = VideoClip(make_frame, duration=time_sum[-1])#.set_audio(audio) # x-second clip if audio_on: audio = AudioFileClip("audioclip.mp3") audio = audio.set_duration(time_sum[-1]) clip = clip.set_audio(audio) clip.write_videofile("my_animation_%sfps_dummy.mp4" %fps, fps=fps) # export as video #clip.write_gif("my_animation.gif", fps=24) # export as GIF
def transcribe_bytes(self, byte_data, clip_length=None, audio_mimetype='', compress=True): ''' a method to transcribe text from audio byte data :param byte_data: byte data in buffer with audio data :param clip_length: [optional] integer with seconds to divide clips into :param compress: [optional] boolean to convert file to audio/ogg :param audio_mimetype: [optional] string with byte data mimetype :return: dictionary with transcribed text segments in 'segments' key ''' title = '%s.transcribe_bytes' % self.__class__.__name__ bytes_arg = "%s(byte_data=b'...')" % title # validate inputs input_fields = { 'clip_length': clip_length, 'audio_mimetype': audio_mimetype } for key, value in input_fields.items(): if value: object_title = '%s(%s=%s)' % (title, key, str(value)) self.fields.validate(value, '.%s' % key, object_title) # validate data mimetype if audio_mimetype: file_extension = '' for key, value in self.fields.schema['audio_extensions'].items(): if value['mimetype'] == audio_mimetype: file_extension = value['extension'] else: if self.magic: magic_details = self.magic.analyze(byte_data=byte_data) file_name = magic_details['name'] if not file_name: file_name = 'audio' file_name += magic_details['extension'] ext_kwargs = { 'file_name': file_name, 'extension_map': self.fields.schema['audio_extensions'], 'method_title': title, 'argument_title': 'byte_data' } file_details = self._validate_extension(**ext_kwargs) audio_mimetype = file_details['mimetype'] file_extension = file_details['extension'] else: raise ValueError('%s argument requires audio_mimetype (or magic) to determine its mimetype.' % bytes_arg) # construct default return transcript_details = { 'error': '', 'segments': [] } # transcribe entire buffer if clip_length is None: try: transcript = self.client.recognize(byte_data, audio_mimetype, continuous=True) if transcript['results']: for result in transcript['results']: transcript_details['segments'].append(result['alternatives'][0]) except Exception as err: transcript_details['error'] = err # save buffer to disk, segment and transcribe in multiple threads else: import os from time import sleep from math import ceil from moviepy.editor import AudioFileClip # save buffer to disk clip_folder = self._create_folder() full_name = 'audio_full.%s' % file_extension full_path = os.path.join(clip_folder, full_name) with open(full_path, 'wb') as f: f.write(byte_data) f.close() # convert file if compress: file_name, file_extension = os.path.splitext(full_path) if file_extension != '.ogg': full_path = self.convert_audio(full_path, 'audio/ogg', True) file_details = { 'name': 'audio_full.ogg', 'extension': '.ogg', 'mimetype': 'audio/ogg' } audio_mimetype = file_details['mimetype'] file_extension = file_details['extension'] # open audio file count = 0 retry_count = 10 while True: try: audio = AudioFileClip(full_path) break except PermissionError: sleep(.05) count += 1 if count > retry_count: raise audio_duration = audio.duration # construct list of files to transcribe file_list = [] if audio_duration < clip_length: file_list.append(full_path) else: # create temporary audio files count = 0 t_start = 0 while t_start < audio_duration: t_end = t_start + clip_length if t_end > audio_duration: t_end = ceil(audio_duration) segment = audio.subclip(t_start) else: segment = audio.subclip(t_start, t_end) clip_name = 'audio%s.%s' % (count, file_extension) clip_path = os.path.join(clip_folder, clip_name) segment.write_audiofile(clip_path, verbose=False) file_list.append(clip_path) count += 1 t_start = t_end # run file transcription method transcript_details = self._transcribe_files(file_list, audio_mimetype) # remove temp files if len(file_list) > 1: from labpack.records.settings import remove_settings for file in file_list: remove_settings(file, remove_dir=True) return transcript_details
def transcribe_url(self, file_url, clip_length=None, compress=True): ''' a method to transcribe the text from an audio url :param file_path: string with url to audio file on web :param clip_length: [optional] integer with seconds to divide clips into :param compress: [optional] boolean to convert file to audio/ogg :return: dictionary with transcribed text segments in 'segments' key ''' title = '%s.transcribe_url' % self.__class__.__name__ # validate inputs input_fields = { 'file_url': file_url, } if clip_length is not None: input_fields['clip_length'] = clip_length for key, value in input_fields.items(): object_title = '%s(%s=%s)' % (title, key, str(value)) self.fields.validate(value, '.%s' % key, object_title) # construct empty file details file_details = { 'name': '', 'mimetype': '', 'extension': '' } # retrieve file name from urllib.parse import urlsplit file_arg = '%s(file_url=%s)' % (title, str(file_url)) url_path = urlsplit(file_url).path path_segments = url_path.split('/') file_details['name'] = path_segments[-1] if not file_details['name']: raise ValueError('%s must have a file name.' % file_arg) # validate file extension ext_kwargs = { 'file_name': file_details['name'], 'extension_map': self.fields.schema['audio_extensions'], 'method_title': title, 'argument_title': 'file_url' } file_details = self._validate_extension(**ext_kwargs) # retrieve file data file_buffer = self._get_data(file_url, file_details['name'], title, 'file_url') if isinstance(file_buffer, dict): raise Exception(str(file_buffer)) # validate file mimetype if self.magic: file_data = file_buffer.getvalue() magic_details = self.magic.analyze(byte_data=file_data) mimetype_text = file_details['mimetype'][6:] if mimetype_text not in magic_details['mimetype']: raise ValueError('%s byte data mimetype %s does not match %s file extension.' % (file_arg, magic_details['mimetype'], file_details['extension'])) # construct default return transcript_details = { 'error': '', 'segments': [] } # transcribe entire buffer if clip_length is None: try: file_data = file_buffer.getvalue() transcript = self.client.recognize(file_data, file_details['mimetype'], continuous=True) if transcript['results']: for result in transcript['results']: transcript_details['segments'].append(result['alternatives'][0]) except Exception as err: transcript_details['error'] = err # save buffer to disk, segment and transcribe in multiple threads else: import os from time import sleep from math import ceil from moviepy.editor import AudioFileClip # save buffer to disk clip_folder = self._create_folder() full_name = 'audio_full.%s' % file_details['extension'] full_path = os.path.join(clip_folder, full_name) with open(full_path, 'wb') as f: f.write(file_buffer.getvalue()) f.close() # convert file if compress: file_name, file_extension = os.path.splitext(full_path) if file_extension != '.ogg': full_path = self.convert_audio(full_path, 'audio/ogg', True) file_details = { 'name': 'audio_full.ogg', 'extension': '.ogg', 'mimetype': 'audio/ogg' } # open audio file count = 0 retry_count = 10 while True: try: audio = AudioFileClip(full_path) break except PermissionError: sleep(.05) count += 1 if count > retry_count: raise audio_duration = audio.duration # construct list of files to transcribe file_list = [] if audio_duration < clip_length: file_list.append(full_path) else: # create temporary audio files count = 0 t_start = 0 while t_start < audio_duration: t_end = t_start + clip_length if t_end > audio_duration: t_end = ceil(audio_duration) segment = audio.subclip(t_start) else: segment = audio.subclip(t_start, t_end) clip_name = 'audio%s.%s' % (count, file_details['extension']) clip_path = os.path.join(clip_folder, clip_name) segment.write_audiofile(clip_path, verbose=False) file_list.append(clip_path) count += 1 t_start = t_end # run file transcription method transcript_details = self._transcribe_files(file_list, file_details['mimetype']) # remove temp files if len(file_list) > 1: from labpack.records.settings import remove_settings for file in file_list: remove_settings(file, remove_dir=True) return transcript_details
def transcribe_file(self, file_path, clip_length=10, compress=True): ''' a method to transcribe the text from an audio file EXAMPLE: https://github.com/dannguyen/watson-word-watcher :param file_path: string with path to audio file on localhost :param clip_length: [optional] integer with seconds to divide clips into :param compress: [optional] boolean to convert file to audio/ogg :return: dictionary with transcribed text segments in 'segments' key ''' title = '%s.transcribe_file' % self.__class__.__name__ # validate inputs input_fields = { 'file_path': file_path, 'clip_length': clip_length } for key, value in input_fields.items(): object_title = '%s(%s=%s)' % (title, key, str(value)) self.fields.validate(value, '.%s' % key, object_title) # run conversion import os if compress: file_name, file_extension = os.path.splitext(file_path) if file_extension != '.ogg': file_path = self.convert_audio(file_path, 'audio/ogg', True) # construct empty file details file_details = { 'name': '', 'mimetype': '', 'extension': '' } # retrieve file name file_arg = '%s(file_path=%s)' % (title, str(file_path)) split_file = os.path.split(file_path) file_details['name'] = split_file[0] if len(split_file) > 1: file_details['name'] = split_file[1] if not file_details['name']: raise ValueError('%s must have a file name.' % file_arg) # validate file extension ext_kwargs = { 'file_name': file_details['name'], 'extension_map': self.fields.schema['audio_extensions'], 'method_title': title, 'argument_title': file_path } regex_details = self._validate_extension(**ext_kwargs) file_details.update(**regex_details) # retrieve byte data if not os.path.exists(file_path): raise ValueError('%s is not a valid file path.' % file_arg) # validate file mimetype if self.magic: magic_details = self.magic.analyze(file_path) mimetype_text = file_details['mimetype'][6:] if mimetype_text not in magic_details['mimetype']: raise ValueError('%s byte data mimetype %s does not match %s file extension.' % (file_arg, magic_details['mimetype'], file_details['extension'])) # import dependencies from math import ceil from moviepy.editor import AudioFileClip # open audio file audio = AudioFileClip(file_path) audio_duration = audio.duration # construct list of files to transcribe file_list = [] if audio_duration < clip_length: file_list.append(file_path) else: # create temporary audio files clip_folder = self._create_folder() count = 0 t_start = 0 while t_start < audio_duration: t_end = t_start + clip_length if t_end > audio_duration: t_end = ceil(audio_duration) segment = audio.subclip(t_start) else: segment = audio.subclip(t_start, t_end) clip_name = 'audio%s.%s' % (count, file_details['extension']) clip_path = os.path.join(clip_folder, clip_name) segment.write_audiofile(clip_path, verbose=False) file_list.append(clip_path) count += 1 t_start = t_end # run file transcription method transcription_result = self._transcribe_files(file_list, file_details['mimetype']) # remove temp files if len(file_list) > 1: from labpack.records.settings import remove_settings for file in file_list: remove_settings(file, remove_dir=True) return transcription_result