I would like help with a problem. I need to check the total time of a video embedded in a website and the video time that was watched. Would anyone have given me a north to be taken? Thanks!
I would like help with a problem. I need to check the total time of a video embedded in a website and the video time that was watched. Would anyone have given me a north to be taken? Thanks!
In JavaScript use the properties:
.currentTime
: position in seconds of the video
.duration
: time in seconds total video
Note: It is not possible to get the current position of the video via PHP, PHP is server side and not client side, what you can do is send
currentTime
via a form or via Ajax
Then with document.querySelector
you can take the video, an example:
var meuVideo = document.querySelector('#meuVideo');
var pegarBtn = document.querySelector('#pegarBtn');
var playBtn = document.querySelector('#playBtn');
pegarBtn.onclick = function () {
console.log("posição atual:", meuVideo.currentTime);
console.log("duração:", meuVideo.duration);
};
playBtn.onclick = function () {
meuVideo.play();
};
<button id="playBtn">Iniciar video</button>
<button id="pegarBtn">Pegar tempo</button>
<video id="meuVideo" src="https://www.w3schools.com/html/mov_bbb.mp4">
Toconvertthesecondstoaneasierformatvisuallytounderstandyoucandividefrom60to60,forexample(exampleof Maniero in PHP):
var tempo = 1909202;
var horas = Math.floor(tempo / 3600);
var minutos = Math.floor((tempo - (horas * 3600)) / 60);
var segundos = Math.floor(tempo % 60);
if (horas < 10) horas = '0' + horas;
if (minutos < 10) minutos = '0' + minutos;
if (segundos < 10) segundos = '0' + segundos;
console.log(horas + ':' + minutos + ':' + segundos);
So in your video you could use it like this:
function humanizar_horas(tempo)
{
var horas = Math.floor(tempo / 3600);
var minutos = Math.floor((tempo - (horas * 3600)) / 60);
var segundos = Math.floor(tempo % 60);
if (horas < 10) horas = '0' + horas;
if (minutos < 10) minutos = '0' + minutos;
if (segundos < 10) segundos = '0' + segundos;
return horas + ':' + minutos + ':' + segundos;
}
var meuVideo = document.querySelector('#meuVideo');
var pegarBtn = document.querySelector('#pegarBtn');
var playBtn = document.querySelector('#playBtn');
pegarBtn.onclick = function () {
console.log("posição atual:", humanizar_horas(meuVideo.currentTime));
console.log("duração:", humanizar_horas(meuVideo.duration));
};
playBtn.onclick = function () {
meuVideo.play();
};
<button id="playBtn">Iniciar video</button>
<button id="pegarBtn">Pegar tempo</button>
<video id="meuVideo" src="https://www.w3schools.com/html/mov_bbb.mp4">
AsIalreadysaid,asPHPdirectlycannotgetthecurrenttimeofthevideo(untilwatched),butitispossibletogettheduration:
YoucantrygetID3
,downloaditat link
require_once '<pasta aonde salvou o getid3>/getid3/getid3.php';
$filename = '<pasta aonde esta o video>/video.mp4';
$getID3 = new getID3;
$file = $getID3->analyze($filename);
echo 'Duração: ', $file['playtime_string'], PHP_EOL,
'Resolução: ', $file['video']['resolution_x'], 'x', $file['video']['resolution_y'], PHP_EOL,
'Bytes: ', $file['filesize'];
Or install ffmpeg and use combined with shell_exec()
of PHP, example based on this response link :
function getDuration($path) {
$response = shell_exec('ffmpeg -i ' . escapeshellarg($path) . ' 2>&1', $output);
$parseduration = '/duration.*?([0-9]{1,})/';
if (preg_match($parseduration, $response, $matches, PREG_OFFSET_CAPTURE, 3) > 0) {
return $matches[1][0];
}
}
echo 'Duração: ', getDuration('pasta/video.flv');