检查下面的python3代码进行换行美化def process_videovideo_file global success_count row failed_count try # 获取文件名 file_name = ospathbasenamevideo_file # 获取文件大小 size_str = convert_siz
def process_video(video_file): global success_count, row, failed_count try: # 获取文件名 file_name = os.path.basename(video_file)
# 获取文件大小
size_str = convert_size(os.path.getsize(video_file))
# 使用mediainfo获取视频信息 / 音频信息
with subprocess.Popen(['mediainfo', '--Inform=Video;%Format%|%Width%x%Height%|%BitRate/String%|%FrameRate%|%Duration/String3%', video_file], stdout=subprocess.PIPE) as video_proc, \
subprocess.Popen(['mediainfo', '--Inform=Audio;%BitRate/String%|%SamplingRate/String%|%Language/String%', video_file], stdout=subprocess.PIPE) as audio_proc:
video_output = video_proc.communicate()[0].decode().strip()
audio_output = audio_proc.communicate()[0].decode().strip()
# 解析输出结果
video_info = video_output.split('|')
audio_info = audio_output.split('|')
# 处理多音轨情况
audio_bitrate, audio_sampling_rate, audio_lang = zip(*[(audio_info[i*3],
audio_info[i*3+1], audio_info[i*3+2]) for i in range(len(audio_info)//3)])
# 时长取整
duration = video_info[4].split('.')[0]
h, m, s = duration.split(':')
duration = f'{h}:{m}:{str(s).zfill(2)}'
# 以分钟计算的时长
duration_minutes = int(h) * 60 + int(m) + math.ceil(float(s)) / 60
# 计算压缩比率
ratio = round(duration_minutes / os.path.getsize(video_file) * 1000000000, 2)
# 使用正则表达式替换码率数据中第1、2数字之间的空格
bitrate = re.sub(pattern, r'\1\2', video_info[2])
# 写入Excel文件
write_cell(ws, row, 1, file_name)
write_cell(ws, row, 2, os.path.dirname(video_file)) # 写入文件路径
write_cell(ws, row, 3, video_info[0])
write_cell(ws, row, 4, size_str)
write_cell(ws, row, 5, duration)
write_cell(ws, row, 6, video_info[1])
write_cell(ws, row, 7, bitrate)
write_cell(ws, row, 8, video_info[3])
write_cell(ws, row, 9, ' / '.join(audio_bitrate))
write_cell(ws, row, 10, ' / '.join(audio_sampling_rate))
write_cell(ws, row, 11, ' / '.join(audio_lang))
write_cell(ws, row, 12, ratio)
write_cell(ws, 2, 13, total_count) # 写入文件总数
# 打印进度
success_count += 1
row += 1
except Exception as e:
# 处理失败, 跳过并打印文件
failed_count += 1
print(f'处理文件 {video_file} 时失败:{e}')
# 写入失败到err.txt
with open('err.txt', 'a') as f:
f.write(f'{video_file}\n')
return False
return True
原文地址: http://www.cveoy.top/t/topic/bfRQ 著作权归作者所有。请勿转载和采集!