使用curl顶部的curlpp从youtube下载flv - 视频无法播放
我正在使用curl lib来从youtube下载视频。
我已经成功下载了flv,但无法使用各种视频播放器打开它。
我使用了另一个youtube视频下载器并下载了完全相同的视频完全相同的质量,我注意到请求是相同的,文件大小也几乎相同 - 我认为这就是问题所在,例如:MyVideo.flv - 4.55MBworkingVideo.flv - 4.53MB
这是代码:
<代码> // 回调函数 size_t FileCallback(FILE f, char ptr, size_t 大小, size_t nmemb) { 返回 fwrite(ptr, 大小, nmemb, f); }
void GetVideo(std::wstring videoUrl) {
FILE * file = fopen("C:\\locatin\\b.flv", "w");
if (file)
{
curlpp::Cleanup cleaner;
curlpp::Easy request;
// Set the writer callback to enable cURL to write result in a memory area
curlpp::types::WriteFunctionFunctor functor(utilspp::BindFirst(utilspp::make_functor(&FileCallback), file));
curlpp::options::WriteFunction *getFileOpt = new curlpp::options::WriteFunction(functor);
request.setOpt(getFileOpt);
// Setting the URL to retrive.
request.setOpt(new curlpp::options::Url(videoUrl));
request.setOpt(new curlpp::options::Verbose(true));
request.setOpt(new Timeout(2000));
std::list<std::string> headers;
headers.push_back("Connection: keep-alive");
headers.push_back("Keep-Alive: 115");
headers.push_back("Accept-Encoding: gzip,deflate");
headers.push_back("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
headers.push_back("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10");
headers.push_back("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
headers.push_back("Accept-Language: en-us,en;q=0.5");
request.setOpt<HttpHeader>(headers);
request.perform();
fclose(file);
}
<代码>}
有人有什么想法吗?
谢谢。
I"m using curlpp on to of curl lib in order to download videos from youtube.
I have managed to download the flv but unable to open it using all sorts of video players.
I used another youtube video downloader and download the exactly same video with the exactly same quality, I've noticed the requests are the same and also the file size is almost identical - here I think is the problem, For example: MyVideo.flv - 4.55MB WorkingVideo.flv - 4.53MB
Here is the code:
// Callback Function
size_t FileCallback(FILE f, char ptr, size_t size, size_t nmemb)
{
return fwrite(ptr, size, nmemb, f);
}
void GetVideo(std::wstring videoUrl)
{
FILE * file = fopen("C:\\locatin\\b.flv", "w");
if (file)
{
curlpp::Cleanup cleaner;
curlpp::Easy request;
// Set the writer callback to enable cURL to write result in a memory area
curlpp::types::WriteFunctionFunctor functor(utilspp::BindFirst(utilspp::make_functor(&FileCallback), file));
curlpp::options::WriteFunction *getFileOpt = new curlpp::options::WriteFunction(functor);
request.setOpt(getFileOpt);
// Setting the URL to retrive.
request.setOpt(new curlpp::options::Url(videoUrl));
request.setOpt(new curlpp::options::Verbose(true));
request.setOpt(new Timeout(2000));
std::list<std::string> headers;
headers.push_back("Connection: keep-alive");
headers.push_back("Keep-Alive: 115");
headers.push_back("Accept-Encoding: gzip,deflate");
headers.push_back("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
headers.push_back("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10");
headers.push_back("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
headers.push_back("Accept-Language: en-us,en;q=0.5");
request.setOpt<HttpHeader>(headers);
request.perform();
fclose(file);
}
}
Any ideas anyone??
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于我正在 Windows 平台上进行开发,因此我决定下载文件时使用 URLDownloadToFile 信息: http://msdn.microsoft.com/en-us/library/ms775123%28VS.85%29.aspx
这个就像一个魅力,仍然不确定为什么curl保存文件与IE或FF或上面的win32 api函数有点不同。
Since I"m developing on windows platform I've decided that for downloading the file I'll use URLDownloadToFile Info: http://msdn.microsoft.com/en-us/library/ms775123%28VS.85%29.aspx
This one works like a charm, Still not sure why curl saves the file a little bit different than IE or FF or the above win32 api function.
YouTube 可以提供以多种格式编码的视频,例如 h263 (flashvideo)、h264 等。你下载的是格式吗? (此网站也有有关 YouTube 音频/视频格式的良好信息)
使用一个验证文件头并确保您下载的内容正确的工具!如果您使用的是 *nix,您可以在命令行上执行 file:
file filename.flv
另一种可能是您可能没有播放视频所需的编解码器。这可以解释为什么您的系统上没有视频播放器能够播放您下载的视频。
Youtube can provide videos encoded on several formats like h263 (flashvideo), h264, etc. Which format are you downloading? (this site also has good information of youtube audio/video formats)
Use a tool to verify the file's header and make sure that what you downloaded is ok! If you are on *nix you can execute file on the command line:
file filename.flv
Another possibility is that you may not have the codec needed to play the video. This could explain why no video player on your system is able to play the video you downloaded.