我的脚本运行的简单方法是用户提供文件夹位置和文件类型,然后 glob.glob() 查找具有提供的文件类型的文件并将它们添加到列表中。然后它使用 for 循环并遍历列表并转换每个视频。但是当我尝试运行我的 ffmpeg 命令时它不喜欢。任何帮助都是极好的。我还在使用带有 64 位 ffmpeg 和 Python 3.3 的 Win 7 64 位,这是错误:
OS Error
Traceback (most recent call last):
File "C:\Python33\lib\subprocess.py", line 1106, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\Workspace\PythonConverter\HTMLandPythonConverter\Converter.py", line 77, in <module>
massConvert(fileNames)
File "C:\Users\user\Workspace\PythonConverter\HTMLandPythonConverter\Converter.py", line 47, in massConvert
convertVideotoNewFormat('.mp4', x)
File "C:\Users\user\Workspace\PythonConverter\HTMLandPythonConverter\Converter.py", line 61, in convertVideotoNewFormat
myFile = subprocess.Popen(ffmpegString)#, stdout=subprocess.PIPE, stderr=subprocess.PIPE
File "C:\Python33\lib\subprocess.py", line 820, in __init__
restore_signals, start_new_session)
File "C:\Python33\lib\subprocess.py", line 1112, in _execute_child
raise WindowsError(*e.args)
FileNotFoundError: [WinError 2] The system cannot find the file specified
这是我的代码:
import subprocess
from subprocess import call
import glob
fileNames = []
fileLocation = {}
filetype = {}
def convertString(location):
s = list(location)
for i in range(len(s)):
if s[i] in '\\':
s[i] = '/'
if s[len(s)-1] != '/':
s.append('/')
location = "".join(s)
return location
def convertStringBack(stringTo):
s = list(stringTo)
for i in range(len(s)):
if s[i] in '/':
s[i] = '\\'
stringTo = "".join(s)
return stringTo
def fileTypeTester():
FieldType = '*' + input('What\'s the file type we are converting from?')
typeSplit = list(FieldType)
if typeSplit[1] != '.':
typeSplit.insert(1,'.')
FieldType = "".join(typeSplit)
if FieldType not in ['*.flv','*.kdb']:
print('Not a valid file type')
else:
return FieldType
return None
def massConvert(listOfFiles):
print('Starting Conversion')
for x in listOfFiles:
#x = convertStringBack(x)
print('Converting ' + x + ' to .mp4')
convertVideotoNewFormat('.mp4', x)
print('Finished File Conversion')
def convertVideotoNewFormat(newFormat, fileLoc):
newFilePath = fileLoc[0:len(fileLoc)-4]
ffmpegString = ["ffmpeg64","-i", fileLoc,"-qscale","0","-ar","22050","-vcodec","libx264",newFilePath,newFormat]
try:
subprocess.check_call(newFilePath)
except OSError:
print('OS Error')
except subprocess.CalledProcessError:
print('Subprocess Error')
myFile = subprocess.Popen(ffmpegString)
print(myFile)
#This will replace old HTML flv object tag with new video tag, but it is yet to be implemented
def replaceHTML():
pass
fileLocation = input('What is the path of the files you\'d like to convert?')
fileLocation = convertString(fileLocation)
fileType = fileTypeTester()
fileNames = glob.glob(fileLocation + fileType)
massConvert(fileNames)
我环顾四周,大多数教程都在 2.7 中,代码是 3.3,我找不到将 ffmpeg 用于 3.3 的教程。我的 ffmpeg 在我的 PATH 上设置为“ffmpeg64”。
谢谢!