我有一个要求,需要从 Android 中的 HLS 流中提取元数据。我找到了两个库 FFMPEG 和 VITAMIO。考虑到android上对HLS流的支持比较零散,看了一大堆更乱的文章,最后敲定了上面两个库做进一步的研究。我还没有找到一个应用提取元数据(timed metadata)的在 Android 上完成。
我什至不知道在 Android 上是否可行。如果是这样,我应该使用哪种方法...... 帮帮我,伙计们....
最佳答案
解析 m3u8 相对容易。您需要创建 String
和 Integer
的 HashMap
来存储解析后的数据。 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/