从HLS流中提取元数据(m3u8文件)

anz*_*anz 2 android ffmpeg http-live-streaming m3u8 vitamio

我有一个要求,我需要从Android中的HLS流中提取元数据.我找到了两个库FFMPEG和VITAMIO.考虑到HLS流媒体在Android上的零碎支持,在阅读了大量更令人困惑的文章之后,我已经完成了上述两个库的进一步研究.我还没有找到一个单独的应用程序,其中提取元数据(定时元数据)已经在Android上完成.

如果在Android上甚至可能,我很困惑.如果是这样,我应该使用哪种方法......帮助我......

Nik*_*ski 7

解析m3u8相对容易.您需要创建HashMapStringInteger存储的解析数据.M3U8文件由3个条目标签组成,它们代表m3u8的条目,媒体序列和所有媒体文件的段持续时间,除了最后一个,与其余文件不同.

在每个#EXTINF整数持续时间粘贴到它之后,我们需要通过使用基本正则表达式解析字符串来获得此结果.

private HashMap<String, Integer> parseHLSMetadata(InputStream i ){

        try {
            BufferedReader r = new BufferedReader(new InputStreamReader(i, "UTF-8"));
            String line;
            HashMap<String, Integer> segmentsMap = null;
            String digitRegex = "\\d+";
            Pattern p = Pattern.compile(digitRegex);

            while((line = r.readLine())!=null){
                if(line.equals("#EXTM3U")){ //start of m3u8
                    segmentsMap = new HashMap<String, Integer>();
                }else if(line.contains("#EXTINF")){ //once found EXTINFO use runner to get the next line which contains the media file, parse duration of the segment
                    Matcher matcher = p.matcher(line);
                    matcher.find(); //find the first matching digit, which represents the duration of the segment, dont call .find() again that will throw digit which may be contained in the description.
                    segmentsMap.put(r.readLine(), Integer.parseInt(matcher.group(0)));
                }
            }
            r.close();
            return segmentsMap;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

干杯.