def splitToMonoFlac(targetName, srcFile): startTime = getCurrTime() targetPathStereo = audioPath + targetName.split('.')[0] + "_notmono.flac" targetPathMono = audioPath + targetName.split('.')[0] + ".flac" if not os.path.exists(targetPathMono): length = float(mediainfo(srcFile)['duration']) subprocess.call(['ffmpeg', '-i', srcFile, targetPathStereo]) subprocess.call([ 'ffmpeg', '-i', targetPathStereo, '-map_channel', '0.0.0', targetPathMono ]) newFileSize = os.path.getsize(targetPathMono.split('.')[0] + ".flac") sampleRate = int( mediainfo(targetPathMono.split('.')[0] + ".flac")['sample_rate']) os.remove(targetPathStereo) convDuration = getTimeElapsed(startTime) print("Duration: " + convDuration) else: length = float(mediainfo(srcFile)['duration']) sampleRate = int( mediainfo(targetPathMono.split('.')[0] + ".flac")['sample_rate']) newFileSize = os.path.getsize(targetPathMono.split('.')[0] + ".flac") convDuration = getTimeElapsed(startTime) return [ targetName.split('.')[0] + ".flac", convDuration, sampleRate, length, newFileSize ]
def convertFile(srcFilePath, targetFolder, targetBitRate): isSupported = True dotRIndex = srcFilePath.rfind(".") fileExt=srcFilePath[dotRIndex + 1:] print("\n handling file " + srcFilePath ) if not fileExt in supported_files: addExtToUnsupStat(fileExt) print("file format " + fileExt + " not supported") if cfg_copyUnspported: isSupported=False print("fopying file to target unprocessed") else: print("dropping file") return None prefix=srcFilePath.replace(" ", "_")[0:dotRIndex] #read bit rate and size from file meta data if 'size' in mediainfo(srcFilePath): fSize = mediainfo(srcFilePath)['size'] else: fSize = -1 if 'bit_rate' in mediainfo(srcFilePath): fBRrate = mediainfo(srcFilePath)['bit_rate'] else: fBRrate = -1 print('---> prefix:%s, fileExt:%s, bit rate:%s, size:%s' % (prefix,fileExt,fBRrate,fSize)) sysCommand = None print('file size MB:%1.2f M TH SIZE %1.2f M -- -file bRate:%1.2f TH BRATE:%1.2f' % ((float(fSize))/M , min_size_MB, (float(fBRrate))/K, min_BitRate/K)) #file not supported or bellow Thresholds -> copy as is to target underBRate = float(fBRrate) < min_BitRate and float(fBRrate) > 0 underSize = (float(fSize))/M < min_size_MB and float(fSize) > 0 if (not isSupported ) or underBRate or underSize: print('file is not supported or size or bit rate is bellow threshold, copying to destination folder as is') sysCommand = ('copy \"%s\" \"%s\" ' % (reverseSlashDirection(srcFilePath), reverseSlashDirection(targetFolder))) if isSupported: if underBRate: statUnderThrsh['bitRate'] += 1 filesListsUThrsh['bitRate'].append(srcFilePath) if underSize: statUnderThrsh['size'] += 1 filesListsUThrsh['size'].append(srcFilePath) #compress else: #ffmpeg -i wma_src.wma -b 12k output.mp3 #trim the file name from tha path fileName = srcFilePath[srcFilePath.rfind("\\"):] print("claculated file name %s" % (fileName)) sysCommand = ('ffmpeg -i "%s" -loglevel %s -b:a %sk "%s"' % (reverseSlashDirection(srcFilePath), "warning", targetBitRate, reverseSlashDirection(targetFolder + fileName + ".mp3"))) print ("sysCommand=" + sysCommand) os.system(sysCommand )
def volume_trial(file): # The threshhold of volume (decibels) threshhold = -45 # The threshhold of total time over volume threshhold (seconds) length_seconds = 5 # Get audio from file and data about audio audio = AudioSegment.from_file(file) data = mediainfo(file) # The number of samples in the audio per second of audio samples_per_second = int(len(audio) / float(data["duration"])) # The time threshhold in samples length_samples = length_seconds * samples_per_second # Find all sets of nonsilent samples nonsilences = silence.detect_nonsilent(audio, 1, silence_thresh=threshhold) # calculate total amount of nonsilences total = 0 for i in nonsilences: total += i[1] - i[0] print(total / samples_per_second) if total >= length_samples: return True else: return False
def process(self): print("[Playlist] Processing started for: " + self.output_file_name) try: AudioSegment.converter = which('ffmpeg') uploaded_file = AudioSegment.from_file(self.uploaded_file_path, self.uploaded_file_path[-3:]) info = mediainfo(self.uploaded_file_path) self.allowed_durations = [ float(info['duration']) / 60 ] #Music of all durations is permitted checker = FileChecker(file_path=self.uploaded_file_path, durations=self.allowed_durations, min_sample_rate=self.min_sample_rate, min_bitrate=self.min_bitrate, recommended_bitrate=self.recommended_bitrate, info=info, do_normalization=True, do_check_duration=False, accept_clipping=True) print("\t* Checking file...") parameters = checker.run_checks() self.build_tags(info) print("\t* Exporting...") uploaded_file.export(self.output_file_path, bitrate=self.recommended_bitrate + "k", tags=self.tags, parameters=parameters) print("\t* Uploading...") self.remote_service.upload_track_to_playlist(self.output_file_name, self.output_file_path) finally: print("\t* Cleaning up...") self.cleanup()
def split_file(source_file, output_directory="output"): # This is the audio input which we will be splitting sound = AudioSegment.from_file(source_file) # This is metadata about the file we will be splitting data = mediainfo(source_file) # This is a conversaion factor used to cut apart the audio into one minute # chunks. Often sample_rate given can be slightly off, so I decided to use # a more base approach, dividing the length (samples) by the duration (seconds) # and scaling up by a factor of 60 (seconds per minute) samples_per_minute = int(60 * len(sound) / float(data["duration"])) # Get log_base_10 of number of minutes in the file to figure out how long # the filename ids must be. If it is between 10 and 99 then all outputs # must be formatted to length 2, i.e. 00, 01, 02... 10, 11, 12... 97, 98, 99 name_size = ceil(log10(float(data["duration"]) / 60)) # Number of minutes in the file, rounded upwards minutes = int(ceil(float(len(sound)) / samples_per_minute)) # Loop through every minute of the audio and output it into the folder # output as "output{n}.wav" where {n} = the minute id (starts at minute 0) output_format = "%s/output%0" + str(name_size) + "d.wav" for i in range(minutes): print("\t" + output_format % (output_directory, i)) split_sound = sound[i * samples_per_minute:(i + 1) * samples_per_minute] split_sound.export(output_format % (output_directory, i), format="wav", bitrate=data["bit_rate"]) return minutes
def wav_split(file): main_wav_path = file path = os.path.dirname(file) + '/' sound_len = int(float(mediainfo(main_wav_path)['duration'])) sound = AudioSegment.from_wav(main_wav_path) part_file_list = list() n = 1 if sound_len > 60: n = sound_len // 60 while n * 60 < sound_len: n = n + 1 with shelve.open('DB/lines.db') as db: for i in range(n): start_time = i * 60 * 1000 + 1 end_time = (i + 1) * 60 * 1000 if end_time > sound_len * 1000: end_time = sound_len * 1000 word = sound[start_time:end_time] part_file_name = '{}part_sound_{}.wav'.format(path, i) word.export(part_file_name, format='wav') part_file_list.append(part_file_name) record = {"start": start_time, "end": end_time, "lines": []} # 对不在数据库中的条目, 存储之 if not db[part_file_name]: db[part_file_name] = record return part_file_list
def check_sound_files_can_be_played(self): """ Iterates through every sound file and attempts to create a player that uses this file. Logs any error and raises a `TypeError` if a file has an unsupported format. """ logger.info("Checking that sounds are playable...") for group, sound, sound_file in utils.sound_tuple_generator( self.groups): root_directory = utils.get_sound_root_directory( group, sound, default_dir=self.default_dir) sound_file_path = os.path.join(root_directory, sound_file.file) try: pygame.mixer.Sound(sound_file_path) except pygame.error: logger.error( f"File {sound_file_path} cannot be played. Its format is unsupported." ) file_info = mediainfo(sound_file_path) logger.error( f"Found format: codec={file_info['codec_long_name']}, " f"sample_rate={file_info['sample_rate']}, channels={file_info['channels']}" ) logger.error( "Note that only signed 16-bit sample formats are supported." ) logger.error( "You can convert your file with the script in 'scripts/convert_file.py'." ) raise TypeError( f"File {sound_file_path} cannot be played. Its format is unsupported." ) logger.info("Success! All sounds can be played.")
def audioPreTreat(self, do_cut=1): import os # How many parts the file need to be devided into? self.__file_size = os.path.getsize(self.__file_name) if do_cut == 0: return 1 self.__slice_num = int(self.__file_size / (size10m)) + 1 from pydub import AudioSegment from pydub.utils import mediainfo self.__bitrate = mediainfo(self.__file_name)['bit_rate'] audio_obj = AudioSegment.from_file(self.__file_name, self.__audio_subfix) # sec to milisec audio_time_length = audio_obj.duration_seconds * 1000 # audio_clip = [] for clip_index in range(0, self.__slice_num): write_offset( int(audio_time_length * ((clip_index) / self.__slice_num)), self.__file_name) write_offset( int(audio_time_length * ((clip_index + 1) / self.__slice_num)), self.__file_name) print('---- ----') current_clip = audio_obj[int(audio_time_length * ( (clip_index) / self.__slice_num)):int(audio_time_length * ( (clip_index + 1) / self.__slice_num))] self.checkTempdir("temp_audioclip") self.checkTempdir("export") current_clip.export( f"./temp_audioclip/{self.__audio_prefix}.{str(clip_index)}.{self.__export_subfix}", format=self.__export_subfix, bitrate=self.__bitrate) return 0
def getArtist(self, song): """Returns the artist of song.""" if song[-4:] == '.mp3': obj = EasyID3(song) if 'artist' in obj.keys(): return obj['artist'] elif 'TAG' in mediainfo(song).keys(): obj = mediainfo(song)['TAG'] if 'artist' in obj.keys(): return [obj['artist']] elif 'ARTIST' in obj.keys(): return [obj['ARTIST']] else: return ['Unknown'] else: return ['Unknown']
def raw_data_processing(data_path): data = list() label = list() file_dir = os.listdir(path=data_path) for i in range(len(file_dir)): file_list = os.listdir(data_path + "\\" + file_dir[i]) for j in range(len(file_list)): file_name = data_path + "\\" + file_dir[i] + "\\" + file_list[j] print("file_name: ", file_name) audio_file, sr = librosa.load( path=file_name, sr=int(mediainfo(file_name)['sample_rate'])) try: audio_plp = plp(input_sig=audio_file, fs=sr) data.append(audio_plp[0]) label.append(i) except: print("Error occured at ", file_name) continue data = np.asarray(data) label = np.asarray(label) return data, label
def check_sound(audio_path): """ всевозможные проверки записи :param audio_path: :return: message: 'OK', 'Not Found', 'Empty File', 'not sound format', 'Invalid param', 'Sound Error', 'too Short' + после этой функции нужно проверять на мин. длительность """ # ------ if not os.path.exists(audio_path): return 'Not Found' # ------ file_name = os.path.split(audio_path)[1] # ('test_sounds', 'test.mp3') file_name, file_ext = os.path.splitext(file_name) # ('test' , '.mp3') if not file_ext in ['.wav', '.mp3', '.mpeg', '.flac']: return "not Sound Format: '{}'".format(file_ext) # ------ if os.path.getsize(audio_path) == 0: return 'Empty File' # ------ media_info = mediainfo(audio_path) # ------ for key, value in params.items(): if not media_info[key] == value: return 'Invalid {}'.format(key) # ------ return 'OK'
def cut_and_eq(song_name): """Cut out silence from the beginning/end of a song and amplify the song if necessary. Returns None on success and the name of the song on failure.""" print("[{}] STATUS: Loading...".format(song_name)) sound_file = AudioSegment.from_mp3(song_name) print("[{}] STATUS: Loaded, now processing...".format(song_name)) sound_file = match_target_amplitude( sound_file, TARGET_VOLUME) # Amplify beforehand to prevent over-zealous cutting chunks = split_on_silence(sound_file, SILENCE_CUTOFF, THRESHOLD, keep_silence=ACCEPTABLE_SILENCE) if len(chunks) > 1: print("[{}] ERROR: Too many chunks ({}) cannot export".format( song_name, len(chunks))) return song_name else: output = AudioSegment.empty() for chunk in chunks: output += chunk new_name = song_name.split(".")[0] print("[{}] STATUS: Processed, now exporting...".format(song_name)) metadata = mediainfo(song_name).get('TAG', {}) output.export(OUTPUT_NAME_FORMAT.format(new_name), format=OUTPUT_FORMAT, tags=metadata) print("[{}] STATUS: Exported to {} - cleaned.{}".format( song_name, new_name, OUTPUT_FORMAT)) return None
def mp3_to_wav(file): print(mediainfo(file)) sound = AudioSegment.from_ogg(file) #flac_file = "learn/static/learn/audio/user.flac" wav_file = "learn/static/learn/audio/dest.wav" print(sound.frame_rate) print(sound.channels) sound = sound.set_channels(1) sound = sound.set_frame_rate(16000) sound.export(wav_file, format="wav") print(sound.frame_rate) print(sound.channels) #sound.export(flac_file, format="flac") #sound2 = AudioSegment.from_wav(wav_file) #sound2 = sound2.set_frame_rate(16000) #sound2 = sound2.set_channels(1) #print(sound2.frame_rate) #print(sound2.channels) #sound2.export(wav_file, format="wav") #with wave.open(wav_file, "rb") as wave_file: # frame_rate = wave_file.getframerate() # channels = wave_file.getnchannels() return wav_file
def getTitle(self, song): """Returns the title of song.""" if song[-4:] == '.mp3': obj = EasyID3(song) if 'title' in obj.keys(): return obj['title'] elif 'TAG' in mediainfo(song).keys(): obj = mediainfo(song)['TAG'] if 'title' in obj.keys(): return [obj['title']] elif 'TITLE' in obj.keys(): return [obj['TITLE']] else: return ['Unknown'] else: return ['Unknown']
def extract_media_info(video_filepath: str): complete_media_info = mediainfo(video_filepath) channel_count = complete_media_info['channels'] bit_rate = complete_media_info['bit_rate'] sample_rate = complete_media_info['sample_rate'] return channel_count, bit_rate, sample_rate
def getAlbum(self, song): """Returns the album of song.""" if song[-4:] == '.mp3': obj = EasyID3(song) if 'album' in obj.keys(): return obj['album'] elif 'TAG' in mediainfo(song).keys(): obj = mediainfo(song)['TAG'] if 'album' in obj.keys(): return [obj['album']] elif 'ALBUM' in obj.keys(): return [obj['ALBUM']] else: return ['Unknown'] else: return ['Unknown']
def splice_audio(file_path, start, end): """ Splice audio to specified selection """ audio = AudioSegment.from_mp3(file_path) # Pull thumbnail tags = ID3(file_path) thumbnail = tags.get("APIC:").data # Pull any other tags from og audio file tags = mediainfo(file_path).get('TAG', {}) # Get start and and end paramters # to pull the audio splice of interest start = timestamp_to_milliseconds(start) end = timestamp_to_milliseconds(end) spliced = audio[start:end] spliced.export( file_path, format="mp3", tags=tags ) audiofile = eyed3.load(file_path) audiofile.tag.images.set(3, thumbnail, 'image/jpeg') audiofile.tag.save()
def get_boost_audio(name, bass, speedup): track = AudioSegment.from_file(name) original_bitrate = mediainfo(name)['bit_rate'] freq = bass_line_freq(track.get_array_of_samples()) if (freq > 0): if (freq < 50): filtered_low = track.low_pass_filter(freq) filtered_high = track.high_pass_filter(freq) boosted = filtered_high.overlay(filtered_low + bass) if (0.5 <= speedup <= 2): boosted = speed(boosted, speedup) else: filtered_low = track.low_pass_filter(freq) filtered_low = filtered_low + bass filtered_high = track.high_pass_filter(freq) for i in range(0, 2): filtered_low = filtered_low.low_pass_filter(freq) if not (i > 1): filtered_high = filtered_high.high_pass_filter(freq) boosted = filtered_high.overlay(filtered_low) if (0.5 <= speedup <= 2): boosted = speed(boosted, speedup) return boosted.export(name.replace('.mp3', '') + ' [dolBitNormalno_bot].mp3', format="mp3", bitrate=original_bitrate) else: return 0
def download_file(self, mp3file): """Download file""" if os.path.isfile(mp3file): t_start = time.clock() t_elapsed = time.clock() - t_start print("* Thread: {} Download {} in {} seconds.".format( self.name, mp3file, str(t_elapsed))) format = mp3file.split('.')[0] print('counter', format) info = mediainfo(mp3file) seconds = float(info['duration']) result = seconds / self.images_count plus_one = result command = "ffmpeg|-y|-r|1/{}|-start_number|1|-i|{}photo-%03d.jpg|-i|{}|-r|18|-pix_fmt|yuv420p|-c:a|aac|-s|320x240|{}.mp4".format( plus_one, self.image_path, mp3file, format) print(command) completed = subprocess.run(command.split('|')) print('returncode:', completed) print(completed.returncode) if completed.returncode == 0: print('Moving file {} .....'.format(mp3file)) shutil.move(mp3file, moved_mp3) else: print('No file found', mp3file)
def get_file_info(file_path): """ 统计文件信息 :param file_path: 文件路径 :return: """ kind = filetype.guess(file_path) if kind is None: result = {"ext": os.path.splitext(file_path)[-1].replace(".", "")} else: result = {"mimetype": kind.mime, "ext": kind.extension} media_info = mediainfo(file_path) if is_audio(result.get("ext")): audio_info = get_file_info_audio(file_path) result = {**audio_info, **result, "type": FileType.audio} result["framerate"] = result.get("framerate") or media_info.get( "sample_rate") or None result["framerate"] = int( result["framerate"] ) if result["framerate"] else result["framerate"] return {**media_info, **result}
def convert_incompatible_wav_files(self): """ Iterates through every sound file and checks its format. If the file is a `.wav` file and its codec is not supported, automatically convert the file into a supported codec. The converted file will be stored in the cache and the sound file will be changed to point to it. """ logger.info("Checking that .wav files have compatible formats...") for group, sound, sound_file in utils.sound_tuple_generator( self.groups): root_directory = utils.get_sound_root_directory( group, sound, default_dir=self.default_dir) sound_file_path = os.path.join(root_directory, sound_file.file) file_info = mediainfo(sound_file_path) if file_info["format_name"] == "wav" and file_info[ "sample_fmt"] != "s16": file_hash = cache.get_file_hash(sound_file_path) path_in_cache = os.path.join(cache.CONVERSION_CACHE_DIR, file_hash) if not cache.exists_converted_file(f"{file_hash}.wav"): logger.warning( f"Found incompatible wav file {sound_file_path}.") logger.warning("Attempting to convert it...") convert_file(sound_file_path, "wav", out=path_in_cache) logger.warning("Success! Conversion done.") sound_file.file = f"{path_in_cache}.wav" logger.info("Success! All .wav files should have compatible formats.")
def MangleLibrary(src, dst, audio, splice=None): if os.path.splitext(audio)[1] != ".mp3": raise "Prank audio is not an mp3" prank = AudioSegment.from_mp3(audio) # Walk src for root, dirs, files in os.walk(src): # Loop through files in this dir for fn in files: # If file is an mp3 if os.path.splitext(fn)[1] == ".mp3": # Import song fullsong = AudioSegment.from_mp3(root+"/"+fn) # Pick random location between 10s and end of song start = random.randint(15,60) print("Spliced {} after {} seconds".format(root+"/"+fn,start)) # Splice in prank song if splice != None: r = random.randint(0,len(splice)-1) final = fullsong[:start*1000] + prank[splice[r][0]:splice[r][1]] + fullsong[start*1000:] # final = fullsong[:start*1000] + prank + fullsong[start*1000:] else: final = fullsong[:start*1000] + prank # Recreate directory structrue in dst if not os.path.exists(dst+"/"+root): os.makedirs(dst+"/"+root) # Export song with tags final.export(dst+"/"+root+"/"+fn, format="mp3", tags=mediainfo(root+"/"+fn).get('TAG', {}))
def get_video_info(video_filepath): """ this function returns number of channels, bit rate, and sample rate of the video""" video_data = mediainfo(video_filepath) channels = video_data["channels"] bit_rate = video_data["bit_rate"] sample_rate = video_data["sample_rate"] return channels, bit_rate, sample_rate
def find_bitrate(self, file_input): # Divide bitrate by 1k, round up, # and return back to 1k divisible for round number. target_bitrate = math.ceil( int(mediainfo(file_input)['bit_rate']) / 1000) * 1000 logging.info(f"Source bitrate: {target_bitrate}") return target_bitrate
def normalize_single_file_volume(self, track): # adjust and normalize audio within a single mp3 file s = AudioSegment.from_mp3(self.playlist[track]) normalized_s = effects.normalize(s) normalized_s.export(str(self.playlist[track]), format="mp3", tags=mediainfo(str(self.playlist[track])).get( 'TAG', {}))
def get_sample_rate(self): sr = 44100 try: audio_info = mediainfo(self.file_path) sr = int(audio_info['sample_rate']) except Exception as e: print(e) return sr
def play_mp3(file_path, loops=0): # Play a wave file try: freq = mediainfo(file_path)['sample_rate'] except: print('Open {} failed'.format(file_path)) return False return play_sound(file_path, int(freq), loops)
def test_exporting_to_ogg_uses_default_codec_when_codec_param_is_none(self): with NamedTemporaryFile('w+b', suffix='.ogg') as tmp_ogg_file: AudioSegment.from_file(self.mp4_file_path).export(tmp_ogg_file, format="ogg") info = mediainfo(filepath=tmp_ogg_file.name) self.assertEqual(info["codec_name"], "vorbis") self.assertEqual(info["format_name"], "ogg")
def process(self): print("Processing started for: " + self.normalized_program_name) try: AudioSegment.converter = which('ffmpeg') uploaded_file = AudioSegment.from_file( self.uploaded_file_path, self.uploaded_file_path[-3:]) info = mediainfo(self.uploaded_file_path) checker = FileChecker(file_path=self.uploaded_file_path, durations=self.allowed_durations, min_sample_rate=self.min_sample_rate, min_bitrate=self.min_bitrate, recommended_bitrate=self.recommended_bitrate, info=info, do_normalization=not self.already_normalized, do_check_duration=self.adjust_duration) print("\t* Checking file...") parameters = checker.run_checks() self.build_tags(info) print("\t* Exporting...") uploaded_file.export(self.output_file_path, bitrate=self.recommended_bitrate + "k", tags=self.tags, parameters=parameters) print("\t* Uploading...") self.remote_service.upload_program_to_archive( self.normalized_program_name, self.output_file_path) if self.emission_date == datetime.now().strftime("%Y%m%d"): self.remote_service.upload_program_to_emission( self.output_file_path) print("\t* Sending email...") self.email_service.send_upload_accepted( checker=checker, program_name=self.normalized_program_name, emission_date=self.emission_date) except IrrecoverableProblemsException: print("\t\t- File has bad problems, sending email...") self.email_service.send_upload_rejected( checker=checker, program_name=self.normalized_program_name, emission_date=self.emission_date) except Exception as e: print("- Something unexpected happened, sending email...") print(e) print_tb(e.__traceback__) self.email_service.send_upload_failed( program_name=self.normalized_program_name, emission_date=self.emission_date) finally: print("\t* Cleaning up...") self.cleanup()
def check_sample_rate(file, rate): info = mediainfo(file) print("Checking " + str(rate) + " vs " + str(info.get("sample_rate"))) if str(info.get("sample_rate")) == str(rate): print(str(info.get("sample_rate")) + " == " + str(rate)) return True else: print(str(info.get("sample_rate")) + " != " + str(rate)) return False
def get_numericdata(directory): audio = AudioSegment.from_mp3(directory) audio_bytestring = audio._data audio_signal = np.fromstring(audio_bytestring, dtype=np.int32()).astype(np.int16()) info = mediainfo(directory) sample_rate = int(info['sample_rate']) channels = int(info['channels']) return audio_signal, sample_rate, channels
def wav_cutter(audio_path, output_dir): file_path_quat_m = int(float(mediainfo(audio_path)['duration']) / 15) y, sr = librosa.load(path=audio_path, sr=int(mediainfo(audio_path)['sample_rate'])) cut = len(y) / file_path_quat_m eof = len(y) t1, t2 = 0, int(cut) index = 0 y_original = y while t2 <= eof: y = y_original[t1:t2] librosa.output.write_wav((output_dir + str(index) + ".wav"), y, sr) index += 1 t1 = t2 t2 = t2 + int(cut)
def _process_file(src, dst, cover_path, audio_type_src, bitrate, audio_type_dst='mp3'): log.trace('Reading source file.') src_file = AudioSegment.from_file(src, format=audio_type_src) log.trace('Reading tags from file') meta = mediainfo(src).get('TAG', {}) log.trace('Creating destination file.') src_file.export(dst, format=audio_type_dst, bitrate=bitrate, tags=meta) log.trace('Reading cover file.') # TODO: Check if the cover parameter of export also works. if cover_path is None: log.debug('No cover to add.') # No cover, therefore we're finished with this file return False # Prepare the cover with open(cover_path, 'rb') as img: cover = img.read() mime = 'image/jpeg' if cover_path.endswith('.png'): mime = 'image/png' log.trace('Reading destination file.') if audio_type_dst == 'mp3': dst_file = MP3(dst, ID3=ID3) try: dst_file.add_tags() except: # Tag already exists pass else: log.warn('Only .mp3 are supported as output format.') return False log.trace('Adding cover to destination file.') dst_file.tags.add( APIC( encoding=3, # utf-8 mime=mime, # image/png or image/jpeg type=3, # front cover data=cover # the image )) log.trace('Saving info in destination file.') dst_file.save() return True
def generate_fm_signal_from_sound(sound_fname, T_sim, offset): """ Generated FM signal from sound file The offset parameter is a float number between 0 and 1. It specifyies the start position of the time window(T_sim) that will be used from the sound file for the FM signal preparation. Parameters: ---------- :param: sound_fname: Name of sound sound file to be imported :param: T_sim: Duration of the reqested simulated signal [s] :param: offset: Offset position of the processed window :type: sound_fname: string :type: T_sim : float :type: offset: float (0..1) Retrun values: -------------- :return: s : Generated FM signal :return: fs: Sampling frequency of the modulated signal :rtype: s: One dimensional numpy array :rtype: fs: float """ # Internal processing parameters resample_ratio = 5 fir_tap_size = 10 # Resample filter tap size fd = 75 # frequency deviation [kHz] -> Same as in FM broadcast # Import sound file sound = AudioSegment.from_mp3(sound_fname) sound_samples = np.array(sound.get_array_of_samples()) info = mediainfo(sound_fname) fs = int(info['sample_rate']) # Resample offset = int(offset * np.size(sound_samples)) N_raw = math.ceil( T_sim / (1 / fs)) # Number of samples needed from the modulating signal sound_samples = sound_samples[offset:offset + N_raw] # Cut out useful portion s_mod = resample.resample_n_filt(resample_ratio, 1, fir_tap_size, sound_samples) fs = resample_ratio * fs # Generate FM signal s_mod = s_mod / np.max(np.abs(s_mod)) # Normalize k_fm = fd * 10**3 / np.max(s_mod) s = np.sin(2 * np.pi * k_fm / fs * np.cumsum(s_mod)) # Modulate return s, fs
def test_export_mp3_with_tags(self): tags = {'artist': 'Mozart', 'title': 'The Magic Flute'} with NamedTemporaryFile('w+b', suffix='.mp3') as tmp_mp3_file: AudioSegment.from_file(self.mp4_file_path).export(tmp_mp3_file, format="mp3", tags=tags) info = mediainfo(filepath=tmp_mp3_file.name) info_tags = info["TAG"] self.assertEqual(info_tags["artist"], "Mozart") self.assertEqual(info_tags["title"], "The Magic Flute")
def test_exporting_to_ogg_uses_default_codec_when_codec_param_is_none(self): delete = sys.platform != 'win32' with NamedTemporaryFile('w+b', suffix='.ogg', delete=delete) as tmp_ogg_file: AudioSegment.from_file(self.mp4_file_path).export(tmp_ogg_file, format="ogg") if sys.platform == 'win32': tmp_ogg_file.close() info = mediainfo(filepath=tmp_ogg_file.name) if sys.platform == 'win32': os.remove(tmp_ogg_file.name) self.assertEqual(info["codec_name"], "vorbis") self.assertEqual(info["format_name"], "ogg")
def cutMp3(inFile,outDir): log('Begin work with file '+ inFile) sound = AudioSegment.from_mp3(inFile) original_bitrate = mediainfo(inFile)['bit_rate'] soundLen = len(sound) minTime = 6*1000 maxTime = 15*1000 minSoundLen = 10*1000 if soundLen < minSoundLen: log(" sound is less then {0} msec".format(minSoundLen)) return 0 if soundLen < maxTime: log(" sound is less then {0} msec".format(maxTime)) return 0 #cut = findSilence(sound[fr:to]) #TempOutputFileName = trimExt(inFile)+'(1).mp3' #sound[0:cut].export(TempOutputFileName, format="mp3", bitrate=original_bitrate) timeLeft = soundLen count = 1 begin = 0 while timeLeft >= minSoundLen: fr = begin+minTime if fr>soundLen: log(" Error: out of bounds, from={0} msec".format(fr)) break; to = begin+maxTime if to>soundLen: to = soundLen silence = findSilence(sound[fr:to]) if not silence: break cut = begin + silence soundToWrite = sound[begin:cut] #soundLeft = sound[cut+1:soundLen] TempOutputFileName = '{0}({1})_{2}_{3}.mp3'.format(getFileName(inFile),count,begin,cut) soundToWrite.export(os.path.join(outDir,TempOutputFileName), format="mp3", bitrate=original_bitrate) log(' write file "{0}"'.format(TempOutputFileName)) begin = cut+1 count+=1 timeLeft = soundLen-(cut+1) #len(soundLeft) TempOutputFileName = '{0}({1})_{2}_{3}.mp3'.format(getFileName(inFile),count,cut+1,soundLen) sound[cut+1:soundLen].export(os.path.join(outDir,TempOutputFileName), format="mp3", bitrate=original_bitrate) log(' write file "{0}"'.format(TempOutputFileName)) return 1
def test_export_mp3_with_tags(self): tags = {'artist': 'Mozart', 'title': 'The Magic Flute'} delete = sys.platform != 'win32' with NamedTemporaryFile('w+b', suffix='.mp3', delete=delete) as tmp_mp3_file: AudioSegment.from_file(self.mp4_file_path).export(tmp_mp3_file, format="mp3", tags=tags) if sys.platform == 'win32': tmp_mp3_file.close() info = mediainfo(filepath=tmp_mp3_file.name) info_tags = info["TAG"] self.assertEqual(info_tags["artist"], "Mozart") self.assertEqual(info_tags["title"], "The Magic Flute") if sys.platform == 'win32': os.remove(tmp_mp3_file.name)
def test_joined(): ''' Должно получиться (и получается) : <Word id="6684" stime="4731.52" dur="0.19" conf="0.990"> the </Word> <Word id="6685" stime="4731.77" dur="0.11" conf="0.990"> the </Word> <Word id="6686" stime="4731.88" dur="0.31" conf="0.990"> lady </Word> <Word id="6687" stime="4732.22" dur="0.13" conf="0.990"> that </Word> <Word id="6688" stime="4732.38" dur="0.11" conf="0.990"> i </Word> <Word id="6689" stime="4732.56" dur="0.22" conf="0.990"> spoke </Word> <Word id="6690" stime="4732.84" dur="0.14" conf="0.990"> to </Word> <Word id="6691" stime="4733.02" dur="0.17" conf="0.990"> was </Word> <Word id="6692" stime="4733.23" dur="0.23" conf="0.990"> really </Word> <Word id="6693" stime="4733.46" dur="0.62" conf="0.990"> helpful </Word> ''' inFile = r'Joined.mp3' outFile=r'Output_audio\part_from_joined.mp3' sound = AudioSegment.from_mp3(inFile) original_bitrate = mediainfo(inFile)['bit_rate'] segment = sound[4731.52*1000:(4733.46+0.62)*1000] segment.export(outFile, format="mp3", bitrate=original_bitrate)
def cut(self,inFile): self.result[:]=[] self.log('Begin work with file '+ inFile) inFile = os.path.join(self.audioInDir,inFile) sound = AudioSegment.from_mp3(inFile) original_bitrate = mediainfo(inFile)['bit_rate'] soundLen = len(sound) if soundLen < self.minSoundLen: self.log(" sound is less then {0} msec".format(self.minSoundLen)) return [] if soundLen < self.maxTime: self.log(" sound is less then {0} msec".format(self.maxTime)) return [] timeLeft = soundLen count = 1 begin = 0 while timeLeft >= self.minSoundLen: fr = begin+self.minTime if fr>soundLen: self.log(" Error: out of bounds, from={0} msec".format(fr)) break; to = begin+self.maxTime if to>soundLen: to = soundLen silence = self.findSilence(sound[fr:to]) if not silence: break cut = begin + silence soundToWrite = sound[begin:cut] self.writeFilePart(inFile, count, soundToWrite, original_bitrate, begin, cut) begin = cut+1 count+=1 timeLeft = soundLen-(cut+1) self.writeFilePart(inFile, count, sound[cut+1:soundLen], original_bitrate, cut+1,soundLen) self.log(' cut OK') return self.result
def test_small(): ''' Должно получиться (но не выходит) <SpeechSegment ch="1" stime="26.20" etime="45.37" lang="eng" tconf="0.91"> <Word id="60" stime="26.50" dur="0.13" conf="0.990"> the </Word> <Word id="61" stime="26.63" dur="0.24" conf="0.990"> person </Word> <Word id="62" stime="26.93" dur="0.12" conf="0.990"> who </Word> <Word id="63" stime="27.05" dur="0.22" conf="0.990"> dealt </Word> <Word id="64" stime="27.27" dur="0.14" conf="0.990"> with </Word> <Word id="65" stime="27.44" dur="0.25" conf="0.990"> me </Word> <Word id="66" stime="27.82" dur="0.26" conf="0.990"> was </Word> <Word id="67" stime="28.13" dur="0.21" conf="0.990"> really </Word> <Word id="68" stime="28.50" dur="0.65" conf="0.990"> professional </Word> <Word id="69" stime="29.62" dur="0.18" conf="0.990"> he </Word> <Word id="70" stime="29.80" dur="0.22" conf="0.990"> helped </Word> ''' inFile = r'Input_audio\0f57e594-09db-4642-8d9b-89cdf5e7424f.mp3' #2-й файл в списке outFile=r'Output_audio\0f57e594-09db-4642-8d9b-89cdf5e7424f_part.mp3' sound = AudioSegment.from_mp3(inFile) original_bitrate = mediainfo(inFile)['bit_rate'] segment = sound[26.50-26.20:(29.80+0.22-26.20)*1000] #26.20 начало сегмента segment.export(outFile, format="mp3", bitrate=original_bitrate)
print("Converting flac / wav files to mp3") path = base_local + flac_ending files = [] for i in os.listdir(path): ext = i.split(".")[-1] if ext == "flac": files.append((i, "flac")) if ext == "wav": files.append((i, "wav")) newExt = "mp3" for i in files: print("Converting " + i[0]) audio = AudioSegment.from_file(path + i[0], i[1]) metadata = mediainfo(path + i[0]).get("TAG", None) try: # Deals with weird case where there is title and TITLE, in which title is wrong. metadata["title"] = metadata["TITLE"] except: # There must not have been a TITLE tag. pass newName = i[0][:-(len(i[1])+1)] + "." + newExt newPath = auto_add_location + newName tempPath = newName audio.export(tempPath, format=newExt, bitrate=mp3Bitrate, tags=metadata) os.system(""" mv "{}" "{}" """.format(tempPath, newPath)) os.system(""" rm "{}" """.format(path + i[0])) print("Done!")
def write_output_files(StartOfFileLoc, FileNum, FirstStringRemoveFlag): CurrentTxtFileName = 'Output_text/'+get_file_name(FileNum) CurrentInputAudioFileName = 'Input_audio/'+get_file_name(FileNum)+'.mp3' CurrentOutputAudioFileName = 'Output_audio/'+get_file_name(FileNum) EndOfFileLoc = find_next_endoffile (lines, StartOfFileLoc, FirstStringRemoveFlag) #Count number of segments and define general speech properties i = StartOfFileLoc NumSegments = 0 while i <= EndOfFileLoc: k, i = get_speech_segment_range(lines, i) NumSegments = NumSegments + 1 GeneralProperties = get_word(lines[EndOfFileLoc]) LastSegmentLength = i-k if EndOfFileLoc == k+1: TrimSegment = True else: TrimSegment = False print ('Number of Segments: ', NumSegments) print ('General properties: ', GeneralProperties) if not(is_general_tag(GeneralProperties)) and not(is_noise_tag(GeneralProperties)): return CurLoc = StartOfFileLoc if NumSegments == 1: SegmentStart, SegmentEnd = get_speech_segment_range(lines, CurLoc) CurText='' if FirstStringRemoveFlag: StartFrom = SegmentStart+2 else: StartFrom = SegmentStart+1 for i in range(StartFrom, SegmentEnd): CurText = CurText + get_word(lines[i])+' ' TempTxtFileName = CurrentTxtFileName + '.txt' f = open(TempTxtFileName, 'w') f.write(CurText) f.close() TempAudioFileName = CurrentOutputAudioFileName + '.mp3' copyfile(CurrentInputAudioFileName, TempAudioFileName) elif NumSegments == 2 and TrimSegment: SegmentStart, SegmentEnd = get_speech_segment_range(lines, CurLoc) CurText='' if FirstStringRemoveFlag: StartFrom = SegmentStart+2 else: StartFrom = SegmentStart+1 for i in range(StartFrom, SegmentEnd): CurText = CurText + get_word(lines[i])+' ' TempTxtFileName = CurrentTxtFileName + '.txt' f = open(TempTxtFileName, 'w') f.write(CurText) f.close() TempAudioFileName = CurrentOutputAudioFileName + '.mp3' copyfile(CurrentInputAudioFileName, TempAudioFileName) CurLoc = SegmentEnd + 1 SegmentStart, SegmentEnd = get_speech_segment_range(lines, CurLoc) else: CurSeg = 1 AllDuration = 0 OldEndTime = 0 Sound = AudioSegment.from_mp3(CurrentInputAudioFileName) original_bitrate = mediainfo(CurrentInputAudioFileName)['bit_rate'] while CurSeg <= NumSegments-1: SegmentStart, SegmentEnd = get_speech_segment_range(lines, CurLoc) CurText='' if (CurSeg == 1) and FirstStringRemoveFlag: StartFrom = SegmentStart+2 else: StartFrom = SegmentStart+1 for i in range(StartFrom, SegmentEnd): CurText = CurText + get_word(lines[i])+' ' CurText = CurText + ' '+GeneralProperties TempTxtFileName = CurrentTxtFileName + '(' + str(CurSeg) + ').txt' f = open(TempTxtFileName, 'w') f.write(CurText) f.close() print(lines[SegmentStart]) StartTime, EndTime = get_segment_time(lines[SegmentStart]) Duration = EndTime-StartTime if CurSeg == 1: GapDuration = 0 else: GapDuration = StartTime-OldEndTime AllDuration = AllDuration + GapDuration print(AllDuration, GapDuration, StartTime, EndTime, Duration) SegmentSound = Sound[1000*AllDuration:1000*(AllDuration + Duration)] TempOutputFileName = CurrentOutputAudioFileName + '(' + str(CurSeg) + ').mp3' SegmentSound.export(TempOutputFileName, format="mp3", bitrate=original_bitrate) AllDuration = AllDuration + Duration OldEndTime = EndTime CurLoc = SegmentEnd + 1 CurSeg = CurSeg + 1 SegmentStart, SegmentEnd = get_speech_segment_range(lines, CurLoc) if (NumSegments > 1) and (LastSegmentLength > 2): NextString = SegmentStart NextFirstStringRemoveFlag = True else: NextString = SegmentEnd + 1 NextFirstStringRemoveFlag = False return NextString, NextFirstStringRemoveFlag
Side data: Должно получиться (получается) <SpeechSegment ch="1" stime="26.20" etime="45.37" lang="eng" tconf="0.91"> <Word id="60" stime="26.50" dur="0.13" conf="0.990"> the </Word> <Word id="61" stime="26.63" dur="0.24" conf="0.990"> person </Word> <Word id="62" stime="26.93" dur="0.12" conf="0.990"> who </Word> <Word id="63" stime="27.05" dur="0.22" conf="0.990"> dealt </Word> <Word id="64" stime="27.27" dur="0.14" conf="0.990"> with </Word> <Word id="65" stime="27.44" dur="0.25" conf="0.990"> me </Word> <Word id="66" stime="27.82" dur="0.26" conf="0.990"> was </Word> <Word id="67" stime="28.13" dur="0.21" conf="0.990"> really </Word> <Word id="68" stime="28.50" dur="0.65" conf="0.990"> professional </Word> <Word id="69" stime="29.62" dur="0.18" conf="0.990"> he </Word> <Word id="70" stime="29.80" dur="0.22" conf="0.990"> helped </Word> ''' fromFile = r'Joined.mp3' sound = AudioSegment.from_mp3(fromFile) search = sound[26.50*1000:(29.80+0.22)*1000] seekInFile = r'Input_audio\0f57e594-09db-4642-8d9b-89cdf5e7424f.mp3' src = AudioSegment.from_mp3(seekInFile) original_bitrate = mediainfo(seekInFile)['bit_rate'] r =findSegment(src, search ) e = src[r[0]:r[1]] e.export(r'Output_audio\part_from_seek.mp3', format="mp3", bitrate=original_bitrate) testFind() #test_small() #test_joined()
def getDuration(self, song): """Returns the duration of song.""" if song[-4:] == '.mp3': return MP3(song).info.length return int(float(mediainfo(song)['duration']))
return trim_ms def detect_trailing_silence(sound, silence_threshold=-50.0, chunk_size=10): return detect_leading_silence(sound.reverse(), silence_threshold, chunk_size) def trim_leading_silence(sound): leading_silence = detect_leading_silence(sound) return sound[leading_silence:] def trim_trailing_silence(sound): trailing_silence = detect_trailing_silence(sound) return sound[:-trailing_silence] if __name__ == "__main__": files = [{'path':sys.argv[1]+name, 'metadata':mediainfo(sys.argv[1]+name), 'name':name} for name in os.listdir(sys.argv[1])] show = [] count = 0 for f in files: if len(show) > 0: duration = reduce(lambda a,b: a+b, [x['seg'].duration_seconds for x in show]) if duration >= 60 ** 2 * 2 + (60 * 5): first_file = show[0] start = 0 start_time = (0, 0) track_num = 0 track_info = []
#!/usr/bin/env python from pydub import AudioSegment from pydub.utils import mediainfo from glob import glob from os import path from warnings import warn # get all flacs in current folder flac_files = glob('./*.flac') for i in range(0, len(flac_files)): cur_flac = AudioSegment.from_file(flac_files[i], 'flac') outfile = path.splitext(flac_files[i])[0] + '.mp3' if path.isfile(outfile): warn('File ' + outfile + ' already exists. Skipping...') else: id3tags = mediainfo(flac_files[i]).get('TAG', {}) cur_flac.export(outfile, format = 'mp3', tags = id3tags, bitrate = '192k')
#!/usr/bin/env python from pydub import AudioSegment from pydub.utils import mediainfo from glob import glob from os import path from warnings import warn # get all mp3 in current folder mp3_files = glob('./*.mp3') for i in range(0, len(mp3_files)): cur_mp3 = AudioSegment.from_file(mp3_files[i], 'mp3') outfile = path.splitext(mp3_files[i])[0] + '_192kb_' + '.mp3' if path.isfile(outfile): warn('File ' + outfile + ' already exists. Skipping...') else: id3tags = mediainfo(mp3_files[i]).get('TAG', {}) cur_mp3.export(outfile, format = 'mp3', tags = id3tags, bitrate = '192k')