-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcov_m3u8_2_mp4.py
More file actions
50 lines (43 loc) · 1.47 KB
/
cov_m3u8_2_mp4.py
File metadata and controls
50 lines (43 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import argparse
import subprocess
import os
import sys
def convert_m3u8_to_mp4(input_m3u8, output_mp4):
"""
执行 FFmpeg 转换命令
"""
# 检查输入文件是否存在
if not os.path.exists(input_m3u8):
print(f"错误:输入文件 '{input_m3u8}' 不存在")
sys.exit(1)
# 创建输出目录(如果不存在)
output_dir = os.path.dirname(output_mp4)
if output_dir and not os.path.exists(output_dir):
os.makedirs(output_dir, exist_ok=True)
# 构建 FFmpeg 命令
cmd = [
"ffmpeg",
"-allowed_extensions", "ALL",
"-i", input_m3u8,
"-c", "copy",
"-movflags", "+faststart",
output_mp4
]
# 执行命令
try:
subprocess.run(cmd, check=True, stderr=subprocess.PIPE, text=True)
print(f"转换成功!输出文件已保存至: {output_mp4}")
except subprocess.CalledProcessError as e:
print(f"转换失败,错误信息:\n{e.stderr}")
sys.exit(1)
if __name__ == "__main__":
# 设置命令行参数
parser = argparse.ArgumentParser(description="将 M3U8 转换为 MP4")
parser.add_argument("-i", "--input", required=True, help="输入 M3U8 文件路径")
parser.add_argument("-o", "--output", required=True, help="输出 MP4 文件路径")
args = parser.parse_args()
# 执行转换
convert_m3u8_to_mp4(
input_m3u8=os.path.abspath(args.input),
output_mp4=os.path.abspath(args.output)
)