如何获取网络视频流的快照在c / c ++中的curl在windows? [英] How to get a snapshot of network video stream with curl in c/c++ in windows?

查看:384
本文介绍了如何获取网络视频流的快照在c / c ++中的curl在windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确切的媒体类型,然后用正确的扩展名保存捕获的图像?

And exact the media type then save the captured image with correct extension?

推荐答案

浏览器通常会发送http GET 请求到要播放视频的服务器(保存视频流),这是什么开始文件传输,但这种握手可能会根据您正在协商的服务器而改变。

The browser usually sends a http GET request to the server (that holds the video stream) when it wants to play the video and that is what starts the file transfer, but this handshake may change according to the server you are negotiating with.

下面是一个简短的代码片段,显示如何设置curl并在文件的完整网址下载文件:

Below is a short code snippet that shows how to setup curl and download a file when you have the complete url of the file:

#include <curl/curl.h>
#include <stdio.h>

void get_page(const char* url, const char* file_name)
{
  CURL* easyhandle = curl_easy_init();

  curl_easy_setopt( easyhandle, CURLOPT_URL, url ) ;

  FILE* file = fopen( file_name, "w");

  curl_easy_setopt( easyhandle, CURLOPT_WRITEDATA, file) ;

  curl_easy_perform( easyhandle );

  curl_easy_cleanup( easyhandle );
}

int main()
{
  get_page( "http://blog.stackoverflow.com/wp-content/themes/zimpleza/style.css", "style.css" ) ;

  return 0;
}

像youtube这样的网站不会轻易提供视频的网址,甚至可以将您重定向到另一个html页面,您可以解析以找到装配完整的视频网址所需的魔法信息。很久以前,我写了一个小bash脚本,以自动完成查找YouTube视频网址并下载视频的过程。我知道它不工作了,所以我将它粘贴它仅用于教育目的:

Websites like youtube don't give the URL of the video so easily, and may even redirect you to another html page that you could parse to find the magic information needed to assembly the full URL of the video. I wrote a small bash script a long time ago to automate the process for finding a youtube's video URL and downloading the video. I know it doesn't work anymore so I'll paste it for education purposes only:

if [ -z "${1}" ]
then
    echo 'Error !!! Missing URL or video_id !!!'
    exit 1
fi

URL="http://www.youtube.com"

# Retrieve video_id from url passed by the user
VAR_VIDEO_ID="${1/*=}"    

# Retrive t variable located in var swfHTML (javascript)
VAR_T=$(wget -qO - $URL/watch?v=$VAR_VIDEO_ID 2>&1 | perl -e 'undef $/; <STDIN> =~ m/&t=([^&]*)&/g; print "$1\n"';)

# Assemble magical string
FLV_URL="$URL/get_video?video_id="$VAR_VIDEO_ID"&t="$VAR_T"=&eurl=&el=detailpage&ps=default&gl=US&hl=en"

# Download flv from Youtube.com. Add 2>&1 before wget cmd to suppress logs
WGET_OUTPUT=$(wget $FLV_URL -O $VAR_VIDEO_ID.flv)

# Making sure the download went okay
if [ $? -ne 0 ]
then
    # wget failed
    echo -e 1>&2 $0: "!!! ERROR: wget failed !!!\n"
    rm $VAR_VIDEO_ID.flv
    exit 1
fi

并回答您的第二个问题,我相信,要确定的文件/媒体类型,你必须下载视频流的第一个字节,因为它包含文件头,然后检查它的已知文件签名。例如, FLV 文件的第一个字节应为:

And to answer your 2nd question, I believe that to identify the file/media type you will have to download the first bytes of the video stream since it contains the file header, and then check it for known file signatures. For instance, the first bytes of a FLV file should be:

46 4C 56 01

编辑

下载视频流并不像人们所认为的那么不同。你需要告诉curl你有自己的方法来保存流数据,这可以通过指定:

Downloading a video stream is not so different as one may believe. You will need to tell curl that you have your own method to save the stream data, and this can be specified by:

curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, cb_writer);
curl_easy_setopt(curl_hndl, CURLOPT_WRITEDATA, url_data);

其中* cb_writter *是当新数据到达时被curl调用的回调。有关这些功能的详细信息,请参阅回调选项上的检查文档

Where *cb_writter* is your callback that will be called by curl when new data arrives. Check the documentation on Callback Options for more info about these functions.

如果您需要完整的示例,您可以检查此主题

If you need a full example you can check this thread.

还有一件事,如果你使用 M-JPEG streams ,您应该查看 cambozola 实施。

One more thing, if you are working with M-JPEG streams you should take a look at the cambozola implementation.

这篇关于如何获取网络视频流的快照在c / c ++中的curl在windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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