-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
60 lines (49 loc) · 2.08 KB
/
utils.py
File metadata and controls
60 lines (49 loc) · 2.08 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
51
52
53
54
# convert from mkv to mp4 using ffmpeg
import subprocess
import os
import pandas as pd
def mkv_to_mp4(video_path, converted_video_path):
"""将 MKV 视频转换为 MP4 (H.264 + AAC)"""
if not os.path.exists(converted_video_path):
print(f"🎬 转换 {video_path} 为 {converted_video_path} (H.264)...")
cmd = [
"ffmpeg", "-y", "-i", video_path,
"-c:v", "libx264", "-preset", "fast", "-crf", "23",
"-c:a", "aac", "-b:a", "128k",
converted_video_path
]
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("✅ 视频转换完成!")
def extract_audio(video_path, audio_output_path):
"""从 MKV 文件提取音频,并转换为 MP3"""
if not os.path.exists(audio_output_path):
print(f"🎵 提取音频到 {audio_output_path}...")
cmd = [
"ffmpeg", "-y", "-i", video_path, # 输入 MKV 文件
"-vn", # 不处理视频
"-acodec", "mp3", # 输出 MP3 音频
"-b:a", "192k", # 保持较好的音质
audio_output_path
]
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("✅ 音频提取完成!")
def convert_video(video_path, converted_video_path):
"""使用 FFmpeg 转换 AV1 视频为 H.264 (如果需要)"""
if not os.path.exists(converted_video_path):
print(f"转换 {video_path} 为 {converted_video_path} (H.264)...")
cmd = [
"ffmpeg", "-y", "-i", video_path,
"-c:v", "libx264", "-preset", "fast", "-crf", "23",
"-c:a", "aac", "-b:a", "128k",
converted_video_path
]
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("视频转换完成!")
def get_unique_filename(filename):
"""如果文件存在,则生成一个不重名的文件名"""
base, ext = os.path.splitext(filename)
counter = 1
while os.path.exists(filename):
filename = f"{base}_{counter}{ext}"
counter += 1
return filename