《自拍教程69》Python 批量重命名音频文件,AV专家必备!
# coding=utf-8
import os
import re
import subprocess
class AudioInfoGetter():
'''获取音频文件的codec, sample_rate, channels, bitrate'''
def __init__(self, audio_file):
'''判断文件是否存在,如果存在获取其mediainfo信息'''
if os.path.exists(audio_file):
self.audio_file = audio_file
p_obj = subprocess.Popen('mediainfo "%s"' % self.audio_file, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
self.info = p_obj.stdout.read().decode("utf-8") # 解决非英文字符的编码问题
else:
raise FileNotFoundError("Not this File!") # 如果多媒体文件路径不存在,必须中断
def get_audio_codec(self):
'''获取音频的编码格式,比如Mepg3L1就是我们常说的Mp3, 还有AAC系列,AMR系列,Flac等等'''
try:
audio_codec = re.findall(r"Format\s+:\s(.*)", self.info)[-1] # 取第最后一个Format字段
audio_codec = audio_codec.strip() # 去除前后的空格
if (audio_codec == "MPEG Audio"):
audio_codec = self.__format_mpeg_audio()
elif (audio_codec == "AMR"):
audio_codec = self.__format_amr_audio()
elif (audio_codec == "AAC"):
audio_codec = self.__format_aac_audio()
elif ("PCM" in audio_codec):
audio_codec = "PCM"
else:
pass
except:
audio_codec = "undef" # 防止程序因为异常而中断
return audio_codec
def get_audio_channel(self):
'''获取声道数,如果是双声道是stereo, 如果是单声道是mono,还存在5声道的情况'''
try:
audio_channel = re.findall(r"Channel\(s\)\s+:\s(.*)\schannel.*", self.info)[-1]
audio_channel = audio_channel.strip() # 过滤掉前后空格
if audio_channel == "2":
audio_channel = "stereo"
elif audio_channel == "1":
audio_channel = "mono"
elif audio_channel == "5":
audio_channel = "5-channels"
else:
audio_channel = "undef" # 设置为undef,表示特殊异常规格,建议人工验证并考虑手动重命名
except:
audio_channel = "undef" # 防止程序因为异常而中断
return audio_channel
def get_audio_sample_rate(self):
'''获取音频采样率,比如常见的48Khz, 44.1Khz等'''
try:
audio_sample_rate = re.findall(r"Sampling rate\s+:\s(.*)", self.info)[-1].strip()
audio_sample_rate = audio_sample_rate.replace(" ", "") # 去1 536 这个数字里的空格
if "K" not in audio_sample_rate:
audio_sample_rate = audio_sample_rate.replace("Hz", "") # 先去掉“Hz"
audio_sample_rate = str(int(int(audio_sample_rate) / 1000))
audio_sample_rate = audio_sample_rate + "Khz" # 再添上KHz的单位
elif audio_sample_rate.endswith(".0Khz"):
audio_sample_rate = audio_sample_rate.replace(".0", "")
elif "/" in audio_sample_rate: # 偶尔会有多个采样率的情况 48.0 Khz / 44.1 KHz 这种
audio_sample_rate = "undef" # # 设置为undef,表示特殊异常规格,建议人工验证并考虑手动重命名
else:
pass
except:
audio_sample_rate = "undef" # 防止程序因为异常而中断
return audio_sample_rate
def get_audio_bitrate(self):
'''获取音频比特率,比如96Kbps, 128Kbps'''
try:
audio_bitrate = re.findall(r"Bit rate\s+:\s(.*)", self.info)[-1].strip()
audio_bitrate = audio_bitrate.replace(" ", "") # 去掉1 536 这个数字里的空格
if "K" not in audio_bitrate:
audio_bitrate = audio_bitrate.replace("bps", "") # 先去掉“bps"
audio_bitrate = str(int(audio_bitrate) / 1000)
audio_bitrate = audio_bitrate + "Kbps" # 再添上KHz的单位
except:
audio_bitrate = "undef" # 防止程序因为异常而中断
return audio_bitrate
def get_audio_container(self):
'''获取音频容器,即文件后缀名'''
_, audio_container = os.path.splitext(self.audio_file)
if not audio_container:
raise NameError("This file no extension")
audio_container = audio_container.replace(".", "")
return audio_container
def __format_mpeg_audio(self):
'''如果是Mpeg Auido的音频格式(常见的比如Mp3(Mpeg1 Level3)),进行格式化'''
try:
mpeg_audio_version = re.findall(r"Format version\s+:\sVersion\s(.*)", self.info)[-1].strip()
mpeg_audio_profile = re.findall(r"Format profile\s+:\sLayer\s(.*)", self.info)[-1].strip()
mpeg_audio_profile = "Mpeg%sL%s" % (mpeg_audio_version, mpeg_audio_profile)
except:
mpeg_audio_profile = "undef"
return mpeg_audio_profile
def __format_amr_audio(self):
'''如果是amr的音频格式(常见的比如amr-nb amr-wb),进行格式化'''
try:
amr_profile = re.findall(r"Format profile\s+:\s(.*)", self.info)[-1].strip()
if amr_profile == "Wide band":
amr_profile = "AMR-WB"
elif amr_profile == "Narrow band":
amr_profile = "AMR-NB"
else:
amr_profile = "undef" # 设置为undef,表示特殊异常规格,建议人工验证并考虑手动重命名
except:
amr_profile = "undef"
return amr_profile
def __format_aac_audio(self):
'''如果是acc的音频格式(常见的比如AAC-LC, AAC-LTP, HE-AAC, HE-AACV2),进行格式化'''
try:
amr_profile = re.findall(r"Format profile\s+:\s(.*)", self.info)[-1].strip()
if amr_profile == "LC":
aac_profile = "AAC-LC"
elif amr_profile == "LTP":
aac_profile = "AAC-LTP"
elif amr_profile.startswith("HE-AACv2"):
aac_profile = "HE-AACV2"
elif amr_profile.startswith("HE-AAC"):
aac_profile = "HE-AAC"
else:
aac_profile = "undef" # 设置为undef,表示特殊异常规格,建议人工验证并考虑手动重命名
except:
aac_profile = "undef"
return aac_profile
if __name__ == '__main__':
# 以下代码块,只是用来测试本模块的,一般不建议直接在这里大面积调用本模块'''
a_obj = AudioInfoGetter("C:\\好听的歌曲.wma")
audio_codec = a_obj.get_audio_codec()
print(audio_codec)