我在curl lib上使用curlpp来从youtube上下载视频。
我已设法下载了flv但无法使用各种视频播放器打开它。
我使用了另一个youtube视频下载器,并以完全相同的质量下载完全相同的视频,我注意到请求是相同的,文件大小几乎相同 - 这里我认为是问题,例如:MyVideo .flv - 4.55MB WorkingVideo.flv - 4.53MB
以下是代码:
// 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);
}
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);
}
任何想法?
感谢。
答案 0 :(得分:1)
由于我在Windows平台上开发,我决定下载文件,我将使用URLDownloadToFile信息:http://msdn.microsoft.com/en-us/library/ms775123%28VS.85%29.aspx
这个就像一个魅力,仍然不确定为什么curl保存文件与IE或FF或上面的win32 api函数有点不同。
答案 1 :(得分:0)
Youtube可以提供several formats上编码的视频,例如h263(flashvideo),h264等。您下载哪种格式? (this site也有很好的YouTube音频/视频格式信息)
使用工具验证文件的标题,并确保您下载的内容正常!如果您使用的是* nix,则可以在命令行上执行文件:file filename.flv
另一种可能性是您可能没有播放视频所需的编解码器。这可以解释为什么您系统上的视频播放器无法播放您下载的视频。