|
| 1 | +package com.googlecode.mp4parser; |
| 2 | + |
| 3 | +import com.coremedia.iso.boxes.Container; |
| 4 | +import com.googlecode.mp4parser.authoring.Movie; |
| 5 | +import com.googlecode.mp4parser.authoring.Track; |
| 6 | +import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder; |
| 7 | +import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator; |
| 8 | +import com.googlecode.mp4parser.authoring.tracks.CencDecryptingTrackImpl; |
| 9 | +import com.googlecode.mp4parser.authoring.tracks.CencEncryptingTrackImpl; |
| 10 | +import com.googlecode.mp4parser.authoring.tracks.CencEncyprtedTrack; |
| 11 | + |
| 12 | +import javax.crypto.SecretKey; |
| 13 | +import javax.crypto.spec.SecretKeySpec; |
| 14 | +import java.io.ByteArrayOutputStream; |
| 15 | +import java.io.FileOutputStream; |
| 16 | +import java.io.IOException; |
| 17 | +import java.nio.channels.Channels; |
| 18 | +import java.util.UUID; |
| 19 | + |
| 20 | +/** |
| 21 | + * Created by user on 22.07.2014. |
| 22 | + */ |
| 23 | +public class CencEncryptDecrypt { |
| 24 | + public static void main(String[] args) throws IOException { |
| 25 | + DefaultMp4Builder mp4Builder = new DefaultMp4Builder(); |
| 26 | + |
| 27 | + Movie mOrig = MovieCreator.build(CencEncryptDecrypt.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/1365070268951.mp4"); |
| 28 | + |
| 29 | + Movie mEncryptOut = new Movie(); |
| 30 | + SecretKey sk = new SecretKeySpec(new byte[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, "AES"); |
| 31 | + for (Track track : mOrig.getTracks()) { |
| 32 | + mEncryptOut.addTrack(new CencEncryptingTrackImpl(track, UUID.randomUUID(), sk)); |
| 33 | + } |
| 34 | + |
| 35 | + Container cEncrypted = mp4Builder.build(mEncryptOut); |
| 36 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 37 | + cEncrypted.writeContainer(Channels.newChannel(baos)); |
| 38 | + |
| 39 | + Movie mEncryptIn = MovieCreator.build (new MemoryDataSourceImpl(baos.toByteArray())); |
| 40 | + Movie mDecrypt = new Movie(); |
| 41 | + |
| 42 | + for (Track track : mEncryptIn.getTracks()) { |
| 43 | + if (track instanceof CencEncyprtedTrack) { |
| 44 | + mDecrypt.addTrack(new CencDecryptingTrackImpl((CencEncyprtedTrack) track, sk)); |
| 45 | + } else { |
| 46 | + mDecrypt.addTrack(track); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + Container cDecrypted = mp4Builder.build(mDecrypt); |
| 51 | + FileOutputStream fos = new FileOutputStream("output.mp4"); |
| 52 | + cDecrypted.writeContainer(fos.getChannel()); |
| 53 | + |
| 54 | + } |
| 55 | +} |
0 commit comments