【视频格式转换】.264格式转为mp4格式
背景
视频格式转换是多媒体处理中的常见需求,尤其在视频编辑、存储或跨平台播放时。H.264(即AVC)是一种广泛使用的视频编码标准,以其高压缩率和良好兼容性著称,但通常以容器格式(如.264
裸流文件)存储。MP4则是一种通用容器格式,支持H.264编码,且兼容绝大多数设备和播放器。将.264
转换为MP4可提升文件易用性,便于直接播放或分享。
概述
.264
文件是纯视频编码数据流,缺乏音频、元信息等容器结构。转换为MP4需将H.264流封装到MP4容器中,通常需借助工具完成。转换过程需注意参数匹配(如帧率、分辨率),避免质量损失。常见方法包括使用FFmpeg命令行工具、专业软件(如HandBrake)或在线转换服务。
一、
要将 .264
格式的视频转换为 .mp4
格式,可以使用 ffmpeg
工具。Python 中可以通过 subprocess
模块调用 ffmpeg
。
下面是一个完整的 Python 脚本,它将当前目录下的 .264
文件转换为 .mp4
文件(需要你本地安装了 ffmpeg
):
✅ 脚本内容(保存为 convert_264_to_mp4.py
):
import os
import subprocessdef convert_264_to_mp4(input_path, output_path):# 构建 ffmpeg 命令command = ['ffmpeg','-y', # 自动覆盖输出文件'-i', input_path,'-c:v', 'copy', # 复制视频流而不重新编码output_path]try:subprocess.run(command, check=True)print(f'✅ 成功转换: {input_path} → {output_path}')except subprocess.CalledProcessError as e:print(f'❌ 转换失败: {input_path}')print(e)def batch_convert_264_to_mp4(directory):for filename in os.listdir(directory):if filename.lower().endswith('.264'):input_path = os.path.join(directory, filename)output_filename = os.path.splitext(filename)[0] + '.mp4'output_path = os.path.join(directory, output_filename)convert_264_to_mp4(input_path, output_path)if __name__ == '__main__':target_directory = '.' # 当前目录batch_convert_264_to_mp4(target_directory)
⚙️ 使用说明:
-
确保你已经安装了
ffmpeg
,命令行中运行ffmpeg -version
可测试是否安装成功。 -
Windows 下可从 Download FFmpeg 下载并配置环境变量。
-
将上面脚本保存为
.py
文件。 -
把
.264
文件放到和脚本相同的目录。 -
打开命令行(终端),运行:
python convert_264_to_mp4.py
二、
如果你希望把 .264
文件拖到 .py
脚本上自动运行,只转换拖入的那个文件。这种方式适用于 Windows 系统 中的“拖拽执行”。
下面是你要的脚本版本,支持将一个或多个 .264
文件拖到 .py
文件上运行,每个文件转换为对应的 .mp4
文件:
✅ 脚本内容:convert_drag_drop.py
import sys
import os
import subprocessdef convert_264_to_mp4(input_path):if not input_path.lower().endswith('.264'):print(f"⚠️ 不是.264文件,跳过: {input_path}")returnoutput_path = os.path.splitext(input_path)[0] + '.mp4'command = ['ffmpeg','-y', # 自动覆盖'-i', input_path,'-c:v', 'copy', # 不重新编码output_path]try:subprocess.run(command, check=True)print(f"✅ 成功转换: {input_path} → {output_path}")except subprocess.CalledProcessError as e:print(f"❌ 转换失败: {input_path}")print(e)if __name__ == '__main__':if len(sys.argv) < 2:print("请将 .264 文件拖拽到本脚本上运行。")input("按下回车退出...")sys.exit(1)for file_path in sys.argv[1:]:convert_264_to_mp4(file_path)input("\n全部处理完成,按回车退出...")
📦 使用方法:
-
安装好
ffmpeg
,并加入系统环境变量。 -
将以上脚本保存为
convert_drag_drop.py
。 -
将
.264
文件拖拽到该.py
文件图标上,双击运行。 -
会在同目录下生成同名的
.mp4
文件。
🧪 支持多个文件:
你也可以一次拖多个 .264
文件到脚本图标上,它会依次转换。
如果你希望脚本在转换完成后不需要按回车、自动退出,只需将前面脚本中最后这行代码删除即可:
# input("\n全部处理完成,按回车退出...")
三、
我们可以用 tkinter
弹出文件选择窗口,让用户手动选择 .264
文件,然后转换为 .mp4
,并在转换完成后自动打开输出目录。
✅ 符合新需求的完整脚本如下:
import os
import subprocess
import tkinter as tk
from tkinter import filedialog
import platformdef select_file():root = tk.Tk()root.withdraw() # 不显示主窗口file_path = filedialog.askopenfilename(title="选择 .264 文件",filetypes=[("H.264 视频文件", "*.264")])return file_pathdef convert_264_to_mp4(input_path):if not input_path or not input_path.lower().endswith('.264'):print("❌ 未选择合法的 .264 文件。")returnoutput_path = os.path.splitext(input_path)[0] + '.mp4'command = ['ffmpeg','-y','-i', input_path,'-c:v', 'copy',output_path]try:subprocess.run(command, check=True)print(f"✅ 成功转换: {input_path} → {output_path}")return output_pathexcept subprocess.CalledProcessError as e:print(f"❌ 转换失败: {input_path}")print(e)return Nonedef open_output_folder(file_path):folder = os.path.dirname(file_path)system = platform.system()try:if system == "Windows":os.startfile(folder)elif system == "Darwin": # macOSsubprocess.run(['open', folder])else: # Linuxsubprocess.run(['xdg-open', folder])except Exception as e:print(f"❌ 无法打开目录: {folder}")print(e)if __name__ == '__main__':input_file = select_file()output_file = convert_264_to_mp4(input_file)if output_file:open_output_folder(output_file)
📦 功能一览:
功能 | 描述 |
---|---|
✅ 弹出窗口 | 使用 tkinter 选择 .264 文件 |
✅ 自动转换 | 使用 ffmpeg 执行 .264 → .mp4 |
✅ 自动打开目录 | 转换完成后自动打开输出文件所在的目录 |
✅ 多平台兼容 | 支持 Windows、macOS、Linux(自动识别) |
⚙️ 使用说明:
-
安装
ffmpeg
并配置到环境变量(命令行运行ffmpeg -version
通过即可)。 -
将脚本保存为
convert_with_gui.py
。 -
双击运行脚本,选择
.264
文件即可。