不少人都在找FFmpeg中是否有hls(m3u8)解析的源码,其实是有的。就是ffmpeg/libavformat/hlsproto.c,它依赖的文件也在那个目录中。
如果要是单纯想解析HLS的话,建议参考https://github.com/winlinvip/srs-bench,这是一个http、hls、rtmp集合在一起的压测工具,里面的代码更专一,代码量也不大。
下面是ffmpeg/libavformat/hlsproto.c
/* * Apple HTTP Live Streaming Protocol Handler * Copyright (c) 2010 Martin Storsjo * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** * @file * Apple HTTP Live Streaming Protocol Handler * http://tools.ietf.org/html/draft-pantos-http-live-streaming */ #include "libavutil/avstring.h" #include "libavutil/time.h" #include "avformat.h" #include "internal.h" #include "url.h" #include "version.h" /* * An apple http stream consists of a playlist with media segment files, * played sequentially. There may be several playlists with the same * video content, in different bandwidth variants, that are played in * parallel (preferably only one bandwidth variant at a time). In this case, * the user supplied the url to a main playlist that only lists the variant * playlists. * * If the main playlist doesn't point at any variants, we still create * one anonymous toplevel variant for this, to maintain the structure. */ struct segment { int duration; char url[MAX_URL_SIZE]; }; struct variant { int bandwidth; char url[MAX_URL_SIZE]; }; typedef struct HLSContext { char playlisturl[MAX_URL_SIZE]; int target_duration; int start_seq_no; int finished; int n_segments; struct segment **segments; int n_variants; struct variant **variants; int cur_seq_no; URLContext *seg_hd; int64_t last_load_time; } HLSContext; static int read_chomp_line(AVIOContext *s, char *buf, int maxlen) { int len = ff_get_line(s, buf, maxlen); while (len > 0 && av_isspace(buf[len - 1])) buf[--len] = '