0

I am trying to encode+encrypt video files at the same time in a way that can be streamed (HLS/DASH) from a NAS/cloud. The videos encryption+encoding is done on android device and the result file will be uploaded to cloud. The playback is done on other devices running Windows. No DRM needed, AES would be fine.

I have tried this code but doesn't not encrypt the video file

MediaCrypto crypto = null;
UUID CLEARKEY_SCHEME_UUID = new UUID(0x1077efecc0b24d02L, 0xace33c1e52e2fb4bL);
MediaDrm drm = new MediaDrm(CLEARKEY_SCHEME_UUID);
byte[] sessionId = drm.openSession();
crypto = new MediaCrypto(CLEARKEY_SCHEME_UUID, new byte[0]);
crypto.setMediaDrmSession(sessionId);

mEncoder.configure(format, null, crypto, MediaCodec.CONFIGURE_FLAG_ENCODE);

I have tried also this library https://github.com/zolad/VideoSlimmer but it does not have the encryption part

How do I convert + encrypt(AES encryption) video files using MediaCodec in Android in the way that can be streamed and played by ExoPlayer?

Duna
  • 1,564
  • 1
  • 16
  • 36
  • ffmpeg and MP4Box both allow simple AES encryption which may help or at least provide some context: https://stackoverflow.com/a/47442039/334402 – Mick Dec 28 '20 at 13:13
  • I am trying to avoid ffmpeg and use android SDK only, since the FFMPEG lib is 15MB, way to much in size – Duna Dec 28 '20 at 15:00
  • Are you planning to stream from Android to ExoPlayer on the same device, from one Android device to ExoPlayer on another Android device or from a server to ExoPlayer on an Android device? – Mick Dec 28 '20 at 17:42
  • @Mick yes I am streaming protected videos. I have seen that MediaCodec on Android is capable for video encoding. I succeeded this part, but using MediaCodec.CryptoInfo not. How do I encrypt videos using MediaCodec.CryptoInfo AES? FFMPEG library is way too large lib, and makes no sense to use it since Android has already embedded this functionality in newer Android OS-es 6.0+. Main problem is they are not sharing any sample project. – Duna Jan 11 '21 at 12:04
  • I think it would be worth you updating your question to explain exactly what your target is - i.e. if you are planning to stream from Android to ExoPlayer on the same device, from one Android device to ExoPlayer on another Android device or from a server to ExoPlayer on an Android device. This will help people ensure they give the right answer. – Mick Jan 11 '21 at 14:54
  • @Mick The flow is easy: encode and encrypt at the same time on Android device -> upload resulted video to cloud - > playback on Windows/Android/iOS devices as video url from cloud – Duna Jan 11 '21 at 15:44
  • Possibly related: [Encryption of video files?](https://stackoverflow.com/q/9496447/295004) and [Play encrypted hls in exoplayer using encrypted keys](https://github.com/google/ExoPlayer/issues/5362) as you seem to lack a HLS server. – Morrison Chang Jan 12 '21 at 07:09
  • @MorrisonChang The encription should be done in chunks not for the entire file, so the encrypted video can be played/seek without entire file decryption. For example live streams AES encryption – Duna Jan 12 '21 at 08:24
  • If you are encoding/encrypting on Android device, you'll have to chunk it yourself, as Android has no knowledge about [m3u8](https://en.wikipedia.org/wiki/M3U), its just a text file. Possibly useful: [Save video in every 5 second interval while video recoding is ON Android OS](https://stackoverflow.com/q/58745599/295004) – Morrison Chang Jan 12 '21 at 08:43
  • I have seen there is CryptoInfo on android that supports AES encryption built in, but there is no way to find how to configure it to make it work mEncoder.configure(format, null, crypto[here goes the object], MediaCodec.CONFIGURE_FLAG_ENCODE); https://developer.android.com/reference/android/media/MediaCodec.CryptoInfo – Duna Jan 12 '21 at 09:04
  • 1
    From https://developer.android.com/reference/android/media/MediaCodec#configure(android.media.MediaFormat,%20android.view.Surface,%20android.media.MediaCrypto,%20int) `MediaCrypto: Specify a crypto object to facilitate secure **decryption** of the media data` – Morrison Chang Jan 12 '21 at 09:16

0 Answers0