skip to Main Content

How to get best/max mp4 video resolution twitter api

 stdClass Object ( [bitrate] => 320000 [content_type] => video/mp4 [url] => https://video.twimg.com/ext_tw_video/4565654656538862849/pu/vid/320x180/8tkyfOGWAsk463643.mp4 ) [1] => stdClass Object ( [content_type] => application/x-mpegURL [url] => https://video.twimg.com/ext_tw_video/8234513464436343/pu/pl/UbTspZ_h6W143643zC.m3u8 ) [2] => stdClass Object ( [bitrate] => 832000 [content_type] => video/mp4 [url] => https://video.twimg.com/ext_tw_video/823451433633464364/pu/vid/640x360/051dcpN_Z9nE346.mp4 ) )

i want to getting mp4 with best resolution 640×360 (not same resolution on other posts and different array[0][1][2][…] )
how i get it?

this my code show all video variants

echo ($media->video_info->variants[0]->url .'<br>') ;


echo($media->video_info->variants[1]->url).'<br>' ;


echo ($media->video_info->variants[2]->url).'<br>';


echo ($media->video_info->variants[3]->url) .'<br>';


echo ($media->video_info->variants[4]->url) .'<br>';

2

Answers


  1. Search this question on how to download a file to your server using PHP from another web resource.

    The second task is to grab the resolution out of the URL. The URLs seem to follow a pattern:

    .../vid/<X>x<Y>/<random string of letters and numbers>.mp4
    

    With a regular expression, you can extract the X and Y resolutions from the mp4 video links:

    $regex = '/([0-9]+)x([0-9]+)/[0-9a-zA-Z]+.mp4$'
    if (preg_match($regex, $url, $matches)) { 
        $x_resolution = intval($matches[1]);
        $y_resolution = intval($matches[2]);
        $pixels = $x_resolution * $y_resolution; 
    } else {
        $pixels = 0;
    }
    

    For each of your URLs you can thus compute the resolution.
    If the URL does not contain a resolution, then you would need to download the file to find out. The code above uses the special value ‘0’ to indicate this.

    Finding out what the resolution of a particular video file is depends on its file format. As there are a massive amount of video containers, codecs, and so on, I won’t go into details here. The best solution here is to determine which formats are actually relevant to you and implement code for only those formats.

    You may want to look at the code of an open-source media player and how it determines resolution.

    To solve the simple case:

    1. Loop over each of the URLs and run the code above.
    2. Find the one with the largest $pixels value.
    3. Download this URL.
    Login or Signup to reply.
  2. This might be late, but consider the following

    var bitrate
    var vid_url
    
    foreach variant
    if variant.bitrate >= bitrate
    vid_url = variant.vid_url
    
    console.log(vid_url)
    

    now you have the highest quality

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search