using ffmpeg command line with python's subprocess module

I

iMath

I have few wav files that I can use either of the following command line mentioned here
https://trac.ffmpeg.org/wiki/How to concatenate (join, merge) media files
to concatenate


ffmpeg -f concat -i <(for f in ./*.wav; do echo "file '$f'"; done) -c copy output.wav
ffmpeg -f concat -i <(printf "file '%s'\n" ./*.wav) -c copy output.wav
ffmpeg -f concat -i <(find . -name '*.wav' -printf "file '%p'\n") -c copy output.wav


anyone know how to convert either of them to work with python's subprocess module, it would be better if your solution is platform independent .
 
I

iMath

在 2013å¹´12月3日星期二UTC+8上åˆ5æ—¶19分21秒,Ben Finney写é“:
In bash, the <(...) notation is like piping: it executes the command
inside the parentheses and uses that as standard input to ffmpeg.



Not standard input, no. What it does is create a temporary file to

contain the result, and inserts that file name on the command line. This

is good for programs that require an actual file, not standard input.



So the above usage seems right to me: the ‘ffmpeg -i FOO’option is

provided with a filename dynamically created by Bash, referring to a

temporary file that contains the output of the subshell.



--

\ “Welchen Teil von ‘Gestalt’ verstehen Sie nicht? [What part of |

`\ ‘gestalt’ don't you understand?]†—Karsten M. Self |

_o__) |

Ben Finney

so is there any way to create a temporary file by Python here ?
 
A

Alain Ketterlin

Ben Finney said:
Not standard input, no. What it does is create a temporary file to
contain the result, and inserts that file name on the command line. This
is good for programs that require an actual file, not standard input.

Just in case (it may not be relevant to the current discussion): it may
not be a file, it will more probably be a FIFO (i.e., not seekable).
Here is the relevant part of the manual page:

| Process Substitution
| Process substitution is supported on systems that support named
| pipes (FIFOs) or the /dev/fd method of naming open files. It
| takes the form of <(list) or >(list). The process list is run
| with its input or output connected to a FIFO or some file in
| /dev/fd. The name of this file is passed as an argument to the
| current command as the result of the expansion.

-- Alain.
 
I

iMath

在 2013å¹´12月3日星期二UTC+8下åˆ5æ—¶33分09秒,Alain Ketterlin写é“:
Just in case (it may not be relevant to the current discussion): it may

not be a file, it will more probably be a FIFO (i.e., not seekable).

Here is the relevant part of the manual page:



| Process Substitution

| Process substitution is supported on systems that support named

| pipes (FIFOs) or the /dev/fd method of naming open files. It

| takes the form of <(list) or >(list). The process list is run

| with its input or output connected to a FIFO or some file in

| /dev/fd. The name of this file is passed as an argument to the

| current command as the result of the expansion.



-- Alain.

thanks for your reply, but is there any method that we can convert this to python ?
 
I

iMath

在 2013å¹´12月3日星期二UTC+8上åˆ9æ—¶42分11秒,rusi写é“:
I use the following code to do the test ,but error occurred ,it prompts system cannot find specified files ,but the files are indeed exists there ,anyhelp ?

with tempfile.TemporaryFile() as fp:
fp.write(("file '"+'a1.mp3'+"'\n").encode('utf-8'))
fp.write(("file '"+'a2.mp3'+"'\n").encode('utf-8'))

subprocess.call(['ffmpeg/ffmpeg', '-f', 'concat','-i',fp, '-c', 'copy', 'a3.mp3'])
 
I

iMath

在 2013å¹´12月3日星期二UTC+8上åˆ9æ—¶42分11秒,rusi写é“:
I use the following code to do the test ,but error occurred ,it prompts system cannot find specified files ,but the files are indeed exists there ,anyhelp ?

with tempfile.TemporaryFile() as fp:
fp.write(("file '"+'a1.mp3'+"'\n").encode('utf-8'))
fp.write(("file '"+'a2.mp3'+"'\n").encode('utf-8'))

subprocess.call(['ffmpeg/ffmpeg', '-f', 'concat','-i',fp, '-c', 'copy', 'a3.mp3'])
 
A

Andreas Perstinger

iMath said:
I use the following code to do the test ,but error occurred ,it
prompts system cannot find specified files ,but the files are indeed
exists there ,any help ?

with tempfile.TemporaryFile() as fp:
fp.write(("file '"+'a1.mp3'+"'\n").encode('utf-8'))
fp.write(("file '"+'a2.mp3'+"'\n").encode('utf-8'))

subprocess.call(['ffmpeg/ffmpeg', '-f', 'concat','-i',fp, '-c',
'copy', 'a3.mp3'])

Basic rule: Always copy'n'paste the exact traceback you get.

I don't think you are running the code you have posted because your
subprocess call doesn't work:
.... subprocess.call(["foo", "bar", fp, "baz"])
....
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python3.3/subprocess.py", line 520, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.3/subprocess.py", line 820, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.3/subprocess.py", line 1380, in _execute_child
restore_signals, start_new_session, preexec_fn)
TypeError: Can't convert '_io.BufferedRandom' object to str implicitly

"fp" is a file object, but subprocess expects a list of strings as
its first argument.

Bye, Andreas
 
C

Chris Angelico

"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

iMath

在 2013å¹´12月4日星期三UTC+8下åˆ5æ—¶38分27秒,Andreas Perstinger写é“:
iMath said:
I use the following code to do the test ,but error occurred ,it
prompts system cannot find specified files ,but the files are indeed
exists there ,any help ?

with tempfile.TemporaryFile() as fp:
fp.write(("file '"+'a1.mp3'+"'\n").encode('utf-8'))
fp.write(("file '"+'a2.mp3'+"'\n").encode('utf-8'))
subprocess.call(['ffmpeg/ffmpeg', '-f', 'concat','-i',fp, '-c',
'copy', 'a3.mp3'])



Basic rule: Always copy'n'paste the exact traceback you get.



I don't think you are running the code you have posted because your

subprocess call doesn't work:



... subprocess.call(["foo", "bar", fp, "baz"])

...

Traceback (most recent call last):

File "<stdin>", line 2, in <module>

File "/usr/lib/python3.3/subprocess.py", line 520, in call

with Popen(*popenargs, **kwargs) as p:

File "/usr/lib/python3.3/subprocess.py", line 820, in __init__

restore_signals, start_new_session)

File "/usr/lib/python3.3/subprocess.py", line 1380, in _execute_child

restore_signals, start_new_session, preexec_fn)

TypeError: Can't convert '_io.BufferedRandom' object to str implicitly



"fp" is a file object, but subprocess expects a list of strings as

its first argument.



Bye, Andreas
sorry for my fuss.
it prompts
StdErr: -c: No such file or directory
 
M

Mark Lawrence

On 06/12/2013 06:23, iMath wrote:

Dearest iMath, wouldst thou be kind enough to partake of obtaining some
type of email client that dost not sendeth double spaced data into this
most illustrious of mailing lists/newsgroups. Thanking thee for thine
participation in my most humble of requests. I do remain your most
obedient servant.
 
N

Ned Batchelder

On 06/12/2013 06:23, iMath wrote:

Dearest iMath, wouldst thou be kind enough to partake of obtaining some
type of email client that dost not sendeth double spaced data into this
most illustrious of mailing lists/newsgroups. Thanking thee for thine
participation in my most humble of requests. I do remain your most
obedient servant.

iMath seems to be a native Chinese speaker. I think this message,
though amusing, will be baffling and won't have any effect...

--Ned.
 
I

iMath

在 2013å¹´12月6日星期五UTC+8下åˆ5æ—¶23分59秒,Mark Lawrence写é“:
On 06/12/2013 06:23, iMath wrote:



Dearest iMath, wouldst thou be kind enough to partake of obtaining some

type of email client that dost not sendeth double spaced data into this

most illustrious of mailing lists/newsgroups. Thanking thee for thine

participation in my most humble of requests. I do remain your most

obedient servant.



--

My fellow Pythonistas, ask not what our language can do for you, ask

what you can do for our language.



Mark Lawrence

yes ,I am a native Chinese speaker.I always post question by Google Group not through email ,is there something wrong with it ?
your english is a little strange to me .
 
I

iMath

在 2013å¹´12月4日星期三UTC+8下åˆ6æ—¶51分49秒,Chris Angelico写é“:
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()
 
C

Chris Angelico

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

You need to close the file before getting the other process to use it.
Otherwise, it may not be able to open the file at all, and even if it
can, you might find that not all the data has been written.

But congrats! You have successfully found the points I was directing
you to. Yes, I was hinting that you need NamedTemporaryFile, the .name
attribute, and delete=False. Good job!

ChrisA
 
M

Mark Lawrence

在 2013å¹´12月6日星期五UTC+8下åˆ5æ—¶23分59秒,Mark Lawrence写é“:

yes ,I am a native Chinese speaker.I always post question by Google Group not through email ,is there something wrong with it ?
your english is a little strange to me .

You can see the extra lines inserted by google groups above. It's not
too bad in one and only one message, but when a message has been
backwards and forwards several times it's extremely irritating, or worse
still effectively unreadable. Work arounds have been posted on this
list, but I'd recommend using any decent email client.

The English I used was archaic, please ignore it :)
 
R

rusi

S

Steven D'Aprano

yes ,I am a native Chinese speaker.I always post question by Google
Group not through email ,is there something wrong with it ? your
english is a little strange to me .

Mark is writing in fake old-English style, the way people think English
was spoken a thousand years ago. I don't know why he did that. Perhaps he
thought it was amusing.

There are many problems with Google Groups. If you pay attention to this
forum, you will see dozens of posts about "Managing Google Groups
headaches" and other complaints:

- Google Groups double-spaces replies, so text which should appear like:

line one
line two
line three
line four

turns into:

line one
blank line
line two
blank line
line three
blank line
line four

- Google Groups often starts sending HTML code instead of plain text

- it often mangles indentation, which is terrible for Python code

- sometimes it automatically sets the reply address for posts to go
to Google Groups, instead of the mailing list it should go to

- almost all of the spam on his forum comes from Google Groups, so many
people automatically filter everything from Google Groups straight to
the trash.

There are alternatives to Google Groups:

- the mailing list, (e-mail address removed)

- Usenet, comp.lang.python

- the Gmane mirror:

http://gmane.org/[email protected]


and possibly others. You will maximise the number of people reading your
posts if you avoid Google Groups. If for some reason you cannot use any
of the alternatives, please take the time to fix some of the problems
with Google Groups. If you search the archives, you should find some
posts by Rusi defending Google Groups and explaining what he does to make
it more presentable, and (if I remember correctly) I think Mark also
sometimes posts a link to managing Google Groups.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top