0

I have a url for a m3u8 file which has the list of ts files.All those files are encrypted by AES-128 method.Along with ts files m3u8 file also contains URI for keys.

First I want to download the ts files & then decrypt them.After decryption I want to play those files.

Url for my m3u8 file is like https://example.com/myxml/myclips/250/prog_index.m3u8

My m3u8 file looks like this.

#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-VERSION:2
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:10, 
#EXT-X-KEY:METHOD=AES-128,URI="https://my.server.com/myxml/myclips/keys/250/crypt_480x272_250_0.key",IV=0x8da4a2d80b88785f7931874bf1e0914b
fileSequence0.ts
#EXTINF:10, 
fileSequence1.ts
#EXTINF:10, 
fileSequence2.ts
#EXTINF:10, 
fileSequence3.ts
#EXTINF:10, 
fileSequence4.ts
#EXTINF:10, 
fileSequence5.ts
#EXTINF:10, 
fileSequence6.ts
#EXTINF:10, 
fileSequence7.ts
#EXTINF:10, 
fileSequence8.ts
#EXTINF:10, 
fileSequence9.ts
#EXTINF:10, 
fileSequence10.ts
#EXTINF:10, 
fileSequence11.ts
#EXTINF:10, 
#EXT-X-KEY:METHOD=AES-128,URI="https://my.server.com/myxml/myclips/keys/250/crypt_480x272_250_1.key",IV=0x8e2d35559338d21f2586e79d6cd5c606
fileSequence12.ts
#EXTINF:10, 
fileSequence13.ts
#EXTINF:10, 
fileSequence14.ts
#EXTINF:10, 
fileSequence15.ts
#EXTINF:10, 
fileSequence16.ts
#EXTINF:10, 
fileSequence17.ts
#EXTINF:10, 
fileSequence18.ts
#EXTINF:10, 
fileSequence19.ts
#EXTINF:2,  
fileSequence20.ts
#EXT-X-ENDLIST

I am not getting any clue how can I do this.Please help.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Arun Badole
  • 10,977
  • 19
  • 67
  • 96
  • Well, you should know it: what have you tried yet? – rekire Jan 09 '13 at 12:26
  • I am able to download all the ts files.But as those are encrypted so I am not able to play. – Arun Badole Jan 09 '13 at 12:28
  • The data seems to be encrypted with AES-128 look for the `CipherInputStream` class. – rekire Jan 09 '13 at 12:30
  • I don't understand. The m3u8 listed above should work out of the box. There is no need for you to de-crypt. The mediaframework is able to do that automatically. – Florian Pilz Jan 09 '13 at 12:36
  • @FlorianPilz Yes I can play the m3u8 file directly in VideoView.But as per my requirement I have to extract the ts files & then have to play all of them manually.Please help.. – Arun Badole Jan 09 '13 at 12:42

2 Answers2

1

As mentioned in my comment, AES-128 decyption is done automatically on Android 3.x & 4.x devices by the mediaframework.

However, there is a mistake in your m3u8. Please reverse the order of #EXTINF and #EXT-X-KEY. #EXTINF requires in the next line to contain the TS URI.

Below a quote from the HLS draft

EXTINF is a record marker that describes the media file identified by the URI that follows it. Each media file URI MUST be preceded by an EXTINF tag.

Community
  • 1
  • 1
Florian Pilz
  • 337
  • 1
  • 12
0

I did almost the same a while ago. I wrote about it here: https://andreasvolkmann.github.io//m3u8-and-ts-segments/

Basically taking a m3u8 playlist with encrypted ts segments and turning it into one mp3 file.

However I'm not on Android. The following code worked for me (Kotlin):

fun getCipher(data: EncryptionData): Cipher {
    val bytes = URL(data.uri).readBytes()
    val chainmode = "CBC"
    val method = when (data.method) {
        EncryptionMethod.AES -> "AES/$chainmode/NoPadding"
        else -> data.method.name
    }
    val keySpec = SecretKeySpec(bytes, data.method.name)
    logger.trace("Decrypting using method ${data.method} ($method)")
    return Cipher
        .getInstance(method)
        .apply { init(Cipher.DECRYPT_MODE, keySpec, IvParameterSpec(ByteArray(16))) }
}
avolkmann
  • 2,962
  • 2
  • 19
  • 27