您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了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://stackoverflow.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 %sn",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);
}
Java的多媒体文件编码,处理类工具解析内容就讲解到这里了,其实编程是一个开放性的答案,有不同思路或者是有其他方法可以实现地都可以在底下评论,期待你的发言。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。