java多媒体文件编码 处理工具类代码实例
程序员文章站
2022-04-16 19:17:51
这篇文章主要介绍了java多媒体文件编码 处理工具类使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
引入...
这篇文章主要介绍了java多媒体文件编码 处理工具类使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
引入
<dependency> <groupid>org.apache.storm</groupid> <artifactid>storm-core</artifactid> <version>1.1.0</version> </dependency> <dependency> <groupid>org.apache.storm</groupid> <artifactid>storm-kafka</artifactid> <version>1.0.4</version> </dependency> <dependency> <groupid>org.jcodec</groupid> <artifactid>jcodec</artifactid> <version>0.2.5</version> <scope>test</scope> </dependency> <dependency> <groupid>io.humble</groupid> <artifactid>humble-video-all</artifactid> <version>0.3.0</version> </dependency> <dependency> <groupid>org.mp4parser</groupid> <artifactid>isoparser</artifactid> <version>1.9.39</version> </dependency>
代码
/*** *https://*.com/questions/38123693/how-to-concatenate-merge-two-videos-in-a-new-video-file-in-java * @param aggregate 合并待输出文件 * @param segments mpeg transport stream(ffmpeg mpegts)、 mpeg-2 ts视频;波形声音 * @throws interruptedexception * @throws ioexception */ private void merge(file aggregate, collection<file> segments) throws interruptedexception, ioexception { muxer muxer = muxer.make(aggregate.tostring(), muxerformat.getformat(96), "wav"); final mediapacket packet = mediapacket.make(); long dts_offset = 0; long pts_offset = 0; for (file segment : segments) { demuxer demuxer = demuxer.make(); //option https://ffmpeg.org/ffmpeg-formats.html demuxer.open(segment.tostring(), null, false, true, null, null); if (muxer.getstate() == muxer.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(); }
测试
@test public void testformat() { muxerformat format = muxerformat.getformat(128); system.out.println(format); collection<muxerformat> formats = muxerformat.getformats(); formats.stream().foreach(this::out); } void out(muxerformat format) { system.out.printf("%s %s\n",format.getname(),format.getmimetype()); } @test public void test() throws ioexception, interruptedexception { string directory = "c:\\users\\administrator\\appdata\\local\\temp\\8ad088a2-0bb3-41dc-89d9-2c57ef8414b0"; list<file> files = files.list(paths.get(directory)).map(path -> path.tofile()).filter(file -> file.getname().endswith(".ts")) .collect(collectors.tolist()); file dest = new file(directory+"\\dest.wav"); merge(dest,files); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读