Download a Facebook Video (.Mp4) Via PHP

Download a Facebook Video (.mp4) via PHP

file_put_contents('derp.mp4', file_get_contents('http://video.ak.fbcdn.net/hvideo-ak-prn2/v/1032822_578813298845318_1606611618_n.mp4?oh=c3c6a02985213f7c47386f4653792ca6&oe=5200506F&__gda__=1375798216_02752679a44bc4b3c514bee21e000959'));

how to download mp4 file and hide url path using php

I wasnt able to get it working with any of the suggestions here, but after continuing to dig deeper, I was able to get it working.

The only issue though is that I have to save the file from my second server to my first server, for the download to work. I then delete it, but I'm pretty sure this is not efficient performance wise. However, nothing else I tried work.

The key thing(s) that got it to work is ob_start(), while (ob_get_level()) { ob_end_clean(); }

full code -

ob_start();

$userid = $_POST['userID'];
$videosid = $_POST['videosID'];

$nameOld = 'https://path/to/get/'.$userid.'/'.$videosid.'/'.$videosid.'.mp4';
$save = '/path/to/save/it/'.$userid.'/'.$videosid.'/'.$videosid.'.mp4';

$nameNew = "download.mp4";

file_put_contents($save, fopen($nameOld, 'r'));

set_time_limit(0);

header('Connection: Keep-Alive');
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=$nameNew");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($save));

while (ob_get_level()) {
ob_end_clean();
}

readfile($save);
exit;

// delete file when done
unlink($save);

If anyone can suggest a more efficient code example, please go ahead.

Download MP4 Video In Specific Folder In server With CURL

I have used this code before to download videos from same source:

function download ($url, $location){
$fp = fopen($location, "w+");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);

return 'done';
}

Decoding facebook's blob video url

This method will get the video and its audio AS SEPARATE FILES. The downloaded video will have no sound, but you will be able to download its audio file and connect it to the video in some video editing program if you need to.

  1. In Google Chrome, go to Facebook.

  2. Open the Chrome Developer Tools (F12).

  3. Go to the Network tab in the Developer Tools (it's at the top of the Developer Tools window) Network tab

  4. Play the video you would like to download on Facebook. Let it play for a few seconds and look at the Network tab during that. Long strings of numbers will appear every couple of seconds.

  5. Right click one of those strings, then go Copy>Copy link address
    Sample Image

  6. Paste (CTRL+V) the link you copied somewhere (eg Notepad) and remove "&bytestart=3684046&byteend=3862768" from the end.

    So I have a link like:

    https://scontent-waw1-1.xx.fbcdn.net/v/t66.18014-6/46772657_738781116188283_6399514850013601801_n.mp4?_nc_cat=107&efg=4yJ7ZW5jb2RlX3RhZyI6ImRhc2hfb2VwX2hxMl9mcmFnXzJfdmlkZW8ifQ%3D%3D&_nc_ht=scontent-waw1-xx&oh=91a5abdd5608768a05fd884773a45802&oe=5C29B042
    &bytestart=3684046&byteend=3862768

    After you cut the bold part out, the link that points to the full .mp4 or .webm of the video is:

    https://scontent-waw1-1.xx.fbcdn.net/v/t66.18014-6/46772657_738781116188283_6399514850013601801_n.mp4?_nc_cat=107&efg=4yJ7ZW5jb2RlX3RhZyI6ImRhc2hfb2VwX2hxMl9mcmFnXzJfdmlkZW8ifQ%3D%3D&_nc_ht=scontent-waw1-xx&oh=91a5abdd5608768a05fd884773a45802&oe=5C29B042

  7. Open the modified link, right click the video inside and select "Save video as..." Right click video to save-as

Sometimes, when you open the link you may get just the audio of the video instead of the video itself.

That's because Facebook downloads the audio and video separately. Just try repeating steps 5-7 with a different string of numbers till you get it right. Instructions to reduce the chance of accidentally picking an audio link are below.

If you need to download many videos and don't want to guess whether your link points to audio or video, type larger-than: 50k (or more) in the Filter field in the Network tab. You can also click the XHR button in Filters so it doesn't show non-video files like FB images.

Helpful filters for Chrome to show only video links

how can i force for download a video from url?

You have to be careful if you use external link in you server. Use this code only for your server file.

<?php
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename="video.mp4"');
readfile('/video/offset-17408-720.mp4');

How to download a very large video file instead of playing in browser with PHP

The easiest solution is to press Ctrl+S, select File>Save or do right click + Save as in the browser when the file starts to load - this will open the Save File dialog.

In case you want to return this file from PHP, you can do that with following script:

<?php
$file = 'video.mp4';

if (file_exists($file)) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;

}



Related Topics



Leave a reply



Submit