public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); FileChannel rChannel = new RandomAccessFile("/media/scratch/CSI.S12E21.HDTV.x264-LOL.mp4", "r").getChannel(); FileChannel wChannel = new RandomAccessFile("/media/scratch/ThreeHundredFourtyThreeMB_2.mp4", "rw").getChannel(); IsoFile isoFile = new IsoFile(rChannel); HandlerBox hdlr = Path.getPath(isoFile, "/moov[0]/trak[0]/mdia[0]/hdlr[0]"); assert hdlr != null; hdlr.setName(RandomStringUtils.random(hdlr.getName().length())); isoFile.getBox(wChannel); rChannel.close(); wChannel.close(); System.err.println((System.currentTimeMillis() - start) + "ms"); new File("/media/scratch/ThreeHundredFourtyThreeMB_2.mp4").delete(); } }
public void writeMp4(String filename) throws IOException { long finalUserDataSize = 0; if (userDataBox != null) { finalUserDataSize = userDataBox.getSize(); } if (needsOffsetCorrection(isoFile)) { correctChunkOffsets(isoFile, finalUserDataSize - originalUserDataSize); } FileOutputStream videoFileOutputStream = null; try { videoFileOutputStream = new FileOutputStream(filename); isoFile.getBox(videoFileOutputStream.getChannel()); } finally { closeQuietly(isoFile); IOUtils.closeQuietly(videoFileOutputStream); } }
H264TrackImpl h264Track = new H264TrackImpl(new BufferedInputStream(some input stream here)); Movie m = new Movie(); IsoFile out = new DefaultMp4Builder().build(m); File file = new File("/sdcard/encoded.mp4"); FileOutputStream fos = new FileOutputStream(file); out.getBox(fos.getChannel()); fos.close();
H264TrackImpl h264Track = new H264TrackImpl(new FileInputStream("raw.h264").getChannel()); Movie m = new Movie(); m.addTrack(h264Track); IsoFile out = new DefaultMp4Builder().build(m); FileOutputStream fos = new FileOutputStream(new File("h264_output.mp4")); out.getBox(fos.getChannel()); fos.close();
IsoFile isoFile = new IsoFile("input.mp4"); TrackHeaderBox tkhd = (TrackHeaderBox) Path.getPath(isoFile, "/moov[0]/trak[0]/tkhd[0]"); tkhd.setMatrix(new Matrix(-1, 0, 0, 1, 0, 0, 1, 0, 0)); FileOutputStream fos = new FileOutputStream("output.mp4"); isoFile.getBox(fos.getChannel());
File fileOne = new File(mediaStorageDir.getPath() + File.separator +"theNameOfMyFirstVideo.mp4"); File fileTwo = new File(mediaStorageDir.getPath() + File.separator +"theNameOfMySecondVideo.mp4"); FileInputStream fisOne = new FileInputStream(fileOne); FileInputStream fisTwo = new FileInputStream(fileTwo); Movie video = MovieCreator.build(Channels.newChannel(fisOne)); Movie videoTwo = MovieCreator.build(Channels.newChannel(fisTwo)); List<Track> videoTracks = video.getTracks(); Track testOneVideoTrack = videoTracks.get(0); video.setTracks(new LinkedList<Track>()); List<Track> videoTwoTracks = videoTwo.getTracks(); Track testTwoVideoTrack = videoTwoTracks.get(0); video.addTrack(new AppendTrack(testTwoVideoTrack,testOneVideoTrack)); int counterFileNumber = 0; while(new File(mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4").exists()){ counterFileNumber++; } IsoFile out = new DefaultMp4Builder().build(video); String outputFileNameString = mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4"; FileOutputStream fos = new FileOutputStream(new File(String.format(outputFileNameString))); out.getBox(fos.getChannel()); fos.close();
MovieCreator mc = new MovieCreator(); Movie video = mc.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-video.mp4"))); Movie audio = mc.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-english-audio.mp4"))); List videoTracks = video.getTracks(); video.setTracks(new LinkedList()); List audioTracks = audio.getTracks(); for(Track videoTrack:videoTracks){ video.addTrack(new AppendTrack(videoTrack, videoTrack)); } for(Track audioTrack:audioTracks){ video.addTrack(new AppendTrack(audioTrack, audioTrack)); } IsoFile out = new DefaultMp4Builder().build(video); FileOutputStream fos = new FileOutputStream(new File(String.format("output.mp4"))); out.getBox(fos.getChannel()); fos.close();
MovieCreator mc = new MovieCreator(); Movie video = mc.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-video.mp4"))); Movie audio = mc.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-english-audio.mp4"))); List<Track> videoTracks = video.getTracks(); video.setTracks(new LinkedList<Track>()); List<Track> audioTracks = audio.getTracks(); for (Track videoTrack : videoTracks) { video.addTrack(new AppendTrack(videoTrack, videoTrack)); } for (Track audioTrack : audioTracks) { video.addTrack(new AppendTrack(audioTrack, audioTrack)); } IsoFile out = new DefaultMp4Builder().build(video); FileOutputStream fos = new FileOutputStream(new File(String.format("output.mp4"))); out.getBox(fos.getChannel()); fos.close();
Movie[] inMovies = new Movie[]{MovieCreator.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-deutsch-audio.mp4"))), MovieCreator.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-english-audio.mp4")))}; List<Track> videoTracks = new LinkedList<Track>(); List<Track> audioTracks = new LinkedList<Track>(); for (Movie m : inMovies) { for (Track t : m.getTracks()) { if (t.getHandler().equals("soun")) { audioTracks.add(t); } if (t.getHandler().equals("vide")) { videoTracks.add(t); } } } Movie result = new Movie(); if (audioTracks.size() > 0) { result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()]))); } if (videoTracks.size() > 0) { result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()]))); } IsoFile out = new DefaultMp4Builder().build(result); FileChannel fc = new RandomAccessFile(String.format("output.mp4"), "rw").getChannel(); fc.position(0); out.getBox(fc); fc.close();
isoFile.getBox(out.getChannel());