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

时间:2013-10-16 13:22:43

标签: android ffmpeg http-live-streaming m3u8 vitamio

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

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

2 个答案:

答案 0 :(得分:7)

解析m3u8相对容易。您需要创建HashMap StringInteger来存储已分析的数据。 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;
    }

干杯。

答案 1 :(得分:2)

定时文本元数据不会像Nikola的回答所建议的那样存储在m3u8文件中,而是存储在mpeg2 ts段中。这里有一个关于如何嵌入ts的概述:https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/HTTP_Live_Streaming_Metadata_Spec/HTTP_Live_Streaming_Metadata_Spec.pdf

您可以尝试使用ffmpeg提取元数据,该命令应该是这样的:

  

ffmpeg -i in.ts -f ffmetadata metadata.txt

您需要使用jni和libavformat执行等效操作。这并不容易,并且您仍然需要提供一种机制来向您的应用程序发送读取元数据的信号。

如果可以,我建议通过单独的机制发信号通知定时元数据。你可以提取它并把它作为你的播放器单独下载的文本文件吗?然后你按照视频播放器报告的时间线排队?实现起来要简单得多,但我不知道你的要求。

相关问题