在窗口上连接mpeg文件时无效语法PYTHON

问题描述:

我打算在Windows 7中将所有mpeg文件连接在一个新文件中,我调整了环境变量并从python shell运行代码,但它提供了无效的语法。任何帮助,因为我是Python和ffmpeg库的新手?在窗口上连接mpeg文件时无效语法PYTHON

我的代码:

ffmpeg -f concat -i <(for f in glob.glob("*.mpeg"); do echo "file '$PWD/$f'"; done) -c copy output.mpeg 

感谢

+2

它不是Python代码,为什么做你在Python Shell中运行它?这是Bash代码,所以在Bash控制台/终端中运行它。 – furas

+0

@furas感谢您的回复。我在ffmpeg库中发现了类似的代码,我应该在哪里运行它,你能帮我吗? – SMH

+0

你使用Linux(或Mac)吗?你知道Bash是什么吗? – furas

你的代码示例是混合或Python代码和Bash代码,因此它不能在Python Shell运行也不在Bash Shell :)

在Linux上作为Bash两个命令:
Windows可能没有printf命令

printf "file '%s'\n" *.wav > input.txt 

ffmpeg -f concat -i input.txt -c copy output.mpeg 

Python版本,这并不需要Bash

#!/usr/bin/env python3 

import os 
import sys 
import glob 
import subprocess 

# get Current Working Directory (CWD) 
pwd = os.getcwd() 

# get list of files 
if len(sys.argv) > 1: 
    #filenames = sys.argv[1:] # Linux 
    filenames = glob.glob(sys.argv[1]) # Windows 
else: 
    filenames = glob.glob("*.mpg") 

#print(filenames) 

# generate "input.txt" file 
with open("input.txt", "w") as f: 
    for name in filenames: 
     f.write("file '{}/{}'\n".format(pwd, name)) 
     #f.write("file '{}'\n".format(name)) 

# run ffmpeg 
subprocess.run('ffmpeg -f concat -i input.txt -c copy output.mpeg', shell=True) 

你可以带或不带参数即运行它。 "*.wav"

python script.py *.wav 

(仅在Linux上测试)


printf(和其他Bash命令)的Windows:GnuWin32

更多关于GnuWin32

+0

非常感谢你的回复。 但是它显示了一个错误:Traceback(最近调用最后一个): 子文件“ ”文件“C:\ Users \ SMH \ Desktop \ files \ myscript.py”,第37行。run('ffmpeg -f concat -i input.txt -c copy output.mpeg',shell = True) AttributeError:'module'对象没有属性'run' 有什么帮助吗? – SMH

+0

你用什么python版本? Python 2? – furas

+0

尝试'调用'而不是'运行' - 更多https://docs.python.org/3/library/subprocess.html#call-function-trio – furas