How to join two video files using Python?

Here I tried to cut the first and second video files 30 seconds long from "path / connect.webm" to the lines and out1. It is working. But I need to do this to combine these two lines and write them to the file "path / final.webm". So in the end I get a 60 second video file "final.webm". But now I get the first video in 30 seconds only as an output. Please help me. Thank you very much in advance.

Code in python:

import subprocess,os fname = "/home/xincoz/test/final.webm" fp = open(fname,'wb') ffmpeg_command = ["ffmpeg", "-i", "/home/xincoz/test/connect.webm", "-acodec", "copy", "-ss", "00:00:00", "-t", "00:00:30","-f", "webm", "pipe:1"] p = subprocess.Popen(ffmpeg_command,stdout=subprocess.PIPE) out, err = p.communicate() ffmpeg_command1 = ["ffmpeg", "-i", "/home/xincoz/test/connect.webm", "-acodec", "copy", "-ss", "00:00:31", "-t", "00:00:30","-f", "webm", "pipe:1"] p1 = subprocess.Popen(ffmpeg_command1,stdout=subprocess.PIPE) out1, err1 = p1.communicate() string = out + out1 print len(out) print len(out1) print len(string) fp.write(string) fp.close() 

Please help me.

+4
source share
3 answers

This code works for me. Thank you all for your great help. Thank you very much.

 import subprocess ffmpeg_command1 = ["ffmpeg", "-i", "/home/xincoz/test/connect.webm", "-acodec", "copy", "-ss", "00:00:00", "-t", "00:00:30", "/home/xincoz/test/output1.webm"] ffmpeg_command2 = ["ffmpeg", "-i", "/home/xincoz/test/connect.webm", "-acodec", "copy", "-ss", "00:00:30", "-t", "00:00:30", "/home/xincoz/test/output2.webm"] ffmpeg_command3 = ["mencoder", "-forceidx", "-ovc", "copy", "-oac", "pcm", "-o", "/home/xincoz/test/output.webm", "/home/xincoz/test/output1.webm", "/home/xincoz/test/output2.webm"] subprocess.call(ffmpeg_command1) subprocess.call(ffmpeg_command2) subprocess.Popen(ffmpeg_command3) 
+7
source

This is similar to one of two questions that any reasonable person would ask at the first attempt to deal with the video, programmatically. "Why can't I just cut and paste the parts I need?" No one answers, because people who can really explain are tired of the question, and people like me who understand this themselves do not want to look stupid. But I do not mind - this is the practical answer.

To copy and combine complex container formats, it is always more complicated than you think, and requires at least a solution for each container.

If you read ffmpeg faq, you could theoretically combine the videos by reformatting them as mpg-v1 (maybe mpg-v2 also works) and then do more or less of what you do.

 cat first_part.mpg second_part.mpg > joined_movie.mpg 

In practice, join_movie.mpg may or may not work smoothly. Even in this very simple format, there is some data that seems to say that โ€œthis file is minuteโ€ or something like that. Thus, you can open it and open for it only 30 seconds, but find that it plays a minute (or not, depending on the player). This can be easily fixed (and I assume that it is lossless, or it is not recommended in ffmpeg faq).

But you probably don't want to work with mpg-v1, ultimately. Webm may be the smart choice. From what I collect, the web container is sourced from MKV. For audio, vorbis is used, and for video, vp8 is used. One layman to another: vp8 ~ H264 (I apologize to any of the doom9 forums that read this and have a heart attack). In any case, for us, the laity, an important point: this means that it is not only not simple, but actually super complex - even just understanding all the parameters of the encoder is a vital job.

I know that mp4box can do something pretty close to what you want with the h264 video inside the mp4 container. If you basically want to cut and join the video programmatically, you can of course just accept mp4 / h264, but you can be free and much more, and want to use webm for ideological or monetary reasons. If you find a solution in webm, I will be interested. Perhaps mkvtool would work, given its proximity to the mkv container?

I assume your files are prepared for streaming, given that you are talking about web video. Therefore, it might seem that you really really should be able to just add them together. But even with everything that alternates with a piece, it should be a little more complicated than just adding them, or even adding them, and then adjusting the title / metadata for the total playing time. I invent complexity because it seems that there are not many tools that will work, and even mp4box could not always do this reliably / accurately.

If you are using mp4, you can tell mp4box to attach files with:

 mp4box -cat file1 -cat file2 -new joined 

Perhaps a patriot with free software will publish how to cut and attach web files from the command line without transcoding.

Good luck with your project.

+5
source

I think if you specify -i infile1 .... -i infile2 .... outfile then ffmpeg will do what you need.

-1
source

Source: https://habr.com/ru/post/1488205/


All Articles