Youtube Question

Help
Robert
2010-12-14
2013-05-30
  • Robert
    Robert
    2010-12-14

    I'm having a problem with Youtube support in that I can't get any of my youtube favorites to show up in the media server database.  I'm running 12.1, I've enabled the support in the configuration file (including setting the favorites user), and when I go into the web interface I have the option to Refresh Youtube content, but it never actually shows up anywhere.  Do I need a specific item/container/object class/etc… to get them to show up?

     
  • John Smith
    John Smith
    2010-12-19

    I came up with a nasty hack to make it work using a php script since I couldn't figure out how to hack the c++ code, in any case if the data in fmt_url_map was parsed instead the app could pull the actual URL for the videos instead of trying to do a URL redirect and not only save a step but it probably wouldn't break the next time google tweaks things….

     
  • John Smith
    John Smith
    2010-12-24

    I seem to have managed to get something to work/compile, or at least it works for me…

    diff -ur mediatomb/src/youtube_video_url.cc mediatomb-0.12.1/src/youtube_video_url.cc
    --- mediatomb/src/youtube_video_url.cc  2010-12-24 15:40:11.917173541 +1000
    +++ mediatomb-0.12.1/src/youtube_video_url.cc   2010-12-24 15:32:38.265167072 +1000
    @@ -47,11 +47,11 @@
     #define YOUTUBE_URL_PARAMS_REGEXP   "var swfHTML.*\\;"
     #define YOUTUBE_URL_LOCATION_REGEXP "\nLocation: (http://[^\n]+)\n"
     #define YOUTUBE_URL_WATCH           "http://www.youtube.com/watch?v="
    -#define YOUTUBE_URL_GET             "http://www.youtube.com/get_video?" 
     #define YOUTUBE_URL_PARAM_VIDEO_ID  "video_id"
    -#define YOUTUBE_URL_PARAM_T_REGEXP  ".*&t=([^&]+)&"
    -#define YOUTUBE_URL_PARAM_T         "t"
    +#define YOUTUBE_URL_PARAM_FMT_REGEXP  ".*&fmt_url_map=([^&]+)&"
    +#define YOUTUBE_URL_PARAM_FMT         "fmt_url_map"
     #define YOUTUBE_IS_HD_AVAILABLE_REGEXP  "IS_HD_AVAILABLE[^:]*: *([^,]*)"
    +
     YouTubeVideoURL::YouTubeVideoURL()
     {
         curl_handle = curl_easy_init();
    @@ -62,8 +62,9 @@
         reVideoURLParams->compile(_(YOUTUBE_URL_PARAMS_REGEXP));
         redirectLocation = Ref<RExp>(new RExp());
         redirectLocation->compile(_(YOUTUBE_URL_LOCATION_REGEXP));
    -    param_t = Ref<RExp>(new RExp());
    -    param_t->compile(_(YOUTUBE_URL_PARAM_T_REGEXP));
    +
    +    FMT = Ref<RExp>(new RExp());
    +    FMT->compile(_(YOUTUBE_URL_PARAM_FMT_REGEXP));
    
         HD = Ref<RExp>(new RExp());
         HD->compile(_(YOUTUBE_IS_HD_AVAILABLE_REGEXP));
    @@ -82,7 +83,12 @@
    
     String YouTubeVideoURL::getVideoURL(String video_id, bool mp4, bool hd)
     {
    -    long retcode; 
    +    Ref<StringBuffer> buffer;
    +    Ref<Matcher> matcher;
    +
    +    long retcode;
    +    String params;
    +    String urls;
         String flv_location;
         String watch;
     #ifdef TOMBDEBUG
    @@ -91,112 +97,50 @@
         bool verbose = false;
     #endif
    
    -   /*
    -// ###########################################################
    -
    -    String swfargs = read_text_file("/home/jin/Work/UPnP/MediaTomb/YouTube/swf_args_new2.txt");
    -    
    -    Ref<Matcher> m2 = param_t->matcher(swfargs);
    -    
    -    if (m2->next())
    -    {
    -        String hmm = m2->group(1);
    -        if (string_ok(hmm))
    -            log_debug("############### t: %s\n", hmm.c_str());
    -        else 
    -            log_debug("no match?\n");
    -    }
    -    throw _Exception(_("OVER"));
    -*/
    -
    -// ###########################################################
    -
    -
         if (!string_ok(video_id))
             throw _Exception(_("No video ID specified!"));
    
         watch = _(YOUTUBE_URL_WATCH) + video_id;
    -
         Ref<URL> url(new URL(YOUTUBE_PAGESIZE));
    
    -    Ref<StringBuffer> buffer = url->download(watch, &retcode, curl_handle,
    -                                             false, verbose, true);
    -    if (retcode != 200)
    -    {
    -        throw _Exception(_("Failed to get URL for video with id ")
    -                         + watch + _("HTTP response code: ") + 
    -                         String::from(retcode));
    -    }
    -
    -    log_debug("------> GOT BUFFER %s\n", buffer->toString().c_str());
    -
    -    Ref<Matcher> matcher =  reVideoURLParams->matcher(buffer->toString());
    -    String params;
    -    if (matcher->next())
    -    {
    -//        params = trim_string(matcher->group(1));
    -        params = trim_string( matcher->group( 0 ) );
    -      /*
    -        int brace = params.index( '{' );
    -        if ( brace > 0 )
    -            params = params.substring( brace );
    -        brace = params.index( '}' );
    -        if ( brace > 0 )
    -            params = params.substring( 0, brace + 1 );
    -            */
    -        Ref<Matcher> m2 = param_t->matcher(params);
    -        if (m2->next())
    -        {
    -            String hmm = m2->group(1);
    -            if (string_ok(hmm))
    -                params = hmm; 
    -            else 
    -            {
    -                throw _Exception(_("Could not retrieve \"t\" parameter."));
    -            }
    -        }
    +    buffer = url->download(watch, &retcode, curl_handle, false, verbose, false);
    +    if(retcode != 200)
    +        throw _Exception(_("Failed to get URL for video with id ") + watch + _("HTTP response code: ") + String::from(retcode));
    +
    +    log_debug("------> REQUEST URL1: %s\n", watch.c_str());
    +    log_debug("------> GOT BUFFER1: %s\n", buffer->toString().c_str());
    +
    +    matcher = FMT->matcher(buffer->toString());
    +    if(matcher->next())
    +    {
    +   urls = url_unescape(trim_string(matcher->group(1)).c_str());
    +   log_debug("------> GOT BUFFER2: %s\n", params.c_str());
    +   return whichURL(urls, mp4, hd);
         }
    -    else
    -    {
    -        throw _Exception(_("Failed to get URL for video with id (step 1)") + video_id);
    -    }
    -
    -    params = _(YOUTUBE_URL_GET) + YOUTUBE_URL_PARAM_VIDEO_ID + '=' + 
    -             video_id + '&' + YOUTUBE_URL_PARAM_T + '=' + params;
    
    -    if (mp4)
    -    {
    -        String format = _("&fmt=18");
    -        
    -        if (hd)
    -        {
    -            matcher = HD->matcher(buffer->toString());
    -            if (matcher->next())
    -            {
    -                if (trim_string(matcher->group(1)) == "true")
    -                    format = _("&fmt=22");
    -            }
    -        }
    -                    
    -        params = params + format;
    -    }
    -
    -    buffer = url->download(params, &retcode, curl_handle, true, verbose, true);
    -
    -    matcher = redirectLocation->matcher(buffer->toString());
    -    if (matcher->next())
    -    {
    -        if (string_ok(trim_string(matcher->group(1))))
    -            return trim_string(matcher->group(1));
    -        else
    -            throw _Exception(_("Failed to get URL for video with id (step 2)")+ 
    -                             video_id);
    -    }
    +    throw _Exception(_("Could not retrieve YouTube video URL"));
    +}
    
    -    if (retcode != 303)
    +String YouTubeVideoURL::whichURL(String tmpStr, bool mp4, bool hd)
    +{
    +    while(tmpStr.find(",") > 0)
         {
    -        throw _Exception(_("Unexpected reply from YouTube: ") + 
    -                         String::from(retcode));
    +   String fmturl = tmpStr.substring(0, tmpStr.find(","));
    +   tmpStr = tmpStr.substring(tmpStr.find(",") + 1);
    +   if(fmturl.find("|") <= 0)
    +       continue;
    +
    +   int fmt = atoi(fmturl.substring(0, fmturl.find("|")).c_str());
    +   fmturl = fmturl.substring(fmturl.find("|") + 1);
    +
    +   log_debug("------> GOT BUFFER3: %d -- %s\n", fmt, fmturl.c_str());
    +
    +   if(hd && fmt == 22)
    +       return fmturl;
    +   if((hd || mp4) && fmt == 18)
    +       return fmturl;
    +   if(fmt == 5)
    +       return fmturl;
         }
    
         throw _Exception(_("Could not retrieve YouTube video URL"));
    diff -ur mediatomb/src/youtube_video_url.h mediatomb-0.12.1/src/youtube_video_url.h
    --- mediatomb/src/youtube_video_url.h   2010-12-24 15:40:11.905172760 +1000
    +++ mediatomb-0.12.1/src/youtube_video_url.h    2010-12-24 15:11:27.146641639 +1000
    @@ -57,6 +57,7 @@
         /// available
         /// \return the url to the .flv or .mp4 file 
         zmm::String getVideoURL(zmm::String video_id, bool mp4, bool hd);
    +    zmm::String whichURL(zmm::String tmpStr, bool mp4, bool hd);
    
     protected:
         // the handle *must never be used from multiple threads*
    @@ -64,7 +65,8 @@
         pthread_t pid;
         zmm::Ref<RExp> reVideoURLParams;
         zmm::Ref<RExp> redirectLocation;
    -    zmm::Ref<RExp> param_t;
    +    zmm::Ref<RExp> param_fmt;
    +    zmm::Ref<RExp> FMT;
         zmm::Ref<RExp> HD;
     };
    
     
  • John Smith
    John Smith
    2010-12-24

    Hmmmm didn't paste very well, I've dumped it to pastebin.com

     
  • jam123
    jam123
    2010-12-29

    The patch works for me, nice work.

     
  • Kyle
    Kyle
    2011-01-14

    Thanks this worked great.

     
  • konrad O
    konrad O
    2011-01-30

    Hi,

    How can I apply this patch?

     
  • jam123
    jam123
    2011-01-30

    Assuming you have the mediatomb source and it is building properly…

    In the pasted code above are two 'patch' files, created by the diff command which immediately preceeds the patches. So create a file for each patch, and paste into them everything which follows the diff command.

    Then you just need to run the patch on the souce files…
    patch original_file patch_file

     
  • konrad O
    konrad O
    2011-01-30

    Fantastic! It worked!

    Big Thanks!

     
  • John Smith
    John Smith
    2011-01-30

    Is this patch, or something like it, going to get in the mediatomb code any time soon?

    It's a hassle to have to manually patch stuff…

     

  • Anonymous
    2011-02-15

    As of today everything works except if the video clip is long, eg:
    http://www.youtube.com/watch?v=qhe0xFmGX2Q

    Any suggestions?

     
  • Kyle
    Kyle
    2011-03-03

    It seems they made a change that broke You Tube url retrievals

     

  • Anonymous
    2011-12-05

    I have the same problem, what can I do to solve them?

     

  • Anonymous
    2012-08-21

    What is the current status of the youtube fix? I see many patches around the net. I am also having the same problem with Youtube.

    It would be great if a developer or someone with inside knowledge would comment on this topic.

    Many thanks,
    Chander

     

  • Anonymous
    2012-08-21

    By the way, I using the 64-bit version:
    apt-cache policy mediatomb:
    mediatomb:
    Installed: 0.12.1-0ubuntu2