您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)java多媒體文件編碼處理工具類的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
引入
<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(); }
測(cè)試
@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); }
關(guān)于“java多媒體文件編碼處理工具類的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。