using ffmpeg command line with python's subprocess module

iMath redstone-cold at 163.com
Fri Dec 6 15:54:38 CET 2013


在 2013年12月4日星期三UTC+8下午6时51分49秒,Chris Angelico写道:
> On Wed, Dec 4, 2013 at 8:38 PM, Andreas Perstinger <andipersti at gmail.com> wrote:
> 
> > "fp" is a file object, but subprocess expects a list of strings as
> 
> > its first argument.
> 
> 
> 
> More fundamentally: The subprocess's arguments must include the *name*
> 
> of the file. This means you can't use TemporaryFile at all, as it's
> 
> not guaranteed to return an object that actually has a file name.
> 
> 
> 
> There's another problem, too, and that's that you're not closing the
> 
> file before expecting the subprocess to open it. And once you do that,
> 
> you'll find that the file no longer exists once it's been closed. In
> 
> fact, you'll need to research the tempfile module a bit to be able to
> 
> do what you want here; rather than spoon-feed you an exact solution,
> 
> I'll just say that there is one, and it can be found here:
> 
> 
> 
> http://docs.python.org/3.3/library/tempfile.html
> 
> 
> 
> ChrisA

I think you mean I should create a temporary file by NamedTemporaryFile(). After tried it many times, I found there is nearly no convenience in creating a temporary file or a persistent one here ,because we couldn't use the temporary file while it has not been closed ,so we couldn't depend on the convenience of letting the temporary file automatically delete itself when closing, we have to delete it later by os.remove() after it has been used in that command line.

code without the with statement is here ,but it is wrong ,it shows this line 

c:\docume~1\admini~1\locals~1\temp\tmp0d8959: Invalid data found when processing input


    fp=tempfile.NamedTemporaryFile(delete=False)
    fp.write(("file '"+fileName1+"'\n").encode('utf-8')) 
    fp.write(("file '"+fileName2+"'\n").encode('utf-8')) 
    

    subprocess.call(['ffmpeg', '-f', 'concat','-i',fp.name, '-c',  'copy', fileName])
    fp.close()



More information about the Python-list mailing list