{"id":149567,"date":"2020-08-29T02:20:39","date_gmt":"2020-08-28T18:20:39","guid":{"rendered":"http:\/\/4563.org\/?p=149567"},"modified":"2020-08-29T02:20:39","modified_gmt":"2020-08-28T18:20:39","slug":"m3u8-%e6%96%87%e4%bb%b6%e8%a7%a3%e6%9e%90%e5%92%8c-ts-%e6%96%87%e4%bb%b6%e5%8a%a0%e8%a7%a3%e5%af%86%ef%bc%8c%e4%b8%8d%e7%9f%a5%e9%81%93%e5%af%b9%e4%b8%8d%e5%af%b9","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=149567","title":{"rendered":"M3u8 \u6587\u4ef6\u89e3\u6790\u548c TS \u6587\u4ef6\u52a0\u89e3\u5bc6\uff0c\u4e0d\u77e5\u9053\u5bf9\u4e0d\u5bf9"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  M3u8 \u6587\u4ef6\u89e3\u6790\u548c TS \u6587\u4ef6\u52a0\u89e3\u5bc6\uff0c\u4e0d\u77e5\u9053\u5bf9\u4e0d\u5bf9               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yuyujulin <\/span>  <span><i><\/i> 7<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>package com.example.demo;  import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Hex; import org.junit.jupiter.api.Test;  import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.io.*; import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.Security;  \/**  * TS \u6587\u4ef6\u52a0\u89e3\u5bc6\u3002 \u5305\u542b\u5982\u4e0b\u4e24\u5957\u52a0\u89e3\u5bc6\u65b9\u5f0f\uff1a  * 1. AES\/CBC\/PKCS7Padding \u6807\u51c6 Java \u52a0\u89e3\u5bc6\u65b9\u5f0f  * 2. AES\/CBC\/NoPadding \u52a0\u624b\u52a8 PKCS7Padding \u65b9\u5f0f\u3002\u5f53\u524d Stream \u91c7\u7528\u8fd9\u79cd\u65b9\u5f0f\u3002  * &lt;p&gt;  * AES-CBC-128 \u52a0\u5bc6  *\/ public class MediaFileCryptoUtils {     \/\/ \u7b97\u6cd5\u540d\u79f0     private static final String KEY_ALG = \"AES\";      \/**      * \u52a0\u89e3\u5bc6\u7b97\u6cd5 \/\u6a21\u5f0f \/\u586b\u5145\u65b9\u5f0f\u3002PKCS7Padding      *\/     private static final String AES_CBC_PKCS7PADDING = \"AES\/CBC\/PKCS7Padding\";      \/**      * \u52a0\u89e3\u5bc6\u7b97\u6cd5 \/\u6a21\u5f0f \/\u586b\u5145\u65b9\u5f0f\u3002      * \u8fd9\u91cc\u867d\u7136\u662f NoPadding\uff0c\u4f46\u5b9e\u9645\u6700\u540e\u4e00\u4e2a\u6570\u636e\u5757\u4f1a\u624b\u52a8\u505a PKCS7Padding      *\/     private static final String AES_CBC_NOPADDING = \"AES\/CBC\/NoPadding\";      \/**      * AES \u52a0\u5bc6\u6570\u636e\u5757\u5206\u7ec4\u957f\u5ea6\u5fc5\u987b\u4e3a 128 \u6bd4\u7279\uff08 bit \u4f4d\uff09\uff0c      * \u5bc6\u94a5\u957f\u5ea6\u53ef\u4ee5\u662f 128 \u6bd4\u7279\u3001192 \u6bd4\u7279\u3001256 \u6bd4\u7279\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\uff08\u5982\u679c\u6570\u636e\u5757\u4e0d\u8db3\u5bc6\u94a5\u957f\u5ea6\u65f6\uff0c\u4f1a\u8865\u9f50\uff09\u3002      *\/     private static final long CIPHER_BLOCK_SIZE = 16;      \/\/ \u6bcf\u6b21\u8bfb\u53d6\u7684\u7f13\u51b2\u533a\u957f\u5ea6\uff0c\u5fc5\u987b\u4e3a CIPHER_BLOCK_SIZE \u7684\u500d\u6570     private static final int BUFFER_SIZE = 1024;      \/\/ \u52a0\u5bc6\u540e\u7684 ts \u6587\u4ef6\u5757\u5927\u5c0f     private static final int TS_BLOCK_SIZE = 188;      static {         Security.addProvider(new BouncyCastleProvider());     }      private static Cipher getCipher(byte[] keyBytes, byte[] ivBytes, String transformation, int encryptMode) {         try {             Cipher cipher = Cipher.getInstance(transformation);             cipher.init(encryptMode, new SecretKeySpec(keyBytes, KEY_ALG), new IvParameterSpec(ivBytes));             return cipher;         } catch (NoSuchAlgorithmException | NoSuchPaddingException                 | InvalidAlgorithmParameterException | InvalidKeyException e) {             throw new RuntimeException(\"Error occurred while getting cipher\", e);         }     }      \/**      * \u7528\u7ed9\u5b9a\u7684 key \u548c iv \u52a0\u5bc6\u6307\u5b9a TS \u6587\u4ef6\u5e76\u5c06\u7ed3\u679c\u5199\u5165\u5230\u6307\u5b9a\u7684\u8f93\u51fa\u6d41      *      * @param keyString   \u79d8\u94a5\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982 \"362ed0938ef220d8\"      * @param ivHexString \u521d\u59cb\u5411\u91cf\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u524d\u9762\u6709 0x \u5f00\u5934\uff0c\u4f8b\u5982 \"0x04401234f48591766c1a3bc51ab173f0\"      * @param sourceTS    \u6e90 TS \u6587\u4ef6\u8def\u5f84      * @param os          \u8981\u8f93\u51fa\u5230\u7684\u6d41      *\/     public static void encryptTS(String keyString, String ivHexString, String sourceTS, OutputStream os) {         byte[] keyBytes = keyString.getBytes(StandardCharsets.UTF_8);         byte[] ivBytes = Hex.decode(ivHexString.substring(2));         encryptTS(keyBytes, ivBytes, sourceTS, os);     }      public static void encryptTsWithManualPadding(String keyString, String ivHexString, String sourceTS, OutputStream os) {         byte[] keyBytes = keyString.getBytes(StandardCharsets.UTF_8);         byte[] ivBytes = Hex.decode(ivHexString.substring(2));         encryptTsWithManualPadding(keyBytes, ivBytes, sourceTS, os);     }       \/**      * \u7528\u7ed9\u5b9a\u7684 key \u548c iv \u52a0\u5bc6\u6307\u5b9a TS \u6587\u4ef6\u5e76\u5c06\u7ed3\u679c\u5199\u5165\u5230\u6307\u5b9a\u7684\u8f93\u51fa\u6d41\u3002      * &lt;p&gt;      * AES-CBC \u5bf9\u6587\u4ef6\u52a0\u5bc6\u7684\u6807\u51c6 Java \u5199\u6cd5\u3002      *      * @param keyBytes \u79d8\u94a5      * @param ivBytes  \u521d\u59cb\u5411\u91cf      * @param sourceTS \u6e90 TS \u6587\u4ef6\u8def\u5f84      * @param os       \u8f93\u51fa\u6d41      *\/     public static void encryptTS(byte[] keyBytes, byte[] ivBytes, String sourceTS, OutputStream os) {         \/\/ \u521d\u59cb\u5316 cipher\uff0c \u540c\u4e00\u4e2a\u6587\u4ef6\u8981\u7528\u4e00\u4e2a Cipher         Cipher cipher = getCipher(keyBytes, ivBytes, AES_CBC_PKCS7PADDING, Cipher.ENCRYPT_MODE);         File plainFile = new File(sourceTS);         try (FileInputStream fis = new FileInputStream(plainFile)) {             byte[] buffer = new byte[BUFFER_SIZE];             int length = -1;             int count = 0;             while ((length = fis.read(buffer)) != -1) {                 System.out.println(\"count: \" + count++ + \", length: \" + length);                 byte[] encryptedData;                 \/\/ \u53ef\u8bfb\u5927\u5c0f\u4e3a 0\uff0c\u8868\u793a\u5f53\u524d\u5df2\u8bfb\u5230\u7684\u6570\u636e\u662f\u6700\u540e\u4e00\u5757\u6570\u636e                 if (fis.available() == 0) {                     encryptedData = cipher.doFinal(buffer, 0, length);                 } else {                     encryptedData = cipher.update(buffer, 0, length);                 }                 os.write(encryptedData);             }         } catch (IOException | BadPaddingException | IllegalBlockSizeException e) {             throw new RuntimeException(\"Error occurred while encrypting ts\", e);         }     }      \/**      * \u7528\u7ed9\u5b9a\u7684 key \u548c iv \u52a0\u5bc6\u6307\u5b9a TS \u6587\u4ef6\u5e76\u5c06\u7ed3\u679c\u5199\u5165\u5230\u6307\u5b9a\u7684\u8f93\u51fa\u6d41\u3002      * &lt;p&gt;      * Stream \u91cc\u9762 TS \u52a0\u5bc6\u7684 Java \u5b9e\u73b0\uff0c\u6240\u6709\u6570\u636e\u5757\u91c7\u7528 AES_CBC_NOPADDING\uff0c\u6700\u540e\u4e00\u4e2a\u6570\u636e\u5757\u9700\u8981\u624b\u52a8\u52a0\u4e0a PKCS7Padding \u3002      *      * @param keyBytes \u79d8\u94a5      * @param ivBytes  \u521d\u59cb\u5411\u91cf      * @param sourceTS \u6e90 TS \u6587\u4ef6\u8def\u5f84      * @param os       \u8f93\u51fa\u6d41      *\/     public static void encryptTsWithManualPadding(byte[] keyBytes, byte[] ivBytes, String sourceTS, OutputStream os) {         \/\/ \u521d\u59cb\u5316 cipher\uff0c \u540c\u4e00\u4e2a\u6587\u4ef6\u8981\u7528\u4e00\u4e2a Cipher         Cipher cipher = getCipher(keyBytes, ivBytes, AES_CBC_NOPADDING, Cipher.ENCRYPT_MODE);         File plainFile = new File(sourceTS);         try (FileInputStream fis = new FileInputStream(plainFile)) {             long totalLength = plainFile.length();             int paddingLength = (int) (CIPHER_BLOCK_SIZE - totalLength % CIPHER_BLOCK_SIZE);             byte[] buffer = new byte[BUFFER_SIZE];             int length = -1;             while ((length = fis.read(buffer)) != -1) {                 byte[] plainData = buffer;                 \/\/ \u53ef\u8bfb\u5927\u5c0f\u4e3a 0\uff0c\u8868\u793a\u5f53\u524d\u5df2\u8bfb\u5230\u7684\u6570\u636e\u662f\u6700\u540e\u4e00\u5757\u6570\u636e\uff0c \u4e14\u9700\u8981 padding                 if (fis.available() == 0 &amp;&amp; paddingLength != 0) {                     plainData = new byte[length + paddingLength];                     System.arraycopy(buffer, 0, plainData, 0, length);                     \/\/ PCKS7 \u586b\u5145\uff0c\u5728\u586b\u5145\u5b57\u8282\u4e0a\u90fd\u586b\u76f8\u540c\u7684\u6570\u636e\uff0c\u6bd4\u5982\u6570\u636e\u7f3a\u5c11 4 \u5b57\u8282\uff0c\u6240\u4ee5\u6240\u6709\u5b57\u8282\u4e0a\u90fd\u586b 4                     for (int i = length; i &lt; plainData.length; i++) {                         plainData[i] = (byte) paddingLength;                     }                 }                  \/**                  *\u8fd9\u91cc\u4e0d\u8981\u4f7f\u7528 cipher.doFinal \u56e0\u4e3a CBC \u662f\u5faa\u73af\u52a0\u5bc6\uff0c\u8981\u628a\u4e0a\u4e00\u4e2a\u52a0\u5bc6\u5feb\u7684\u7ed3\u679c\u4f5c\u4e3a\u4e0b\u4e00\u6b21\u52a0\u5bc6\u7684 iv \u3002                  * \u5373\u4f7f\u662f\u6700\u540e\u4e00\u4e2a\u6570\u636e\u5757\u4e5f\u4e0d\u9700\u8981\u4f7f\u7528 cipher.doFinal\uff0c\u56e0\u4e3a\u4e0a\u9762\u9488\u5bf9\u6700\u540e\u4e00\u4e2a\u6570\u636e\u5757\u624b\u52a8\u8fdb\u884c\u4e86 PKCS7 \u586b\u5145                  *\/                 byte[] encryptedData = cipher.update(plainData);                 os.write(encryptedData);             }         } catch (IOException e) {             throw new RuntimeException(\"Error occurred while encrypting ts with manual padding\", e);         }     }       \/**      * \u7528\u7ed9\u5b9a\u7684 key \u548c iv \u89e3\u5bc6\u6307\u5b9a TS \u6587\u4ef6\u5e76\u5c06\u7ed3\u679c\u5199\u5165\u5230\u6307\u5b9a\u7684\u8f93\u51fa\u6d41      *      * @param keyString   \u79d8\u94a5\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982 \"362ed0938ef220d8\"      * @param ivHexString \u521d\u59cb\u5411\u91cf\u7684\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u524d\u9762\u6709 0x \u5f00\u5934\uff0c\u4f8b\u5982 \"0x04401234f48591766c1a3bc51ab173f0\"      * @param sourceTS    \u6e90 TS \u6587\u4ef6\u8def\u5f84      * @param os          \u8981\u8f93\u51fa\u5230\u7684\u6d41      *\/     public static void decryptTS(String keyString, String ivHexString, String sourceTS, OutputStream os) {         byte[] keyBytes = keyString.getBytes(StandardCharsets.UTF_8);         byte[] ivBytes = Hex.decode(ivHexString.substring(2));         decryptTS(keyBytes, ivBytes, sourceTS, os);     }      public static void decryptTsWithManualPadding(String keyString, String ivHexString, String sourceTS, OutputStream os) {         byte[] keyBytes = keyString.getBytes(StandardCharsets.UTF_8);         byte[] ivBytes = Hex.decode(ivHexString.substring(2));         decryptTsWithManualPadding(keyBytes, ivBytes, sourceTS, os);     }      \/**      * \u7528\u7ed9\u5b9a\u7684 key \u548c iv \u89e3\u5bc6\u6307\u5b9a TS \u6587\u4ef6\u5e76\u5c06\u7ed3\u679c\u5199\u5165\u5230\u6307\u5b9a\u7684\u8f93\u51fa\u6d41\u3002      * &lt;p&gt;      * AES-CBC \u5bf9\u6587\u4ef6\u89e3\u5bc6\u7684\u6807\u51c6 Java \u5199\u6cd5\u3002      *      * @param keyBytes \u79d8\u94a5      * @param ivBytes  \u521d\u59cb\u5411\u91cf      * @param sourceTS \u6e90 TS \u6587\u4ef6\u8def\u5f84      * @param os       \u8f93\u51fa\u6d41      *\/     public static void decryptTS(byte[] keyBytes, byte[] ivBytes, String sourceTS, OutputStream os) {         \/\/ \u521d\u59cb\u5316 cipher\uff0c \u540c\u4e00\u4e2a\u6587\u4ef6\u8981\u7528\u4e00\u4e2a Cipher         Cipher cipher = getCipher(keyBytes, ivBytes, AES_CBC_PKCS7PADDING, Cipher.DECRYPT_MODE);         File encryptedFile = new File(sourceTS);         try (FileInputStream fis = new FileInputStream(encryptedFile)) {             byte[] buffer = new byte[BUFFER_SIZE];             int length;             while ((length = fis.read(buffer)) != -1) {                 byte[] plainData;                 if (fis.available() == 0) {                     plainData = cipher.doFinal(buffer, 0, length);                 } else {                     plainData = cipher.update(buffer, 0, length);                 }                 os.write(plainData);             }         } catch (IOException | BadPaddingException | IllegalBlockSizeException e) {             throw new RuntimeException(\"Error occurred while decrypting ts\", e);         }     }      \/**      * \u7528\u7ed9\u5b9a\u7684 key \u548c iv \u89e3\u5bc6\u6307\u5b9a TS \u6587\u4ef6\u5e76\u5c06\u7ed3\u679c\u5199\u5165\u5230\u6307\u5b9a\u7684\u8f93\u51fa\u6d41\u3002      * &lt;p&gt;      * Stream \u91cc\u9762 TS \u89e3\u5bc6\u7684 Java \u5b9e\u73b0\uff0c\u6240\u6709\u6570\u636e\u5757\u91c7\u7528 AES_CBC_NOPADDING\uff0c\u6700\u540e\u4e00\u4e2a\u6570\u636e\u5757\u9700\u8981\u624b\u52a8\u53bb\u9664 padding \u3002      *      * @param keyBytes \u79d8\u94a5      * @param ivBytes  \u521d\u59cb\u5411\u91cf      * @param sourceTS \u6e90 TS \u6587\u4ef6\u8def\u5f84      * @param os       \u8f93\u51fa\u6d41      *\/     public static void decryptTsWithManualPadding(byte[] keyBytes, byte[] ivBytes, String sourceTS, OutputStream os) {         \/\/ \u521d\u59cb\u5316 cipher\uff0c \u540c\u4e00\u4e2a\u6587\u4ef6\u8981\u7528\u4e00\u4e2a Cipher         Cipher cipher = getCipher(keyBytes, ivBytes, AES_CBC_NOPADDING, Cipher.DECRYPT_MODE);         File encryptedFile = new File(sourceTS);         try (FileInputStream fis = new FileInputStream(encryptedFile)) {             byte[] buffer = new byte[BUFFER_SIZE];             int totalLength = fis.available();             int length;             while ((length = fis.read(buffer)) != -1) {                 byte[] plainData = cipher.update(buffer);                 int plainDataLength = plainData.length; \/\/ \u9ed8\u8ba4\u4e3a\u89e3\u5bc6\u540e\u7684\u6570\u636e\u957f\u5ea6                 if (fis.available() == 0) {                     \/\/ \u6700\u540e\u4e00\u4e2a\u89e3\u5bc6\u51fa\u6765\u7684\u6570\u636e\u6570\u636e\u5757\uff0c\u8981\u53bb\u6389 Padding \u7684\u6570\u636e                     \/\/ \u8ba1\u7b97 padding \u957f\u5ea6                     int paddingLength = totalLength % TS_BLOCK_SIZE;                     \/\/ \u53bb\u6389\u65e0\u7528\u7684 padding                     plainDataLength = length - paddingLength;                 }                 os.write(plainData, 0, plainDataLength);             }         } catch (IOException e) {             throw new RuntimeException(\"Error occurred while decrypting ts with manual padding\", e);         }     }      @Test     public void testEncryptFile() {         try (FileOutputStream fos = new FileOutputStream(new File(\"D:\\196.ets\"))) {             encryptTS(\"7db4fd4359bb25b0\", \"0xb70cbefa3168efd2d0984abc8181ecff\",                     \"D:\\196.ts\", fos);         } catch (IOException e) {             e.printStackTrace();         }     }      @Test     public void testDecryptFile() {         try (FileOutputStream fos = new FileOutputStream(new File(\"D:\\9.ts\"))) {             decryptTS(\"7db4fd4359bb25b0\", \"0xb70cbefa3168efd2d0984abc8181ecff\",                     \"D:\\record-crypt\\06987ff1-0357-45b1-a6b8-f062e989c82d\\videoHD\\9.ts\", fos);         } catch (IOException e) {             e.printStackTrace();         }     } } <\/code><\/pre>\n<pre><code>package com.example.demo;  import org.junit.jupiter.api.Test; import org.springframework.util.CollectionUtils;  import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors;  public class M3u8Parser {     \/**      * m3u8 \u6587\u4ef6\u5934\u6307\u4ee4\uff1am3u8 \u6587\u4ef6\u5934\u3002\u5fc5\u987b\u5728\u6587\u4ef6\u7b2c\u4e00\u884c\u3002      *\/     private static final String DIRECTIVE_HEADER = \"#EXTM3U\";      \/**      * \u7801\u6d41\u4fe1\u606f\u6307\u4ee4\uff1a\u5e26\u5bbd\u3001\u5206\u8fa8\u7387\uff0c\u89e3\u7801\u5668\u7b49\u952e\u503c\u5bf9\u4fe1\u606f\u3002\u540e\u4e00\u884c\u8ddf\u5bf9\u5e94\u7801\u6d41\u7684 m3u8 \u6587\u4ef6\u4f4d\u7f6e\u3002      *\/     private static final String DIRECTIVE_STREAM_INF = \"#EXT-X-STREAM-INF\";      \/**      * \u97f3\u9891\uff0c\u89c6\u9891\u8f68\u9053\u4fe1\u606f\u6307\u4ee4\uff1a\u65f6\u957f\uff08\u79d2\uff09\uff0c\u6807\u9898\uff0c\u5176\u4ed6\u989d\u5916\u4fe1\u606f\uff08\u5982 logo \uff09\u4ee5\u952e\u503c\u5bf9\u663e\u793a\u3002\u540e\u4e00\u884c\u8ddf\u5bf9\u5e94 ts \u7684\u6587\u4ef6\u4f4d\u7f6e      *\/     private static final String DIRECTIVE_TRACK_INF = \"#EXTINF\";      \/**      * \u5217\u8868\u7ec8\u6b62\u6807\u8bc6\u6307\u4ee4      *\/     private static final String DIRECTIVE_ENDLIST = \"#EXT-X-ENDLIST\";      \/**      * m3u8 \u6587\u4ef6\u5305\u542b\u7684\u6700\u5c0f\u884c\u6570      *\/     private static final int M3U8_MIN_LINES = 2;      public List&lt;String&gt; getAllTsPaths(String indexM3u8) {         File indexM3u8File = new File(indexM3u8);         if (!indexM3u8File.exists()) {             throw new IllegalArgumentException(\"File not found\");         }          if (!indexM3u8File.isFile()) {             throw new IllegalArgumentException(indexM3u8File + \" is not a file\");         }         String basePath = indexM3u8File.getParentFile().getAbsolutePath();         Set&lt;String&gt; tsSet = parseIndexM3u8(basePath, indexM3u8File);         if (CollectionUtils.isEmpty(tsSet)) {             throw new IllegalArgumentException(\"No TS in specified m3u8 file\");         }         return tsSet.stream().map(tsName -&gt; basePath + File.separator + tsName).collect(Collectors.toList());     }      private Set&lt;String&gt; parseIndexM3u8(String basePath, File indexM3u8File) {         \/\/ index m3u8 \u6587\u4ef6\u6bd4\u8f83\u5c0f\uff0c\u4e00\u6b21\u6027\u8bfb\u5b8c         List&lt;String&gt; indexM3u8Lines = readAllLines(indexM3u8File);         validateM3u8(indexM3u8Lines);         for (int i = 1; i &lt; indexM3u8Lines.size(); i++) {             String line = indexM3u8Lines.get(i);             if (line.startsWith(DIRECTIVE_STREAM_INF)) {                 \/\/ \u9047\u5230\u7b2c\u4e00\u4e2a\u7801\u6d41\u4fe1\u606f\uff0c\u53d6\u7801\u6d41\u4e4b\u540e\u7684\u4e00\u884c\u5c31\u662f\u5b50 m3u8 \u6587\u4ef6\u7684\u4f4d\u7f6e\uff0c\u5f53\u524d\u7b2c\u4e00\u4e2a\u7801\u6d41\u4fe1\u606f\u5c31\u591f\u4e86                 String subM3u8 = basePath + File.separator + indexM3u8Lines.get(i + 1);                 return parseSubM3u8(subM3u8);             }         }         throw new IllegalArgumentException(\"Not a valid m3u8 file: no ts info\");     }      private Set&lt;String&gt; parseSubM3u8(String subM3u8) {         \/\/ sub m3u8 \u6587\u4ef6\u53ef\u80fd\u4f1a\u6bd4\u8f83\u5927\uff0c\u6bcf\u8bfb\u4e00\u884c\u5c31\u89e3\u6790\u4e00\u884c         try (FileReader fr = new FileReader(new File(subM3u8));              BufferedReader bf = new BufferedReader(fr)) {             Set&lt;String&gt; tracks = new LinkedHashSet&lt;&gt;();             String line;             while ((line = bf.readLine()) != null) {                 if (line.startsWith(DIRECTIVE_TRACK_INF)) {                     \/\/ \u5f53\u524d\u884c\u662f\u8f68\u9053\u4fe1\u606f\uff0c\u5c31\u518d\u8bfb\u4e00\u884c                     line = bf.readLine();                     if (line != null) {                         tracks.add(line);                     }                 }                 if (line.startsWith(DIRECTIVE_ENDLIST)) {                     break;                 }             }             return tracks;         } catch (IOException e) {             throw new IllegalArgumentException(\"Error occurred while parsing sub m3u8 file\", e);         }     }      private void validateM3u8(List&lt;String&gt; indexM3u8Lines) {         if (indexM3u8Lines.size() &lt; M3U8_MIN_LINES) {             throw new IllegalArgumentException(\"Invalid m3u8 file: insufficient lines\");         }         if (!DIRECTIVE_HEADER.equals(indexM3u8Lines.get(0))) {             throw new IllegalArgumentException(\"Invalid m3u8 file: invalid m3u8 header\");         }     }      public List&lt;String&gt; readAllLines(File file) {         List&lt;String&gt; lines = new ArrayList&lt;&gt;();         try (FileReader fr = new FileReader(file);              BufferedReader bf = new BufferedReader(fr)) {             String line;             while ((line = bf.readLine()) != null) {                 lines.add(line);             }             return lines;         } catch (IOException e) {             throw new RuntimeException(\"Error occurred while reading file\", e);         }     }      @Test     public void testM3u8Parser() {         M3u8Parser m3u8Parser = new M3u8Parser();         m3u8Parser.getAllTsPaths(\"D:\\record-crypt\\40ac6397-5116-4b44-8cb9-a2f70d8d68fa\\videoHD\\index.m3u8\").stream().forEach(System.out::println);     } }  <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>M3u8 \u6587\u4ef6\u89e3\u6790\u548c TS \u6587\u4ef6\u52a0&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149567"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=149567"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149567\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149567"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}