Python3 代码错误修复:视频信息提取脚本优化

本文修复了 Python3 代码中存在的错误,并优化了视频信息提取脚本。该脚本使用 mediainfo 获取视频信息,并将其写入 Excel 文件。

代码错误分析与修正

原始代码中存在以下错误:

  1. video_files 在使用前被引用,但是其定义在后面。
  2. 变量 video_folder 未定义,应该改为 video_path
  3. video_info 是一个字符串,不能使用索引获取其中的元素。
  4. 压缩比率计算公式中的常量 1000000000 应该改为 60
  5. 写入 Excel 文件时,压缩比率应该使用 value 属性而不是 number_format 属性。

以下是修正后的代码:

import os
import re
import subprocess
import openpyxl
import math
from pathlib import Path
from openpyxl.styles import Font, PatternFill, Alignment

# 视频格式
supported_formats = [
    '.mp4',
    '.avi',
    '.mkv',
    '.wmv',
    '.mov',
    '.flv',
    '.m2ts',
    '.ts',
    '.rm',
    '.rmvb',
    '.vob',
    '.3gp',
    '.webm',
    '.hdmov',
    '.mp4v',
    '.mpv4',
    '.divx',
    '.xvid',
    '.f4v',
    '.mpeg',
    '.asf',
    '.asx',
    '.m2t',
]

# 视频文件夹或文件路径
video_path = input('请输入文件或文件夹路径:')

# 输出Excel路径
excel_folder = '.'

# 输出Excel文件名
excel_file = 'video_info.xlsx'

# 正则表达式匹配码率数据中的空格
pattern = re.compile(r'(\d+)\s+(\d+)')

# 创建Excel文件
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'Video Info'

# 视频文件列表
video_files = []

# 判断video_path是单个视频文件还是文件夹
path = Path(video_path)
if path.is_file():
    video_files.append(video_path)
elif path.is_dir():
    # 获取视频文件列表(包括子目录)
    for root, dirs, files in os.walk(video_path):
        for file in files:
            for format in supported_formats:
                if file.endswith(format):
                    video_files.append(os.path.join(root, file))
                    break

if len(video_files) == 0:
    print('未找到视频文件, 请检查路径是否正确')
