跳至主要内容

如何得到优酷网mp4格式视频文件url


前言

众所周知,视频在网页中使用FLV格式是最常用和方便的(原因你懂得),但是当我们在移动平台上就会遇到麻烦(FLASH不被这些移动设备支持,为啥?我也不知道!)但是这样一个需求来了,在Android平台上播放YouKu的视频。
So, 我就开始研究了!

分析

经过一番研究我发现,他们其实是有另外一个格式的视频存在,但是需要成为他们的合作伙伴(也就是想用的得意思意思)。因此得出结论方法有二:
  1. 成为他们的合作伙伴(这得花点买路钱)。
  2. 条条大路通罗马,绕道走。

过程

方法一,这里就不讨论了。我们主要讲方法二(如何绕道)。
第一步:网页内嵌的播放地址 例如:http://player.youku.com/player.php/Type/Folder/Fid/18117290/Ob/1/sid/XNDQ0MDQzMTgw/v.swf
猜测可得出“XNDQ0MDQzMTgw”这一段为视频的索引也就是ID
而优酷android app请求服务器的url为:
http://api.3g.youku.com/layout/phone2_1/play?point=1&id=XNDQ0MDQzMTgw&pid=352e7f78a0bc479b&format=4&language=guoyu&audiolang=1&guid=c7a0fd9f8f19ea5cbafde16f327f8004&ver=2.3.1&operator=%E4%B8%AD%E5%9B%BD%E8%81%94%E9%80%9A_46001&network=WIFI
返回值是一个json数据如下:

{
  "status":"success",
  "lang":"",
  "title":"\u5916\u4ea4\u90e8\u56de\u5e94\u65e5\u9a7b\u534e\u5927
  \u4f7f\u5750\u8f66\u60ac\u6302\u7684\u56fd\u65d7\u88ab\u62a2\uff1a\u4e2d\u65b9\u6b63\u5728
  \u8ba4\u771f\u8c03\u67e5[\u770b\u4e1c\u65b9]",
  "weburl":"http://v.youku.com/v_show/id_XNDQ0MDQzMTgw.html",
  "videoid":"XNDQ0MDQzMTgw",
  "results":{
    "3gphd":[
      {
        "seconds":26,"url":"http://f.youku.com/player/getFlvPath/sid/134625952093737_01/st/mp4/fileid/
  0300200100503C2912CACF06F7B7BE321D4B39-09C7-D266-1DC7-72FBD749E129?
  K=74d69d0e9a11e5ab261c87ec&hd=0",
        "id":1,
        "size":959876
      }
    ],
    "3gphd_rtsp":[
      {"seconds":26,"url":"rtsp://vod1.3g.youku.com/0300200100503C2912CACF06F7B7BE321D4B3
  9-09C7-D266-1DC7-72FBD749E129.mp4","id":1,"size":959876}
    ]
  },
  "points":[],
  "totalseconds":29.3,
  "audiolang":[]
  }


根据几次测试得出结论:
results结果中的rtsp协议结果为mp4直实地址。但是悲剧的是这个协议我们用不起来!所以只能使用上面的http的url
http://f.youku.com/player/getFlvPath/sid/134625952093737_01/st/mp4/fileid/0300200100503C2912CACF06F7B7BE321D4B39-09C7-D266-1DC7-72FBD749E129?K=74d69d0e9a11e5ab261c87ec&hd=0
再请求这个url查看response的header中type为mp4数据,而且已经经过服务器根据您当前网络环境计算返回给你一个下载速度最快的url了(据猜测)。
得到json的url中参数可根据语意猜测,但是个另参数具体用途还没有揣摩清楚,不过基本没有大碍了。 以下是参数含义的猜测,不一定正确哈。
  • point 当前播放秒数
  • id 视频索引ID
  • pid 未知
  • format 可能是视频格式
  • language 未知
  • audiolang 未知
  • guid 未知
  • ver 系统版本
  • operator 移动信号运营商(如:中国联通)_46001(这个部分不理解)
  • network(当前的上网方式)

结论

把播放视频的索引id代入到api的url参数中,从服务器得到mp4视频的地址……

评论

此博客中的热门博文

How to change ViewPager scroll animation duration and velocity

When you call change current selected view pager position you may call like this. // change current position default is viewPager.setCurrentItem(position, true); viewPager . setCurrentItem ( position ); There will show transaction animation for change position , but this animation is too fast. So I want change this animation. When I see source code I find this, like flow. where is the scroll animation and speed.     int duration = 0 ;     velocity = Math . abs ( velocity );     if ( velocity > 0 ) {         duration = 4 * Math . round ( 1000 * Math . abs ( distance / velocity ));     } else {         final float pageWidth = width * mAdapter . getPageWidth ( mCurItem );         final float pageDelta = ( float ) Math . abs ( dx ) / ( pageWidth + mPageMargin );         duration = ( int ) (( pageDelta + 1 ) * 100 );     }     duration = Math . min ( duration , MAX_SETTLE_DURATION );     mScroller . startScroll ( sx , sy , dx , d

how to open context menu for android listview item button

Like title, here is this problem in my activity like this: An more button in every row of listview item . When I click this more button then show activity context menu. So I try to click button, but open context menu is the Adaper's context menu. My  Adapter  is extends from BaseAdapter ,  BaseAdapter  has not context menu. My solution like example : <ImageButton android:id="@+id/more" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:background="#00000000" android:src="@drawable/btn_addressbook_more" android:onClick="onMoreClick"/> Add an event named  onMoreClick , when user click this button will do  onClick  event in  activity . // maybe you can registe

How to use Realm database in Kotlin

Hot to use Realm in android project. Befor you use realm we should be read documentation https://realm.io/docs/java/latest#getting-started Then we got the way how to install. Install Realm as a Gradle plugin. Setp 1:   change build.gradle in project level buildscript { repositories { jcenter() } dependencies { classpath "io.realm:realm-gradle-plugin:5.14.0" } } Step 2:   Apply the realm-android plugin to the top of the application level build.gradle file. Create reaml map Object There has two way for mapping Object Make   Object   extends   ReamlObject Or make   Object   implement   ReamlModel Declear reaml mapping objects Realm not support   data class   yet. So we can't use it now. And object will make it open, because Realm object will generate subclass after   aapt   or   kapt . // 1. open class User : RealmObject() { var id: String = UUID . random UUID() . to String() var name: String = "