Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audio file detection by magic numbers #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion clearvoice/dataloader/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,41 @@
import os
import sys
import librosa
import struct

def is_audio_file(file_path):
"""
通过检查文件头部的魔数来判断文件是否为音频文件

支持的格式:
- WAV (RIFF header)
- FLAC (fLaC header)
- MP3 (ID3 or MPEG sync)
- M4A/AAC (ftyp header)
"""
try:
with open(file_path, 'rb') as f:
header = f.read(12) # 读取前12个字节

# WAV: RIFF xxxxWAVE
if header.startswith(b'RIFF') and b'WAVE' in header:
return True

# FLAC: fLaC
if header.startswith(b'fLaC'):
return True

# MP3: ID3 或 MPEG sync
if header.startswith(b'ID3') or (header[0:2] == b'\xFF\xFB' or header[0:2] == b'\xFF\xF3'):
return True

# M4A/AAC: ftyp
if b'ftyp' in header:
return True

return False
except (IOError, OSError):
return False

def read_and_config_file(args, input_path, decode=0):
"""
Expand Down Expand Up @@ -60,7 +95,7 @@ def read_and_config_file(args, input_path, decode=0):
processed_list = librosa.util.find_files(input_path, ext="flac")
else:
# If it's a single file and it's a .wav or .flac, add to processed list
if input_path.lower().endswith(".wav") or input_path.lower().endswith(".flac"):
if is_audio_file(input_path):
processed_list.append(input_path)
else:
# Read file paths from the input text file (one path per line)
Expand Down