@Test public void testEncryptDecryptFragmentedMp4() throws Exception { SecretKey sk = new SecretKeySpec(new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, "AES"); Movie m = MovieCreator.build( CencTracksImplTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/com/googlecode/mp4parser/authoring/samples/1365070268951.mp4"); List<Track> encTracks = new LinkedList<Track>(); for (Track track : m.getTracks()) { encTracks.add(new CencEncryptingTrackImpl(track, UUID.randomUUID(), sk)); } m.setTracks(encTracks); Mp4Builder mp4Builder = new FragmentedMp4Builder(); Container c = mp4Builder.build(m); ByteArrayOutputStream baos = new ByteArrayOutputStream(); c.writeContainer(Channels.newChannel(baos)); c.writeContainer(new FileOutputStream("output.mp4").getChannel()); Movie m2 = MovieCreator.build(new MemoryDataSourceImpl(baos.toByteArray())); List<Track> decTracks = new LinkedList<Track>(); for (Track track : m2.getTracks()) { decTracks.add(new CencDecryptingTrackImpl((CencEncyprtedTrack) track, sk)); } m2.setTracks(decTracks); c = mp4Builder.build(m2); c.writeContainer(new FileOutputStream("output2.mp4").getChannel()); }
public static void main(String[] args) throws IOException { Movie video = MovieCreator.build( Channels.newChannel( Mp4WithAudioDelayExample.class.getResourceAsStream("/count-video.mp4"))); Movie audio = MovieCreator.build( Channels.newChannel( Mp4WithAudioDelayExample.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) { audioTrack.getTrackMetaData().setStartTime(10.0); video.addTrack(audioTrack); } IsoFile out = new DefaultMp4Builder().build(video); FileOutputStream fos = new FileOutputStream(new File(String.format("output.mp4"))); out.getBox(fos.getChannel()); fos.close(); }
@Override void executeExample() throws IOException { String audioEnglish = EXAMPLE_ROOT + "/count-video.mp4"; Movie countVideo = MovieCreator.build(audioEnglish); TextTrackImpl subTitleEng = new TextTrackImpl(); subTitleEng.getTrackMetaData().setLanguage("eng"); subTitleEng.getSubs().add(new TextTrackImpl.Line(5000, 6000, "Five")); subTitleEng.getSubs().add(new TextTrackImpl.Line(8000, 9000, "Four")); subTitleEng.getSubs().add(new TextTrackImpl.Line(12000, 13000, "Three")); subTitleEng.getSubs().add(new TextTrackImpl.Line(16000, 17000, "Two")); subTitleEng.getSubs().add(new TextTrackImpl.Line(20000, 21000, "one")); countVideo.addTrack(subTitleEng); TextTrackImpl subTitleDeu = SrtParser.parse(new FileInputStream(new File(EXAMPLE_ROOT + "/count-subs-deutsch.srt"))); subTitleDeu.getTrackMetaData().setLanguage("deu"); countVideo.addTrack(subTitleDeu); Container out = new DefaultMp4Builder().build(countVideo); FileOutputStream fos = new FileOutputStream(new File(EXAMPLE_ROOT + "/output.mp4")); FileChannel fc = fos.getChannel(); out.writeContainer(fc); fos.close(); }
public static void main(String[] args) throws IOException { String audioEnglish = RemoveSomeSamplesExample.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/count-english-audio.mp4"; Movie originalMovie = MovieCreator.build(new FileInputStream(audioEnglish).getChannel()); Track audio = originalMovie.getTracks().get(0); Movie nuMovie = new Movie(); nuMovie.addTrack( new AppendTrack( new CroppedTrack(audio, 0, 10), new CroppedTrack(audio, 100, audio.getSamples().size()))); Container out = new DefaultMp4Builder().build(nuMovie); FileOutputStream fos = new FileOutputStream(new File("output.mp4")); out.writeContainer(fos.getChannel()); fos.close(); }
/** * all the trimming happens in this method. * * <p>STEP A ==>> For each track in the video, we first get the start time for all the tracks. * * <p>STEP B ==>> For each track in the video, we crop the tracks with a 'start time' and 'end * time' and add it to the Movie object. * * <p>STEP C ==>> Finally write the newly created movie to the disk. */ private String cropSelectedVideo() { FileInputStream fileInputStream = null; FileChannel fileChannel = null; try { File videoFile = new File(mFullPathToVideoFile); fileInputStream = new FileInputStream(videoFile); fileChannel = fileInputStream.getChannel(); Movie movie = MovieCreator.build(fileChannel); if (movie == null) { return null; } else { List<Track> tracks = movie.getTracks(); movie.setTracks(new LinkedList<Track>()); boolean timeCorrected = false; if ((tracks == null) && (tracks.size() <= 0)) { return null; } else { /** * here we try to find a track that has sync samples. Since we can only start decoding at * such a sample we should make sure that the start of the fragment is exactly the such a * frame. */ for (Track track : tracks) { if (track.getSyncSamples() != null && track.getSyncSamples().length > 0) { if (timeCorrected) { /** * This exception here can be false position in case we have multiple tracks with * sync sample at exactly the same position. E.g. a single movie containing multiple * qualities of the same video. */ } else { mStartTime = correctTimeToNextSyncSample(track, mStartTime, false); mEndTime = correctTimeToNextSyncSample(track, mEndTime, true); timeCorrected = true; } } } for (Track track : tracks) { long currentVidSample = 0; double currentTime = 0; long startVidSample = -1; long endVidSample = -1; for (int i = 0; i < track.getDecodingTimeEntries().size(); i++) { TimeToSampleBox.Entry myEntry = track.getDecodingTimeEntries().get(i); for (int j = 0; j < myEntry.getCount(); j++) { // I am trying to find the start time and end time when the trimmimg occurs if (currentTime <= mStartTime) { // our current video sample is before the starting time for the crop // if the startVidSample is equal to the length of the video, // an error happened, and we should throw an exception startVidSample = currentVidSample; // the new begining of the video will be set to this place // of the video } else if (currentTime <= mEndTime) { // our current video sample is after the starting time for the crop // but before the end time of the crop endVidSample = currentVidSample; // the new end of the video will be set to this place of the // video } else { // our current video sample is after the end time of the cropping // we just stop this this loop break; } // getDelta() : the amount of time the current video sample covers currentTime += (double) myEntry.getDelta() / (double) track.getTrackMetaData().getTimescale(); currentVidSample++; } } movie.addTrack(new CroppedTrack(track, startVidSample, endVidSample)); } } } IsoFile isoFile = new DefaultMp4Builder().build(movie); /** STEP C ==>> After we created the Movie, we have to place it into a nonvolatile memory. */ return Util.placeFileInNonVolatileDrive(isoFile, mVideFolderPath, "trim_output"); } catch (Exception e) { Log.e(TAG, "IO ERROR: " + e); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { Log.e(TAG, "IO ERROR: " + e); } } if (fileChannel != null) { try { fileChannel.close(); } catch (IOException e) { Log.e(TAG, "IO ERROR: " + e); } } } return null; }