else:
    # 锁定表头
    ws.freeze_panes = 'A2'

    # 设置表头样式
    header_font = Font(bold=True, color='800080')  # 紫色
    header_fill = PatternFill('solid', fgColor='C5E0B4')
    header_alignment = Alignment(horizontal='center', vertical='center')
    headers = [
        '文件名',
        '文件路径',
        '格式',
        '大小',
        '时长',
        '分辨率',
        '码率',
        '帧率',
        '音频码率',
        '音频采样率',
        '音频语言',
        '压缩比率',
    ]
    for col, header in enumerate(headers, start=1):
        cell = ws.cell(row=1, column=col, value=header)
        cell.font = header_font
        cell.fill = header_fill
        cell.alignment = header_alignment

    # 处理失败文件个数
    failed_count = 0

    success_count = 0
    row = 2  # 从第二行开始写入数据
    for video_file in video_files:
        try:
            # 获取文件名
            file_name = os.path.basename(video_file)

            # 获取文件大小并进行单位换算
            size = os.path.getsize(video_file)
            if size < 1024:
                size_str = f'{size} B'
            elif size < 1024 * 1024:
                size_str = f'{size / 1024:.2f} KiB'
            elif size < 1024 * 1024 * 1024:
                size_str = f'{size / 1024 / 1024:.2f} MiB'
            else:
                size_str = f'{size / 1024 / 1024 / 1024:.2f} GiB'

            # 使用mediainfo获取视频信息 / 音频信息
            video_result = subprocess.run([
                'mediainfo',
                '--Inform=Video;%Format%|%Width%x%Height%|%BitRate/String%|%FrameRate%|%Duration/String3%',
                video_file,
            ], stdout=subprocess.PIPE)
            video_output = video_result.stdout.decode().strip()
            audio_result = subprocess.run([
                'mediainfo',
                '--Inform=Audio;%BitRate/String%|%SamplingRate/String%|%Language/String%',
                video_file,
            ], stdout=subprocess.PIPE)
            audio_output = audio_result.stdout.decode().strip()

            # 解析输出结果
            video_info = video_output.split('|')
            audio_info = audio_output.split('|')

            # 处理多音轨情况
            audio_bitrate = []
            audio_sampling_rate = []
            audio_lang = []
            for i in range(len(audio_info) // 3):
                audio_bitrate.append(audio_info[i * 3])
                audio_sampling_rate.append(audio_info[i * 3 + 1])
                audio_lang.append(audio_info[i * 3 + 2])

            # 时长取整
            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(size / duration_minutes / 60, 2)

            # 使用正则表达式替换码率数据中第一个数字和第二个数字之间的空格
            bitrate = re.sub(pattern, r'\1\2', video_info[2])

            # 写入Excel文件
            ws.cell(row=row, column=1, value=file_name)
            ws.cell(row=row, column=2, value=os.path.dirname(video_file))  # 写入文件夹路径
            ws.cell(row=row, column=3, value=video_info[0])
            ws.cell(row=row, column=4, value=size_str)
            ws.cell(row=row, column=5, value=duration)
            ws.cell(row=row, column=6, value=video_info[1])
            ws.cell(row=row, column=7, value=bitrate)
            ws.cell(row=row, column=8, value=video_info[3])
            ws.cell(row=row, column=9, value=' / '.join(audio_bitrate))
            ws.cell(row=row, column=10, value=' / '.join(audio_sampling_rate))
            ws.cell(row=row, column=11, value=' / '.join(audio_lang))
            ws.cell(row=row, column=12, value=ratio)
            # 打印进度
            print(f'已处理 {success_count+1}/{len(video_files)} 共计', end='\r')
            success_count += 1
            row += 1

        except Exception as e:
            # 处理失败,跳过并打印文件名和具体错误信息
            failed_count += 1
            print(f'处理文件 {file_name} 时失败:{e}')
            # 将失败的文件写入到err.txt
            with open('err.txt', 'a') as f:
                f.write(f'{file_name}\n')
            continue

    # 调整L列左对齐
    l_col = ws['L']
    for cell in l_col:
        cell.alignment = Alignment(horizontal='left')  # 左对齐

    # 固定列宽
    for col in ws.columns:
        col_letter = col[0].column_letter
        if col_letter not in ['C', 'H']:  # 不固定格式、fps列的宽度
            ws.column_dimensions[col_letter].width = 11

    # 保存Excel文件
    if not os.path.exists(excel_folder):
        os.makedirs(excel_folder)
    excel_file_path = os.path.join(excel_folder, excel_file)
    wb.save(excel_file_path)

    # 打印处理失败文件个数和总计处理文件数
    total_count = len(video_files)
    print(
        f'已完成,处理成功 {success_count} 个文件,处理失败 {failed_count} 个文件,视频信息已保存到 {excel_file_path}'
    )

优化后的代码

  1. 代码中 video_files 变量的定义位置调整,使其在使用前就被定义。
  2. 代码中将 video_folder 变量改为了 video_path
  3. 代码中使用 split('|') 方法将 video_info 字符串分割成列表,然后使用索引获取所需信息。
  4. 代码中压缩比率计算公式中的常量 1000000000 被替换为 60
  5. 代码中写入 Excel 文件时,压缩比率使用 value 属性而非 number_format 属性。

总结

通过修复代码错误和优化代码结构,该脚本可以更稳定、高效地提取视频信息并写入 Excel 文件。

**注:**该脚本需要安装 mediainfo 工具,可通过 pip install mediainfo 命令进行安装。

Python3 代码错误修复:视频信息提取脚本优化

原文地址: https://www.cveoy.top/t/topic/mFA3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录