import os import sys import json import librosa import numpy as np from pathlib2 import Path import moviepy from moviepy.editor import VideoFileClip, AudioFileClip if __name__ == "__main__": path_data = Path("/mnt/data2/DATA/LRW/processing") # for each word v_ids = sorted(path_data.glob("*")) print(f"Have {len(v_ids)} dir files, \n {v_ids[:3]}") for i, ipath_id in enumerate(v_ids): id_name = ipath_id.stem dir_video = path_data.joinpath(id_name) v_dir_videos = sorted(dir_video.glob("*/*/video/video_orignal.mp4")) print(f"\nid= {id_name}, Have {len(v_dir_videos)} video files, \n {v_dir_videos[:3]}") # for each video for j, jpath_videos in enumerate(v_dir_videos): if (j+1) % 100 == 0: print(f"processing {j+1}/{len(v_dir_videos)}") jpath_data = jpath_videos.parent.parent jpath_frame = jpath_data.joinpath("frames") nframes = len(sorted(jpath_frame.glob("*.jpg"))) #print(f"jpath_data = {jpath_data}, nframe = {nframes}") ipath_video = jpath_data.joinpath("video/video_orignal.mp4") ipath_audio = jpath_data.joinpath("audio/audio_orignal.wav") ipath_save_feat = jpath_data.joinpath("audio/feat_mfcc_align.npy") video = VideoFileClip(str(ipath_video)) # print(f"\nvideo duration = {video.duration}, fps = {video.fps}, (start, end)=({video.start},{video.end}), nframes={int(video.duration * video.fps)}") audio = AudioFileClip(str(ipath_audio)) # print(f"audio duration = {audio.duration}, fps = {audio.fps}, (start, end)=({audio.start},{audio.end}), nframes={int(audio.duration * audio.fps)}") # for some data not have .wav files if not ipath_audio.exists(): print(f"NOT EXISTING: {ipath_audio}") continue if ipath_save_feat.exists(): print(f"EXSTING, {ipath_save_feat}") continue fps = video.fps data, sr = librosa.load(str(ipath_audio), sr=16000) # 16000 = fps * 550 让这个sr是fps的整数, hop_len = int(sr / fps) # 640 n_fft = int(sr * 0.1) # default 2048 # print(f"data={data.shape}, sr={sr}, fps={fps}, hop_len={sr/fps}, frames = {(len(data) / sr) * fps}, minmax = {np.min(data)},{np.max(data)}") mfcc = librosa.feature.mfcc(y=data, sr=sr, n_fft=n_fft, # FFT窗口的长度 hop_length=hop_len, # 帧移 win_length=n_fft, # 窗口长度,默认=n_fft window="hann", # 加hanming窗 n_mfcc=27, fmin=0.0, fmax=8000) # mfcc = np.log(mfcc+1).T mfcc = mfcc.T # print(f"sr={sr}, data ={data.shape}, mfcc = {mfcc.shape}, (min,max)=({np.min(mfcc), np.max(mfcc)}") if mfcc.shape[0] < nframes: print(f"\nERROR:") print(f"video duration = {video.duration}, fps = {video.fps}, (start, end)=({video.start},{video.end}), nframes={int(video.duration * video.fps)}") print(f"audio duration = {audio.duration}, fps = {audio.fps}, (start, end)=({audio.start},{audio.end}), nframes={int(audio.duration * audio.fps)}") print(f"sr={sr}, data ={data.shape}, mfcc = {mfcc.shape}, (min,max)=({np.min(mfcc), np.max(mfcc)}") print(f"mfcc length(={mfcc.shape[0]}) can not < nframe {nframes} ") continue else: # save mfcc_align = mfcc[:nframes, :] np.save(str(ipath_save_feat), mfcc_align) # # extarct mfcc feature # audio, sr = librosa.load(str(ipath_audio), sr=16000) # (69760, ) # mfcc_orig = python_speech_features.mfcc(audio, 16000, winstep=0.01) # #duration = len(audio) / sr # #print(f"audio shape={audio.shape}, mfcc shape={mfcc_orig.shape}, duration={duration}") # # splited by windows (to fit video frame with fps 25) # wind_stride = 4 # 4 x 10ms = 40 ms, video(25fps) frame = 1/25s = 40ms # wind_length = 28 # 28 x 10ms = 280 ms, = 7 frames * 40 ms # # seq_len2 = (seq_len1 + 2*pad - wind_length) // wind_stride + 1 # mfcc = splited_by_windows(mfcc_orig, wind_stride, wind_length, padding=True) # seq_len2 x 28 x 12, # #print(f"MFCC input size (splited by windows) ={mfcc.shape}") # save files # np.save(str(ipath_feat_orignal_save), mfcc_orig) # np.save(str(ipath_feat_save), mfcc) # print(f"SAVE: mfcc size={mfcc.shape} --> {str(ipath_feat_save)}") # mfcc size=(31, 28, 12) # print(f"SAVE: mfcc size={mfcc_orig.shape} --> {str(ipath_feat_orignal_save)}") # mfcc size=(31, 28, 12) #break #break