ffmpeg-如何传递HTTP标头? [英] ffmpeg - How to pass http headers?

查看:441
本文介绍了ffmpeg-如何传递HTTP标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将http标头(用户代理和ip)传递给ffmpeg命令.

I need to pass http headers (user agent and ip) to an ffmpeg command.

我使用以下命令:

ffmpeg  -y -timeout 5000000 -map 0:0 -an -sn -f md5 - -headers "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36" -headers "X-Forwarded-For: 13.14.15.66"  -i "http://127.0.0.1" 

然后我运行一个本地的node.js服务器来查看我得到的标头:

And I run a local node.js server to see the headers I get:

'use strict';

var express = require('express');

var server = express();

server.all('/*', function(req, res) {
  console.log(JSON.stringify(req.headers));
  res.sendFile('SampleVideo_1080x720_1mb.mp4', {root: '.'});

});


server.listen(80);

我一直收到一条错误消息:在HTTP标头中找不到尾随的CRLF."并且请求被卡住了.

I keep getting an error saying "No trailing CRLF found in HTTP header." and the request is stuck.

如果我放下标题-一切正常.

If I drop the headers - everything works normally.

我还尝试将两个标头都放在一个字符串中,但是我使用的任何换行符(\ r \ n,\ r \ n等)均无效.

I also tried putting both headers in one string, but any line breaking character I used (\r\n, \r\n, etc.) didn't work.

有人可以帮我弄清楚如何正确地使用附带的标头来编写此命令吗?

Can someone help me figure out how to write this command correctly with the headers included?

推荐答案

简短回答

确保您使用的是最新的 ffmpeg ,并使用 -user-agent 选项.

对于调试,我设置了 BaseHTTPSever ,其运行于 127.0.0.1:8080 ,其中 do_GET()为:

For debugging, I setup a BaseHTTPSever running at 127.0.0.1:8080 with do_GET() as:

def do_GET(self):
   try:
       f = open(curdir + sep + self.path, 'rb')
       self.send_response(200)
       self.end_headers()
       print("GET: "+ str(self.headers))
       self.wfile.write(f.read())
       f.close()
       return

   except IOError:
       self.send_error(404,'File Not Found: %s' % self.path)

运行后,这使我可以运行您的命令,例如:

With that running, this enabled me to run your command like:

ffmpeg  \
    -y \
    -timeout 5000000 \
    -map 0:0 \
    -an \
    -sn \
    -f md5 - \
    -headers "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36" \
    -headers "X-Forwarded-For: 13.14.15.66" \
    -i "http://127.0.0.1:8080/some_video_file.mp4" \
    -v trace

执行此操作时,我从 ffmpeg 中看到以下相关输出:

When I do this, I see the following relevant output from ffmpeg:

Reading option '-headers' ... matched as AVOption 'headers' with argument 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36'.
Reading option '-headers' ... matched as AVOption 'headers' with argument 'X-Forwarded-For: 13.14.15.66'.

在服务器上,我看到了:

On the server, I saw:

User-Agent: Lavf/56.40.101
X-Forwarded-For: 13.14.15.66

所以看起来 ffmpeg 设置了它自己的.但是,对于 ffmpeg ,有一个 -user-agent 选项,当我将 -headers"User-Agent:< foo>" 替换为 -user-agent< foo>" ,然后我也确实在服务器上看到它,以及 X-Forwarded-For 标头:

So it looks like ffmpeg is setting it's own. But there is an option -user-agent to ffmpeg, and when I replaced -headers "User-Agent: <foo>" with -user-agent "<foo>", I then did see it too on the server, alongside the X-Forwarded-For header:

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36

最后的提示.关于 ffmpeg 的trac中的标头错误,有很多讨论.我在上面观察到的(基本上是 起作用,也许只需进行少量命令更改)就是一个相当新的版本:

Last note. There are lots of discussions around headers bugs in trac for ffmpeg. What I have observed above (that essentially it is working, perhaps with a small command change) was with a fairly recent version:

ffmpeg version 2.8.1 Copyright (c) 2000-2015 the FFmpeg developers
built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04)
configuration: --enable-libx264 --enable-gpl --prefix=/usr/local --enable-shared --cc='gcc -fPIC'
libavutil      54. 31.100 / 54. 31.100
libavcodec     56. 60.100 / 56. 60.100
libavformat    56. 40.101 / 56. 40.101
libavdevice    56.  4.100 / 56.  4.100
libavfilter     5. 40.101 /  5. 40.101
libswscale      3.  1.101 /  3.  1.101
libswresample   1.  2.101 /  1.  2.101
libpostproc    53.  3.100 / 53.  3.100

因此,您的下一步可能是确保您具有最新版本的 ffmpeg .

So, your next move might be make sure you have the latest version of ffmpeg.

这篇关于ffmpeg-如何传递HTTP标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