#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Gui
import os
import subprocess
def get_source_file_list(source_dir):
if os.path.exists(source_dir):
file_list = []
for root, folders, file_items in os.walk(source_dir):
for file_item in file_items:
file_path = os.path.join(root, file_item)
if os.path.splitext(file_path)[-1].lower() == '.mp4':
file_list.append(file_path)
return file_list
if __name__ == "__main__":
# Set the source_dir & ffmpeg bin path.
source_dir = r"E:\\video\\satoshi"
ffmpeg_bin = "ffmpeg.exe"
# Get all the mp4 files.
file_list = get_source_file_list(source_dir)
# Construct the path then do the conversion.
for i in file_list:
target_dir = os.path.dirname(i)
source_file_base_name = os.path.basename(i)
target_file_base_name = os.path.splitext(source_file_base_name)[0] + '.mp3'
target_file_name = target_dir + os.sep + target_file_base_name
if not os.path.exists(target_file_name):
# Do the conversion.
print("Converting [{0}] to [{1}]...".format(i, target_file_name))
return_code = subprocess.call([ffmpeg_bin, "-i", i, target_file_name])
if return_code:
print("Conversion Error for <{0}>!".format(i))
continue
else:
print("Target file existed.")
可以贴代码,支持缩进,还能颜色高亮,不错!