简体   繁体   English

ffmpeg - 如何传递http标头?

[英]ffmpeg - How to pass http headers?

I need to pass http headers (user agent and ip) to an ffmpeg command. 我需要将http标头(用户代理和IP)传递给ffmpeg命令。

I use the following 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" 

And I run a local node.js server to see the headers I get: 我运行一个本地node.js服务器来查看我得到的标头:

'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);

I keep getting an error saying "No trailing CRLF found in HTTP header." 我不断收到错误消息“在HTTP标头中找不到尾随CRLF”。 and the request is stuck. 并且请求被卡住了。

If I drop the headers - everything works normally. 如果我删除标题 - 一切正常。

I also tried putting both headers in one string, but any line breaking character I used (\\r\\n, \\r\\n, etc.) didn't work. 我也尝试将两个标题放在一个字符串中,但我使用的任何换行符(\\ r \\ n,\\ r \\ n等)都不起作用。

Can someone help me figure out how to write this command correctly with the headers included? 有人可以帮我弄清楚如何使用包含的头文件正确编写此命令吗?

Short Answer 简答

Make sure you're using the latest ffmpeg , and use the -user-agent option. 确保您使用的是最新的ffmpeg ,并使用-user-agent选项。

Longer Answer 更长的答案

For debugging, I setup a BaseHTTPSever running at 127.0.0.1:8080 with do_GET() as: 为了调试,我设置了运行在127.0.0.1:8080BaseHTTPSever ,其中do_GET()为:

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

When I do this, I see the following relevant output from ffmpeg : 当我这样做时,我看到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

So it looks like ffmpeg is setting it's own. 所以看起来ffmpeg正在设置它自己。 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-agentffmpeg ,当我用-user-agent "<foo>"替换-headers "User-Agent: <foo>"时,我确实在服务器上看到了它,同时X-Forwarded-For标题:

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

Last note. 最后一点。 There are lots of discussions around headers bugs in trac for ffmpeg . 关于ffmpeg trac中有很多关于头文件错误的讨论。 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

So, your next move might be make sure you have the latest version of ffmpeg . 因此,您的下一步行动可能是确保您拥有最新版本的ffmpeg

Well, ffmpeg manual says to split multiple http-headers by CRLF. 好吧,ffmpeg手册说要通过CRLF分割多个http-header。 The problem is that you overwrite your first "-header" argument with the second "-header" as there can be only one "-header" argument. 问题是你用第二个“-header”覆盖你的第一个“-header”参数,因为只能有一个“-header”参数。

For your example, you need to join User-Agent and X-Forwarded into one argument by valid CRLF like this: 对于您的示例,您需要通过有效的CRLF将User-AgentX-Forwarded加入到一个参数中,如下所示:

-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"$'\\r\\n'"X-Forwarded-For: 13.14.15.66"$'\\r\\n'

For set x:1 and y:2 for header ffmpeg request, use this: 对于set ffmpeg请求的set x:1y:2 ,请使用:

ffmpeg -headers $'x:1\r\ny:2\r\n' -i 'http://sample.com' -y 'sample.mp4' -v debug

Result: 结果:

[http @ 0x358be00] Setting default whitelist 'http,https,tls,rtp,tcp,udp,crypto,httpproxy'
[http @ 0x358be00] request: GET / HTTP/1.1
User-Agent: Lavf/57.76.100
Accept: */*
Range: bytes=0-
Connection: close
Host: example.com
Icy-MetaData: 1
x:1
y:2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM