File size: 6,212 Bytes
b58079c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | 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" # 输出为AVLips格式
EXTRACT_AUDIO = True # 是否从视频中提取音频
def extract_audio_from_video(video_path, audio_path):
"""使用ffmpeg从视频中提取音频"""
try:
cmd = [
"ffmpeg", "-i", video_path,
"-vn", # 不要视频
"-acodec", "pcm_s16le", # WAV格式
"-ar", "16000", # 采样率16kHz(librosa默认)
"-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()
|