Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 22nd, 2012, 07:42 AM
Darwinian Darwinian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 5 Darwinian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 28 m 48 sec
Reputation Power: 0
Downloading Youtube Videos in D7

First of all when I found the following code I thought that this was the answer to all my problems..

Code:
unit uDownload;

interface

uses
  Classes,
  SysUtils,
  IDHttp,
  WinInet,
  Windows;

const
  c_YouTubeHeader = 'http://www.youtube.com/watch?v=';
  c_YouTubeHost = 'http://www.youtube.com/';

procedure TryDownloadYouTubeVideo(UrlPath: string; Dir: string);

implementation

procedure TryDownloadYouTubeVideo(UrlPath: string; Dir: string);
var
  http: TIdHttp;
  strm: TStringStream;
  FName: string;
  FUrl: string;

  function fnc_GetSourcePath: string;
  const
    c_sBeginPath = 'img.src = "http:\/\/';
    c_sMiddlePath = '\/';
    c_dMiddlePath = '/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2C';
    c_sEndPath = '"';
    c_dBeginPath = 'http://tc.';
    c_dEndPath = 'redirect_counter=1';

    c_sBreakChar1 = '%2C';
    c_sBreakChar2 = '\u0026';

    c_dBreakChar = '&';

  var
    tmpstr: string;
    tmpstrlst: TStringList;
    tmpRes: string;

    function fnc_IsTrash: Boolean;
    begin
      Result := (Pos(c_sBreakChar1, tmpstr) <> 0) and
                (Pos(c_sBreakChar2, tmpstr) <> 0) and
                (Pos(c_sBreakChar1, tmpstr) < Pos(c_sBreakChar2, tmpstr));
    end;

  begin
    Result := '';
    tmpstrlst := TStringList.Create;
    try
      tmpstr := strm.DataString;
      Delete(tmpstr, 1, Pos(c_sBeginPath, tmpstr) + Length(c_sBeginPath) - 1);
      Delete(tmpstr, Pos(c_sEndPath, tmpstr), Length(tmpstr));

      tmpstrlst.Delimiter := '.';
      tmpstrlst.DelimitedText := Copy(tmpstr, 1, Pos(c_sMiddlePath, tmpstr) - 1);

      tmpRes := Concat(c_dBeginPath,
                       tmpstrlst.Strings[3],
                       '.',
                       Copy(tmpstrlst.Strings[4], 3, Length(tmpstrlst.Strings[4])),
                       '.',
                       tmpstrlst.Strings[5],
                       '.',
                       tmpstrlst.Strings[6],
                       '.',
                       tmpstrlst.Strings[7],
                       c_dMiddlePath);

      while fnc_IsTrash do
        Delete(tmpstr, 1, Pos(c_sBreakChar1, tmpstr) + Length(c_sBreakChar1) - 1);

      tmpstrlst.Delimiter := c_dBreakChar;
      tmpstrlst.Clear;

      while Pos(c_sBreakChar2, tmpstr) <> 0 do
      begin
        tmpstrlst.Add(Copy(tmpstr, 1, Pos(c_sBreakChar2, tmpstr) - 1));
        Delete(tmpstr, 1, Pos(c_sBreakChar2, tmpstr) + Length(c_sBreakChar2) - 1);
      end;

      tmpstrlst.Add(tmpstr);
      tmpstrlst.Add(c_dEndPath);

      tmpRes := Concat(tmpRes,
                       tmpstrlst.DelimitedText);
    finally
      FreeAndNil(tmpstrlst);
    end;

    Result := tmpRes;
  end;

  function fnc_GetFileName: string;

    function fnc_GetDir: string;
    begin
      Result := Dir;
      if Result[Length(Result)] <> '\' then
        Result := Concat(Result, '\');
    end;

  const
    c_TitleBegin = '<meta name="title" content="';
    c_TitleEnd = '"';
  var
    tmpstr: string;
  begin
    tmpstr := strm.DataString;
    Delete(tmpstr, 1, Pos(c_TitleBegin, tmpstr) + Length(c_TitleBegin) - 1);
    Delete(tmpstr, Pos(c_TitleEnd, tmpstr), Length(tmpstr));
    tmpstr := UTF8ToUnicodeString(Trim(tmpstr));
    Result := Concat(fnc_GetDir, tmpstr, '.avi');
  end;

begin
  http := TIdHTTP.Create(nil);
  strm := TStringStream.Create;
  try
    http.Request.Host := c_YouTubeHost;
    http.Get(UrlPath, strm);

    FName := fnc_GetFileName;
    FUrl := fnc_GetSourcePath;

    http.Disconnect;
    strm.Clear;
    http.Get(FUrl, strm);

    strm.SaveToFile(FName);
  finally
    FreeAndNil(strm);
    FreeAndNil(http);
  end;
 end;

end.


However, it doesn't seem to work in Delphi 7 as I suspect it was written in Delphi 2010 so, can anyone get it to work in D7 or, suggest another way that my app can download youtube videos from their url and save them as a file?

For your information I am writing a media player and thought it would be pretty cool if the user could drag/copy the youtube url over to it and get it to download the clip and save it as a file on their system.

Thanks

Reply With Quote
  #2  
Old May 23rd, 2012, 02:49 AM
Darwinian Darwinian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 5 Darwinian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 28 m 48 sec
Reputation Power: 0
Or, if anyone does have delphi 2010 and/or can get it working ok, could they perhaps create a small windowless executable for me that I can pass params to, such as..

save_toutube_as.exe W4WGQmWcrbs D:\familyguy.flv

Or whatever, that would be really helpful

Reply With Quote
  #3  
Old May 23rd, 2012, 01:09 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
Perhaps your original post contained too much code without any specifics about what
does not work and/or what errors you were receiving, for anyone to dive in.

Do you know that this works in Delphi 2010 and above?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Downloading Youtube Videos in D7

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap