| import os |
| import shutil |
| from tqdm import tqdm |
| import librosa |
| import soundfile as sf |
| import subprocess |
|
|
| """ |
| 将FT_work数据集转换为AVLips格式 |
| |
| FT_work原始结构: |
| videos/ |
| ├── train/ |
| │ ├── real/Real/*.mp4 |
| │ └── fake/{AniPortrait,Joyvasa,Ditto,Hallo,Sonic}/*.mp4 |
| ├── test/ |
| │ ├── real/Real/*.mp4 |
| │ └── fake/{SadTalk,EDTalk,Float}/*.mp4 |
| └── val/ |
| ├── real/Real/*.mp4 |
| └── fake/{AniPortrait,Joyvasa,Ditto,Hallo,Sonic}/*.mp4 |
| |
| 目标AVLips结构: |
| AVLips/ |
| ├── 0_real/*.mp4 |
| ├── 1_fake/*.mp4 |
| └── wav/ |
| ├── 0_real/*.wav |
| └── 1_fake/*.wav |
| |
| 注意:需要先从视频中提取音频 |
| """ |
|
|
| |
| FT_WORK_ROOT = "/apdcephfs_gy5/share_303628665/joyewu/dataset/FT_work" |
| OUTPUT_ROOT = "./AVLips" |
| EXTRACT_AUDIO = True |
|
|
|
|
| def extract_audio_from_video(video_path, audio_path): |
| """使用ffmpeg从视频中提取音频""" |
| try: |
| cmd = [ |
| "ffmpeg", "-i", video_path, |
| "-vn", |
| "-acodec", "pcm_s16le", |
| "-ar", "16000", |
| "-ac", "1", |
| "-y", |
| audio_path |
| ] |
| subprocess.run(cmd, capture_output=True, check=True) |
| return True |
| except subprocess.CalledProcessError as e: |
| print(f"音频提取失败: {video_path}") |
| print(f"错误: {e.stderr.decode() if e.stderr else 'Unknown error'}") |
| return False |
| except Exception as e: |
| print(f"音频提取异常: {video_path}") |
| print(f"错误: {str(e)}") |
| return False |
|
|
|
|
| def process_split(split_name, output_video_real, output_video_fake, output_audio_real, output_audio_fake): |
| """ |
| 处理一个数据集分割(train/test/val) |
| |
| Args: |
| split_name: 分割名称 ('train', 'test', 'val') |
| output_video_real: 输出真实视频目录 |
| output_video_fake: 输出假视频目录 |
| output_audio_real: 输出真实音频目录 |
| output_audio_fake: 输出假音频目录 |
| """ |
| split_path = os.path.join(FT_WORK_ROOT, "videos", split_name) |
| |
| if not os.path.exists(split_path): |
| print(f"警告: {split_path} 不存在,跳过") |
| return |
| |
| |
| real_path = os.path.join(split_path, "real", "Real") |
| if os.path.exists(real_path): |
| print(f"\n处理 {split_name}/real...") |
| video_files = [f for f in os.listdir(real_path) if f.endswith('.mp4')] |
| |
| for video_file in tqdm(video_files, desc="真实视频"): |
| src_video = os.path.join(real_path, video_file) |
| dst_video = os.path.join(output_video_real, video_file) |
| |
| |
| if not os.path.exists(dst_video): |
| shutil.copy2(src_video, dst_video) |
| |
| |
| if EXTRACT_AUDIO: |
| audio_file = video_file.replace('.mp4', '.wav') |
| dst_audio = os.path.join(output_audio_real, audio_file) |
| if not os.path.exists(dst_audio): |
| extract_audio_from_video(src_video, dst_audio) |
| |
| |
| fake_path = os.path.join(split_path, "fake") |
| if os.path.exists(fake_path): |
| print(f"\n处理 {split_name}/fake...") |
| fake_methods = os.listdir(fake_path) |
| |
| for method in fake_methods: |
| method_path = os.path.join(fake_path, method) |
| if not os.path.isdir(method_path): |
| continue |
| |
| video_files = [f for f in os.listdir(method_path) if f.endswith('.mp4')] |
| |
| for video_file in tqdm(video_files, desc=f"假视频/{method}"): |
| src_video = os.path.join(method_path, video_file) |
| |
| |
| new_name = f"{method}_{video_file}" |
| dst_video = os.path.join(output_video_fake, new_name) |
| |
| |
| if not os.path.exists(dst_video): |
| shutil.copy2(src_video, dst_video) |
| |
| |
| if EXTRACT_AUDIO: |
| audio_file = new_name.replace('.mp4', '.wav') |
| dst_audio = os.path.join(output_audio_fake, audio_file) |
| if not os.path.exists(dst_audio): |
| extract_audio_from_video(src_video, dst_audio) |
|
|
|
|
| def main(): |
| |
| print("创建输出目录结构...") |
| dirs = [ |
| os.path.join(OUTPUT_ROOT, "0_real"), |
| os.path.join(OUTPUT_ROOT, "1_fake"), |
| os.path.join(OUTPUT_ROOT, "wav", "0_real"), |
| os.path.join(OUTPUT_ROOT, "wav", "1_fake"), |
| ] |
| |
| for d in dirs: |
| os.makedirs(d, exist_ok=True) |
| print(f" 创建: {d}") |
| |
| |
| splits = ['train', 'test', 'val'] |
| |
| for split in splits: |
| print(f"\n{'='*50}") |
| print(f"处理分割: {split}") |
| print(f"{'='*50}") |
| |
| process_split( |
| split_name=split, |
| output_video_real=os.path.join(OUTPUT_ROOT, "0_real"), |
| output_video_fake=os.path.join(OUTPUT_ROOT, "1_fake"), |
| output_audio_real=os.path.join(OUTPUT_ROOT, "wav", "0_real"), |
| output_audio_fake=os.path.join(OUTPUT_ROOT, "wav", "1_fake") |
| ) |
| |
| print(f"\n{'='*50}") |
| print("转换完成!") |
| print(f"{'='*50}") |
| print(f"\n输出目录: {OUTPUT_ROOT}") |
| print(f"目录结构:") |
| print(f"├── 0_real/ ({len(os.listdir(os.path.join(OUTPUT_ROOT, '0_real')))} 个视频)") |
| print(f"├── 1_fake/ ({len(os.listdir(os.path.join(OUTPUT_ROOT, '1_fake')))} 个视频)") |
| print(f"└── wav/") |
| print(f" ├── 0_real/ ({len(os.listdir(os.path.join(OUTPUT_ROOT, 'wav', '0_real')))} 个音频)") |
| print(f" └── 1_fake/ ({len(os.listdir(os.path.join(OUTPUT_ROOT, 'wav', '1_fake')))} 个音频)") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|