function s_to_hs(s){
var h;
h = Math.floor(s/60);
s = s%60;
s = Math.floor(s);
h += '';
s += '';
h = (h.length==1)?'0'+h:h;
s = (s.length==1)?'0'+s:s;
return h+':'+s;
}
$('input[name="row[video]"]').change(function () {
var url = $(this).val();
var duration;
var video = document.createElement("video");
video.setAttribute('crossOrigin', 'anonymous');
video.setAttribute('src', url);
video.setAttribute('width', 400);
video.setAttribute('height', 240);
video.setAttribute('preload', 'auto');
video.addEventListener("loadedmetadata", function (_event) {
duration = s_to_hs(video.duration);
$('#c-video_duration').val(duration);
});
video.addEventListener("loadeddata", function () {
setTimeout(function () {
var canvas = document.createElement("canvas"),
width = video.width,
height = video.height;
canvas.width = width;
canvas.height = height;
canvas.getContext("2d").drawImage(video, 0, 0, width, height);
var dataURL = canvas.toDataURL('image/jpeg');
$('#p-video').find('.thumbnail').find('img').attr('src', dataURL);
$('#c-video_img').val(dataURL);
}, 500);
});
});
if (file_exists(ROOT_PATH . DS . 'public' . $params['video'])) {
$mime_type = mime_content_type(ROOT_PATH . DS . 'public' . $params['video']);
if (strpos($mime_type, 'video') === 0) {
if (!empty($params['video_img']) && empty($params['image'])) {
$base64 = substr(strstr($params['video_img'],','),1);
$data = base64_decode($base64);
$path = DS . 'uploads' . DS . date('Ymd') . '_videoimg' . DS . time() . '.png';
if (!is_dir(ROOT_PATH . 'public' . DS . 'uploads' . DS . date('Ymd') . '_videoimg')) {
@mkdir(ROOT_PATH . 'public' . DS . 'uploads' . DS . date('Ymd') . '_videoimg');
}
file_put_contents(ROOT_PATH . 'public' . $path, $data);
$params['image'] = $path;
}
} else {
if (empty($params['image'])) {
$params['image'] = '/assets/img/audio.png';
}
}
}