forked from shede333/SWConvertVideoToAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
142 lines (121 loc) · 4.79 KB
/
convert.py
File metadata and controls
142 lines (121 loc) · 4.79 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python2.7
# _*_ coding:UTF-8 _*_
"""
__author__ = 'wangshaowei'
"""
import argparse
import os
import subprocess
from datetime import datetime
def ffmpeg(src_path, dst_path):
"""
调用ffmpeg命令,执行转换过程(比特率一般为128kbps、196kbps;要求不高的话,可以使用32kbps,减小体积)
注意:比特率的单位为bps
:param src_path: 输入视频文件路径
:param dst_path: 输出文件路径
:return: bool值,转换结果成功or失败
"""
command = "ffmpeg -i '{}' -vn -ar 44100 -ac 2 -ab 32k -f mp3 '{}'".format(src_path, dst_path)
try:
subprocess.check_call(command, shell=True)
is_success = True
except subprocess.CalledProcessError as e:
print "error code: {}! shell command: {}".format(e.returncode, e.cmd)
is_success = False
return is_success
def convert_dir(dir_path, output_dir, is_traverse=False):
"""
批量转换视频文件
:param dir_path: 输入目录路径(内含视频文件)
:param output_dir: 输出目录
:param is_traverse: 是否遍历子目录,默认False
:return: 元组:转换成功数目,失败数目
"""
success_num = 0 # 转换成功的文件数
fail_num = 0 # 转换失败的文件数
file_list = []
dir_list = []
for file_name in os.listdir(dir_path):
if file_name.startswith("."):
continue
file_path = os.path.join(dir_path, file_name)
if os.path.isfile(file_path):
file_list.append(file_path)
elif os.path.isdir(file_path):
dir_list.append(file_path)
else:
print "暂时不支持该路径:{}".format(file_path)
for file_path in sorted(file_list):
print "file_path:", file_path
result = convert_file(file_path, output_dir)
if result:
success_num += 1
else:
fail_num += 1
for dir_path in sorted(dir_list):
print "dir_path:", dir_path
file_name = os.path.basename(dir_path)
result = convert_dir(dir_path, os.path.join(output_dir, file_name), is_traverse)
success_num += result[0]
fail_num += result[1]
return success_num, fail_num
def convert_file(file_path, output_dir):
"""
转换单个视频文件
:param file_path: 输入视频文件路径
:param output_dir: 输出文件目录路径
:return: bool值,转换结果成功or失败
"""
if not os.path.isdir(output_dir):
os.makedirs(output_dir)
video_file_name = os.path.basename(file_path)
name_body = video_file_name.rsplit(".", 1)[0]
audio_name = name_body + ".mp3"
dst_path = os.path.join(output_dir, audio_name)
return ffmpeg(file_path, dst_path)
def parse_arg():
"""解析命令行的输入的参数"""
parser = argparse.ArgumentParser(u"Python批量转换 视频 为 音频MP3(即提取音频文件)")
parser.add_argument(u"file_path", help=u"输入文件、目录路径,如果为目录,则遍历目录下的文件")
parser.add_argument(u"--output-dir", help=u"(可选)输出目录路径,如果不传,则使用输入文件目录")
parser.add_argument(u"--traverse", action=u'store_true',
help=u"(可选)src-path为目录是,是否遍历子目录,默认False")
return parser.parse_args()
def main():
"""主入口"""
try:
# 检测ffmpeg是否已安装
result = subprocess.check_output("ffmpeg -version", shell=True)
print "ffmpeg:\n", result
except subprocess.CalledProcessError:
print "ffmpeg未安装,请先安装:ffmpeg"
return
# 解析输入参数
command_param = parse_arg()
file_path = command_param.file_path
output_dir = command_param.output_dir
start_time = datetime.now()
success_num = 0 # 转换成功的文件数
fail_num = 0 # 转换失败的文件数
if os.path.isfile(file_path): # 文件
if not output_dir:
output_dir = os.path.dirname(file_path) # 与输入文件同级目录
result = convert_file(file_path, output_dir)
if result:
success_num += 1
else:
fail_num += 1
elif os.path.isdir(file_path): # 目录
if not output_dir:
output_dir = file_path # 使用输入目录
success_num, fail_num = convert_dir(file_path, output_dir,
is_traverse=command_param.traverse)
else:
assert False, u"file_path 不存在:'{}'".format(file)
end_time = datetime.now()
cost_seconds = (end_time - start_time).seconds
print u"转换的成功文件数:{}个".format(success_num)
print u"转换的失败文件数:{}个".format(fail_num)
print u"总耗时:{}秒".format(cost_seconds)
if __name__ == '__main__':
main()