Đây là bộ code giúp bạn lấy được video từ fb với nhiều chất lượng độ net khác nhau!
đầu tiên tạo 1 file index.html với nội dung sau đây
<!DOCTYPE html>
<html lang="en" class="h-100">
<head>
<meta charset="utf-8">
<title>Facebook Video Downloader</title>
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="asset/app.css">
</head>
<body class="d-flex flex-column h-100">
<div class="d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-white border-bottom shadow-sm">
<h5 class="my-0 mr-md-auto font-weight-normal">Facebook Video Downloader</h5>
</div>
<main role="main" class="flex-shrink-0">
<div class="container">
<div class="title px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
<h1 class="display-4">Facebook Video Downloader</h1>
</div>
<div class="input-group">
<label class="sr-only" for="link">Facebook Video URL</label>
<input type="text" class="form-control mb-2 mr-sm-2" id="link" placeholder="Facebook Video Link"
name="link">
<span class="input-group-btn">
<input type="button" name="download" id="download" value="Download!" class="btn btn-primary"
data-disable-with="Search" onclick="getDownloadLink();">
</span>
</div>
<div id="bar" style="display:none;">
<p class="text-center"><img src="https://cyber.manhtuong.net/fb/asset/ajax.gif"></p>
</div>
<div class="mt-3" id="result" style="display: none;">
<div id="downloadUrl">
<div class="row">
<div class="col-md-12">
<div class="d-flex p-2 bg-primary text-white">Title</div>
<div class="d-flex p-2 bg-dark text-white" id="title"></div>
</div>
<div class="col-md-12 mt-1">
<div class="d-flex p-2 bg-primary text-white">Source</div>
<div class="d-flex p-2 bg-dark text-white" id="source"></div>
</div>
<div class="col-md-12 mt-1">
<div class="d-flex p-2 bg-primary text-white">Download Link:</div>
<div class="p-2 bg-dark text-break" id="links"></div>
</div>
</div>
</div>
</div>
</div>
</main>
<footer class="footer mt-auto py-3">
<div class="container-fuild text-center">
<span class="text-muted">This website demo
</span>
</div>
</footer>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cyber.manhtuong.net/fb/asset/app.js?v=1"></script>
</body>
</html>
Sau đó tạo 1 thư mục app và file main.php với nội dung sau
<?php
header('Content-Type: application/json');
$msg = [];
try {
$url = $_POST['url'];
if (empty($url)) {
throw new Exception('Please prvode the URL', 1);
}
$context = [
'http' => [
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36',
],
];
$context = stream_context_create($context);
$data = file_get_contents($url, false, $context);
$msg['success'] = true;
$msg['id'] = generateId($url);
$msg['title'] = getTitle($data);
$videoData = getLinks($data);
if ($videoData) {
foreach ($videoData[1] as $key => $value) {
$msg['links'][$value] = cleanStr($videoData[2][$key]);
}
}
} catch (\Exception $e) {
$msg['success'] = false;
$msg['message'] = $e->getMessage();
}
echo json_encode($msg);
function generateId($url)
{
$id = '';
if (is_int($url)) {
$id = $url;
} elseif (preg_match('#(\d+)/?$#', $url, $matches)) {
$id = $matches[1];
}
return $id;
}
function cleanStr($str)
{
return html_entity_decode(strip_tags($str), ENT_QUOTES, 'UTF-8');
}
function getLinks($curl_content)
{
$regex = '/FBQualityLabel=\\\\"([^"]+)\\\\">\\\\x3CBaseURL>([^\\\\]+)/';
if (preg_match_all($regex, $curl_content, $output_array)) {
return $output_array;
}
}
function getTitle($curl_content)
{
$title = null;
if (preg_match('/h2 class="uiHeaderTitle"?[^>]+>(.+?)<\/h2>/', $curl_content, $matches)) {
$title = $matches[1];
} elseif (preg_match('/title id="pageTitle">(.+?)<\/title>/', $curl_content, $matches)) {
$title = $matches[1];
}
return cleanStr($title);
}
function getDescription($curl_content)
{
if (preg_match('/span class="hasCaption">(.+?)<\/span>/', $curl_content, $matches)) {
return cleanStr($matches[1]);
}
return false;
}
Tạo thư mục asset và tạo 2 file css và js với nội dung sau
app.css
html {
font-size: 14px;
}
@media (min-width: 768px) {
html {
font-size: 16px;
}
}
.container {
max-width: 960px;
}
2: app.js
const getDownloadLink = async () => {
$('#result').hide()
const vid_url = $('#link').val()
$('#download').val('Grabbing Link ...')
$('#download').attr('disabled', 'disabled')
$('#bar').show()
const formData = new FormData()
formData.append('url', vid_url)
let response = await fetch('app/main.php', {
method: 'POST',
body: formData
})
const res = await response.json()
if (res.success) {
$('#bar').hide()
$('#result').show()
$('#title').html(res.title)
$('#source').html(`<a class='text-white' href='${vid_url}'>${vid_url}</a>`)
$('#links').html('')
const links = res.links
Object.keys(links).forEach(function (key) {
$('#links').append(`<a class="btn btn-info mr-2" href="${links[key]}" role="button">${key}</a>`)
})
} else {
$('#bar').hide()
alert(res.message)
}
$('#download').val('Download!')
$('#download').removeAttr('disabled')
}
Vậy là chúng ta đã song các bạn có thể xem demo ở đây:https://cyber.manhtuong.net/fb/
Download:Facebook Video Downloader
Bài viết liên quan:
https://severphim.blogspot.com/2020/04/share-code-get-link-download-video-tu.html
Nhận xét này đã bị quản trị viên blog xóa.
Trả lờiXóa