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

标签 android ffmpeg http-live-streaming m3u8 vitamio

我有一个要求,需要从 Android 中的 HLS 流中提取元数据。我找到了两个库 FFMPEG 和 VITAMIO。考虑到android上对HLS流的支持比较零散,看了一大堆更乱的文章,最后敲定了上面两个库做进一步的研究。我还没有找到一个应用提取元数据(timed metadata)的在 Android 上完成。

我什至不知道在 Android 上是否可行。如果是这样,我应该使用哪种方法...... 帮帮我,伙计们....

最佳答案

解析 m3u8 相对容易。您需要创建 StringIntegerHashMap 来存储解析后的数据。 M3U8 文件由 3 个入口标签组成,分别代表 m3u8 的入口、媒体序列和所有媒体文件的 fragment 持续时间,除了最后一个,这与其余的不同。

在每个 #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;
    }

干杯。

关于android - 从 HLS 流中提取元数据(m3u8 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19404669/

相关文章:

java - Android:点击小部件事件

android - Inapp billing subscription error 您的订单被拒绝,因为它被认为是高风险的

安卓 : MediaPlayer getDuration() and getCurrentPosition() has NullPointerException

linux - 将 movflags 添加到 mp4 文件的顶部,而不使用 ffmpeg 进行实时 RTSP 流

swift - 如何从 swift/AVKIT 中的 HLS 流读取 id3 标签/其他元数据

javascript - 将自定义 header 添加到 videojs 播放器源

android - Lucky Patcher 会放弃它修补的应用程序并且 Google API 会验证 SHA1 吗?

python - "FFmpeg was not found, spotdl can' t continue”,即使它是使用 sudo apt-get install spotdl 安装的 + 如果从终端运行它也可以工作

video - 如何使用选定的音频流将视频转换为 hls?

go - 视频的实时复用