3

Here I tried to cut first and second 30sec long video file from "path/connect.flv" to the files output1.flv and output2.flv. It works. I able to concatenate these two files to form a new file "final.flv" of 60sec long. So this works and i am getting the outputs output1.flv [30sec], output2.flv[30sec] and final.flv[1min].

Here is the python code:

import subprocess

ffmpeg_command1 = ["ffmpeg", "-i", "/home/xincoz/test/connect.flv", "-acodec", "copy", "-ss", "00:00:00", "-t", "00:00:30", "/home/xincoz/test/output1.flv"]

ffmpeg_command2 = ["ffmpeg", "-i", "/home/xincoz/test/connect.flv", "-acodec", "copy", "-ss", "00:00:30", "-t", "00:00:30", "/home/xincoz/test/output2.flv"]

ffmpeg_command3 = ["mencoder", "-forceidx", "-ovc", "copy", "-oac", "pcm", "-o", "/home/xincoz/test/final.flv", "/home/xincoz/test/output1.flv", "/home/xincoz/test/output2.flv"]

subprocess.call(ffmpeg_command1)

subprocess.call(ffmpeg_command2)

subprocess.Popen(ffmpeg_command3)

But what i really want is to concatenate two strings out1 and out2 and concatinate these two to a file instead of concatenating "/home/xincoz/test/output1.flv" and "/home/xincoz/test/output2.flv". So how can i parse string out1 and out2 as the inputs to mencoder ? Please edit my code to achieve the result.

import subprocess,os

ffmpeg_command = ["ffmpeg", "-i", "/home/xincoz/test/connect.flv", "-acodec", "copy", "-ss", "00:00:00", "-t", "00:00:30","-f", "flv", "pipe:1"]

p = subprocess.Popen(ffmpeg_command,stdout=subprocess.PIPE)

out1, err = p.communicate()

ffmpeg_command1 = ["ffmpeg", "-i", "/home/xincoz/test/connect.flv", "-acodec", "copy", "-ss", "00:00:30", "-t", "00:00:30","-f", "flv", "pipe:1"]

p1 = subprocess.Popen(ffmpeg_command1,stdout=subprocess.PIPE)

out2, err1 = p1.communicate()

ffmpeg_command2 = ["mencoder", "-forceidx", "-ovc", "copy", "-oac", "pcm", "-o", "/home/xincoz/test/final.flv", out1, out2 ]

p2=subprocess.Popen(ffmpeg_command2)

Please help me. Thanks a lot in advance.

rash
  • 1,316
  • 1
  • 12
  • 17
  • could you tell us what the contents of out1 and out2 are? – Tom P Jun 26 '13 at 10:42
  • Thanks for the reply @TomP. Its the binary data in output1.flv and output2.flv (ie, first and second 30sec binary data of the connect.flv). Here we are not creating output1.flv and output2.flv, instead we are taking the binary data to the strings out1 and out2. – rash Jun 26 '13 at 10:50
  • I believe Tom P was asking what the video and audio formats are since the format type can determine how to proceed with joining. It's fairly obvious that it is "binary data". – llogan Jun 26 '13 at 18:27
  • Sorry @LordNeckbeard. I am new to this. Here is the format: Video format- H264-MPEG-4 AVC and Audio format -MPEG AAC Audio. – rash Jun 27 '13 at 03:34
  • 1
    `-forceidx` option might require a real file i.e., piping data via stdin, named pipe might not work. You could test it anyway. [Here's an example that shows how to pass data using stdin, named pipe, NamedTemporaryFile](http://stackoverflow.com/a/15343686/4279). A temporary file should work (it is essentially the same as your first code example except filenames won't conflict when you start several scripts in parallel). – jfs Jun 27 '13 at 12:34
  • Thanks Thanks Thanks a lot for your reply sir @J.F.Sebastian. Really great you are. (y) – rash Jun 27 '13 at 12:44

1 Answers1

0

You haven't said how your code fails, but it looks like you are trying to give MEncoder binary strings as command line parameters. http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-selecting-input.html says that you can give the filename in the command line, not the contents of a file.

so to answer your question:

with open('merged.flv', 'wb') as f:
    f.write(out1 + out2)

ffmpeg_command2 = ["mencoder", "your", "other", "args", "merged.flv"]
Scruffy
  • 908
  • 1
  • 8
  • 21