播放器:记录对gzip解压支持的修改

问题背景:
有些服务器,播放HLS时,m3u8文件经过gzip压缩,需要进行解压操作,否则无法播放。

方法1、使用curl下载

使用curl的话则十分简单,可以在下载之前设置参数,curl会自己进行解压。参考源文档的解释,参数enc可以设置“gzip”或者“”即可(“”则支持更多的压缩方式)。

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_ACCEPT_ENCODING, char *enc);

例子:

CURL *curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
 
  /* enable all supported built-in compressions */
  curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
 
  /* Perform the request */
  curl_easy_perform(curl);
}

Three encodings are supported: identity, meaning non-compressed, deflate which requests the server to compress its response using the zlib algorithm, and gzip which requests the gzip algorithm.
If a zero-length string is set like “”, then an Accept-Encoding: header containing all built-in supported encodings is sent.

参考:
https://curl.haxx.se/libcurl/c/CURLOPT_ACCEPT_ENCODING.html

方法2、下载后使用zlib库解压

需要注意的点:
1、gzip格式使用inflate函数进行解压,而不是uncompress函数(uncompress是给zlib格式使用的)。
2、初始化函数必须使用inflateInit2(stream,47);

例子(截取一些调用代码):

(1)初始化:
    /* 15 is the max windowBits, +32 to enable optional gzip decoding */
    if(inflateInit2(&s->b_inflate.stream, 32+15 ) != Z_OK ) {
        av_log(h, AV_LOG_ERROR, "Error during zlib initialisation: %s\n", s->b_inflate.stream.msg);
    }
    if(zlibCompileFlags() & (1<<17)) {
        av_log(h, AV_LOG_ERROR, "zlib was compiled without gzip support!\n");
    }

(2)解压:
        int ret;
        if(!s->b_inflate.p_buffer) {
            s->b_inflate.p_buffer = av_malloc(256*1024);
        }
        if(s->b_inflate.stream.avail_in == 0) {
            int i_read = http_read(h, s->b_inflate.p_buffer, 256*1024);
            if(i_read <= 0)
                return i_read;
            s->b_inflate.stream.next_in = s->b_inflate.p_buffer;
            s->b_inflate.stream.avail_in = i_read;
        }
        s->b_inflate.stream.avail_out = size;
        s->b_inflate.stream.next_out = buf;
        ret = inflate(&s->b_inflate.stream, Z_SYNC_FLUSH);

(3)结束:
    inflateEnd(&s->b_inflate.stream);
    av_free(s->b_inflate.p_buffer);

参考:
gzip格式rfc 1952 http://www.ietf.org/rfc/rfc1952.txt
deflate格式rfc 1951 http://www.ietf.org/rfc/rfc1951.txt
zlib开发库 http://www.zlib.net/manual.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当出现"gzip: stdin: not in gzip format"的错误提示时,这通常表示你在解压过程中使用了错误的解压命令。根据引用中提到的两种情况,这个错误可能有两种原因。 第一种情况是你使用的解压命令不正确。你可以使用"file"命令来查看要解压的文件的实际类型,然后使用适当的解压命令进行解压。你可以通过百度等搜索引擎查找与该文件类型对应的解压命令。 第二种情况是该压缩文件实际上是一个页面,而不是一个压缩文件。这通常发生在一些脚本自动从网上下载压缩文件的情况下。解决这个问题的方法是按照给出的网址自己从网上下载该文件,并将其复制到解压目录中。 综上所述,如果你出现了"gzip: stdin: not in gzip format"的错误提示,请先确认你使用的解压命令是否正确,并检查该文件是否实际上是一个压缩文件。根据具体情况采取相应的解决方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【linux下tar解压报错gzip: stdin: not in gzip format】](https://blog.csdn.net/boybs/article/details/129528817)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [LInux解压时:gzip: stdin: not in gzip format的错误(转载)](https://blog.csdn.net/AirJoker/article/details/85263931)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值