so, hab dir mal ein kleines Beispiel gemacht.
Den Code habe ich nur kurz in 2min runtergetippert, aber er scheint zu funktionieren.
Benötigt die Indy Komponenten (IdHTTP).
Code:
uses idhttp,strutils;
function youtube_dl(const url,_file:string; var err: string):boolean;
function gettostr(const yurl:string; var src,err:string):boolean;
begin
result:=true;
with tidhttp.Create do
try
try
src:=get(yurl);
except
on e:exception do
begin
err:=e.Message;
result:=false;
end;
end;
finally
free;
end;
end;
function gettofile(const yurl,_file:string; var err:string):boolean;
var fs:tfilestream;
begin
result:=true;
try
fs:=tfilestream.Create(_file,fmcreate or fmsharedenynone);
try
with tidhttp.Create do
try
handleredirects:=true;
try
get(yurl,fs);
except
on e:exception do
begin
err:=e.Message;
result:=false;
end;
end;
finally
free;
end;
finally
fs.Free;
end;
except
on e:exception do
begin
err:=e.Message;
result:=false;
end;
end;
end;
function getvidid(const url:string; var id:string):boolean;
var p:integer;
begin
p:=pos('watch?v=',url);
result:=p>0;
if result then id:=copy(url,p+8,length(url));
end;
function getstrb(const s,left,right:string; var str:string):boolean;
var p,q:integer;
begin
result:=false;
p:=pos(left,s);
if p>0 then
begin
q:=p+length(left);
p:=posex(right,s,q);
if p>0 then
begin
str:=copy(s,q,p-q);
result:=true;
end;
end;
end;
var s,id,t:string;
begin
result:=false;
if getvidid(url,id) then
begin
if gettostr(url,s,err) then
begin
if getstrb(s,'"t": "','"',t) then
result:=gettofile(format('http://www.youtube.com/get_video?video_id=' +
'%s&t=%s&el=detailpage&ps=&fmt=34', [id,t]),_file,err)
else err:='t arg not found';
end;
end else err:='vid id not found';
end;
Möglicher Aufruf:
Code:
var err:string;
begin
//Video-URL anpassen.
if youtube_dl('http://www.youtube.com/watch?v=...','c:\vid.flv',err) then
showmessage('ok')
else showmessage('error: '+err);
end;