1.安装python
Welcome to Python.org
2.安装FFmpeg
https://ffmpeg.org/download.html#build-windows
将下载好的文件保存到系统环境变量中,例如
C:\Program Files\ffmpeg\bin
3.将如下代码保存为.py文件
记得替换路径,示例代码中index.m3u8指的是播放列表文件。这个文件包含了视频流的信息和各个片段(.ts 文件)的链接。
import os
import subprocess
def convert_m3u8_to_mp4(m3u8_path, output_path):
# 检查FFmpeg是否可用
try:
subprocess.run(['ffmpeg', '-version'], check=True)
except subprocess.CalledProcessError:
print("FFmpeg is not installed or not found in your PATH.")
return
# 使用FFmpeg命令将.m3u8转换为.mp4
command = ['ffmpeg', '-i', m3u8_path, '-c', 'copy', output_path]
try:
subprocess.run(command, check=True)
print(f"Conversion successful! Output saved to: {output_path}")
except subprocess.CalledProcessError as e:
print(f"Error during conversion: {e}")
# 示例用法
m3u8_file = r'C:\迅雷下载\m(2).m3u8\index.m3u8' # 替换为你的.m3u8文件路径
output_file = r'C:\迅雷下载\m(2).m3u8\output_video.mp4' # 输出的MP4文件路径
convert_m3u8_to_mp4(m3u8_file, output_file)
4.在cmd中使用如下命令执行该脚本
python 文件名.py