繁体   English   中英

使用ffmpeg转换视频时Python的回溯错误

[英]Traceback error with Python when using ffmpeg to convert a video

我的脚本运行的简单方式是用户提供文件夹位置和文件类型,然后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​​,但找不到用于3.3的ffmpeg教程。 我的ffmpeg在我的PATH上设置为“ ffmpeg64”。

谢谢!

第一:

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')

这部分可能无法做任何有用的事情。 newFilePath是您通过从视频文件中剥离最后4个字符而newFilePath的路径。 您不能在该路径上运行该程序,因为(除非您非常不幸),否则不会出现此类问题。

那解释了第一个OSError


对于第二个错误,它告诉您ffmpeg64不在PATH 你说这你的PATH ,但你可以得到这个错误来自没有其他的办法即行代码 您可以根据需要查找CreateProcess功能。

造成这种情况的三个常见原因:

  1. 您已经使用SET来修改特定cmd.exe会话(DOS提示符)中的PATH ,但是您正在其他DOS提示符下运行代码,或者正在运行GUI代码,或者由于其他原因而拥有不同的会话。
  2. 您已经使用控制面板为用户修改了PATH ,但是您正在以其他用户身份运行Python脚本(例如,作为WSGI服务的一部分)。
  3. 您根本没有修改PATH 您所依赖的事实是,您已经将cd放入与ffmpeg64和相同的目录中. 在Windows中的默认PATH上。

附带说明一下:

newFilePath = fileLoc[0:len(fileLoc)-4]

… 是相同的:

newFilePath = fileLoc[:-4]

…除了它更难阅读,更不健壮之外(如果fileLoc的4个字符以下错误会引发异常),而且更容易出错。

不过说真的,如果你想去掉一个扩展,你不想要么 如果您有foobar.mpeg ,您是否真的要将其转换为foobar..mp4 使用os.path模块调整路径:

newFilePath, ext = os.path.splitext(fileLoc)

在进行此操作时,您的代码中还有其他一些问题:

myFile = subprocess.Popen(ffmpegString)
print(myFile)

subprocess.Popen创建一个子流程对象,您最终将不得不wait它。 打印出来不会做任何特别有用的事情。

如果你想要做一个转换的时间,等待着每一个做了下,使用前完成check_call ,不Popen这里。

如果要并行启动它们,请在此处return myFile ,然后执行以下操作:

children = []
for x in listOfFiles:
    print('Converting ' + x + ' to .mp4')
    children.append(convertVideotoNewFormat('.mp4', x))
for child in children:
    child.wait()
print('Finished File Conversion')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM