Reputation:
I am creating a video using humble-video's RecordAndEncodeVideo.java.
I Now want to combine two videos first is the recorded video and second is the pre-stored video of same frame-rate and resolution on my system
. Due to limitation, I want to do this by using humble-video apis in java. If anyone have used it please guide me how to do it.
here is the code which i tried:
public static void combineVideo() throws InterruptedException, IOException {
final Demuxer demuxer = Demuxer.make();
demuxer.open("a.mp4", null, false, true, null, null);
final Muxer muxer = Muxer.make("b.mp4", null, null);
final MuxerFormat format = muxer.getFormat();
final MediaPacket packet = MediaPacket.make();
muxer.open(null, null);
while (demuxer.read(packet) >= 0) {
muxer.write(packet, false);
}
muxer.close();
demuxer.close();
}
Update
I want to concatenate two video files and save as a new Video File. I know this had been asked in the previous questions e.g-
But most of the answer links to the resources or libraries which are not available. Can anyone guide me to some resources. Any code snippet may be helpful.
Upvotes: 2
Views: 6291
Reputation: 1711
Any operation on video data is typically already implemented in ffmpeg and its libraries. While this is not a Java code base, it provides you with an open source reference on how to do common video data transformations, also such as concatenating video data.
You might want to take a look here to read up on what ffmpeg implements.
That being said, you might want to think about not implementing stuff that is already available in ffmpeg, proven and tested. If portability of your code base in Java across architectures is not a major issue, it might be better to call ffmpeg's libraries from within your Java code base for complex video tasks you need execute.
Upvotes: 1
Reputation: 301
I was not able to open a muxer for an existing file and append to it, but it's possible to create a new muxer and write data from multiple demuxers into it. One more thing to do is to shift packets' dts/pts so that they increment through the resulting file.
Below is a working code snippet to concatenate multiple avi into one (assuming the identical codec, timebase etc in all the source files):
private static void merge(File aggregate, Collection<File> segments) throws InterruptedException, IOException {
Muxer muxer = Muxer.make(aggregate.toString(), null, "avi");
final MediaPacket packet = MediaPacket.make();
long dts_offset = 0;
long pts_offset = 0;
for (File segment : segments) {
Demuxer demuxer = Demuxer.make();
demuxer.open(segment.toString(), null, false, true, null, null);
if (muxer.getState() == State.STATE_INITED) {
int numStreams = demuxer.getNumStreams();
for (int s = 0; s < numStreams; s++) {
DemuxerStream demuxerStream = demuxer.getStream(s);
Decoder decoder = demuxerStream.getDecoder();
muxer.addNewStream(decoder);
}
muxer.open(null, null);
}
long dts_max = 0;
long pts_max = 0;
while (demuxer.read(packet) >= 0) {
if (packet.isComplete()) {
packet.setDts(packet.getDts() + dts_offset);
packet.setPts(packet.getPts() + pts_offset);
dts_max = packet.getDts() + 1;
pts_max = packet.getPts() + 1;
muxer.write(packet, false);
}
}
dts_offset = dts_max;
pts_offset = pts_max;
demuxer.close();
}
muxer.close();
}
Upvotes: 0
Reputation: 4289
Most codecs also do not support concatenation - it will not help you to concatenate the bitstreams together.
However, if you're sure that the codecs have the same properties, then some container formats support concatenation. MPEG-TS is one of these. You could copy the video into a Transport Stream without re-encoding, and then concatenate the transport streams using normal file operations.
Upvotes: 1