2018-03-10 22:06:42 +01:00
|
|
|
/*
|
2018-07-24 03:11:56 +02:00
|
|
|
This file is part of FreeTube.
|
2018-03-10 22:06:42 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
FreeTube is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2018-03-10 22:06:42 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
FreeTube is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2018-03-10 22:06:42 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FreeTube. If not, see <http://www.gnu.org/licenses/>.
|
2018-03-10 22:06:42 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2018-03-12 04:06:30 +01:00
|
|
|
* File for functions related to videos.
|
|
|
|
*/
|
2018-03-10 22:06:42 +01:00
|
|
|
|
|
|
|
/**
|
2018-03-12 04:06:30 +01:00
|
|
|
* Display the video player and play a video
|
|
|
|
*
|
|
|
|
* @param {string} videoId - The video ID of the video to be played.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2018-07-25 04:44:09 +02:00
|
|
|
function playVideo(videoId) {
|
2018-09-19 17:01:21 +02:00
|
|
|
hideViews();
|
2018-04-01 00:14:07 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
playerView.playerSeen = true;
|
|
|
|
playerView.videoId = videoId;
|
|
|
|
playerView.video480p = undefined;
|
|
|
|
playerView.video720p = undefined;
|
|
|
|
playerView.embededHtml = "<iframe width='560' height='315' src='https://www.youtube-nocookie.com/embed/" + videoId + "?rel=0' frameborder='0' allow='autoplay; encrypted-media' allowfullscreen></iframe>";
|
|
|
|
|
|
|
|
let videoHtml = '';
|
2018-07-24 03:11:56 +02:00
|
|
|
|
|
|
|
const checkSavedVideo = videoIsSaved(videoId);
|
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
// Change the save button icon and text depending on if the user has saved the video or not.
|
|
|
|
checkSavedVideo.then((results) => {
|
|
|
|
if (results === false) {
|
|
|
|
playerView.savedText = 'FAVORITE';
|
|
|
|
playerView.savedIconType = 'far unsaved';
|
|
|
|
} else {
|
|
|
|
playerView.savedText = 'FAVORITED';
|
|
|
|
playerView.savedIconType = 'fas saved';
|
|
|
|
}
|
2018-03-10 22:06:42 +01:00
|
|
|
});
|
2018-09-19 17:01:21 +02:00
|
|
|
//"kpkXPy_jXmU"
|
|
|
|
invidiousAPI('videos', videoId, {}, function (data) {
|
|
|
|
|
2018-09-21 16:49:23 +02:00
|
|
|
console.log(data);
|
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
// Figure out the width for the like/dislike bar.
|
|
|
|
playerView.videoLikes = data.likeCount;
|
|
|
|
playerView.videoDislikes = data.dislikeCount;
|
|
|
|
let totalLikes = parseInt(playerView.videoLikes) + parseInt(playerView.videoDislikes);
|
|
|
|
playerView.likePercentage = parseInt((playerView.videoLikes / totalLikes) * 100);
|
|
|
|
|
|
|
|
/*invidiousAPI('videos', "9Ww-TQUeA3E", {}, function (data) {
|
|
|
|
console.log(data);
|
|
|
|
});*/
|
|
|
|
|
|
|
|
playerView.videoTitle = data.title;
|
|
|
|
playerView.channelName = data.author;
|
|
|
|
playerView.channelId = data.authorId;
|
|
|
|
//playerView.channelIcon = data['author']['avatar'];
|
|
|
|
|
|
|
|
let videoUrls = data.formatStreams;
|
|
|
|
|
|
|
|
// Add commas to the video view count.
|
|
|
|
playerView.videoViews = data.viewCount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
|
|
|
|
|
|
playerView.videoThumbnail = data.videoThumbnails[0].url;
|
|
|
|
|
|
|
|
// Format the date to a more readable format.
|
|
|
|
let dateString = new Date(data.published);
|
|
|
|
dateString.setDate(dateString.getDate() + 1);
|
|
|
|
playerView.publishedDate = dateFormat(dateString, "mmm dS, yyyy");
|
|
|
|
|
|
|
|
let description = data.description;
|
|
|
|
// Adds clickable links to the description.
|
|
|
|
playerView.description = autolinker.link(description);
|
|
|
|
|
|
|
|
// Search through the returned object to get the 480p and 720p video URLs (If available)
|
|
|
|
Object.keys(videoUrls).forEach((key) => {
|
|
|
|
switch (videoUrls[key]['itag']) {
|
|
|
|
case '18':
|
|
|
|
playerView.video480p = decodeURIComponent(videoUrls[key]['url']);
|
|
|
|
//console.log(video480p);
|
|
|
|
break;
|
|
|
|
case '22':
|
|
|
|
playerView.video720p = decodeURIComponent(videoUrls[key]['url']);
|
|
|
|
//console.log(video720p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let useEmbedPlayer = false;
|
|
|
|
|
|
|
|
// Default to the embeded player if the URLs cannot be found.
|
|
|
|
if (typeof (playerView.video720p) === 'undefined' && typeof (playerView.video480p) === 'undefined') {
|
|
|
|
//useEmbedPlayer = true;
|
|
|
|
playerView.currentQuality = 'EMBED';
|
|
|
|
//playerView.playerSeen = false;
|
|
|
|
//useEmbedPlayer = true;
|
|
|
|
showToast('Unable to get video file. Reverting to embeded player.');
|
|
|
|
} else if (typeof (playerView.video720p) === 'undefined' && typeof (playerView.video480p) !== 'undefined') {
|
|
|
|
// Default to the 480p video if the 720p URL cannot be found.
|
|
|
|
console.log('Found');
|
|
|
|
playerView.videoUrl = playerView.video480p;
|
|
|
|
playerView.currentQuality = '480p';
|
|
|
|
} else {
|
|
|
|
// Default to the 720p video.
|
|
|
|
playerView.videoUrl = playerView.video720p;
|
|
|
|
playerView.currentQuality = '720p';
|
|
|
|
//playerView.videoUrl = playerView.liveManifest;
|
|
|
|
}
|
2018-04-01 00:14:07 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
if (!useEmbedPlayer) {
|
|
|
|
data.captions.forEach((caption) => {
|
2018-09-21 16:49:23 +02:00
|
|
|
let subtitleUrl = 'https://www.invidio.us/api/v1/captions/' + videoId + '?label=' + caption.label;
|
2018-03-26 22:28:52 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
videoHtml = videoHtml + '<track kind="subtitles" src="' + subtitleUrl + '" srclang="' + caption.languageCode + '" label="' + caption.label + '">';
|
|
|
|
});
|
2018-03-26 22:28:52 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
playerView.subtitleHtml = videoHtml;
|
|
|
|
}
|
2018-08-03 04:18:08 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
const checkSubscription = isSubscribed(playerView.channelId);
|
|
|
|
|
|
|
|
checkSubscription.then((results) => {
|
|
|
|
if (results === false) {
|
|
|
|
if (subscribeButton != null) {
|
|
|
|
playerView.subscribedText = 'SUBSCRIBE';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (subscribeButton != null) {
|
|
|
|
playerView.subscribedText = 'UNSUBSCRIBE';
|
|
|
|
}
|
2018-07-24 03:11:56 +02:00
|
|
|
}
|
2018-09-19 17:01:21 +02:00
|
|
|
});
|
2018-06-02 00:17:33 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
playerView.recommendedVideoList = [];
|
2018-06-02 00:17:33 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
data.recommendedVideos.forEach((video) => {
|
|
|
|
let data = {};
|
2018-06-02 00:17:33 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
let time = video.lengthSeconds;
|
|
|
|
let hours = 0;
|
2018-06-11 04:58:25 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
if (time >= 3600) {
|
|
|
|
hours = Math.floor(time / 3600);
|
|
|
|
time = time - hours * 3600;
|
|
|
|
}
|
2018-06-02 00:17:33 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
let minutes = Math.floor(time / 60);
|
|
|
|
let seconds = time - minutes * 60;
|
2018-03-12 04:06:30 +01:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
if (seconds < 10) {
|
|
|
|
seconds = '0' + seconds;
|
|
|
|
}
|
2018-07-24 03:11:56 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
if (hours > 0) {
|
|
|
|
data.duration = hours + ":" + minutes + ":" + seconds;
|
|
|
|
} else {
|
|
|
|
data.duration = minutes + ":" + seconds;
|
|
|
|
}
|
2018-06-25 22:04:11 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
data.id = video.id;
|
|
|
|
data.title = video.title;
|
|
|
|
data.channelName = video.author;
|
2018-09-21 16:49:23 +02:00
|
|
|
data.thumbnail = video.videoThumbnails[4].url;
|
2018-09-19 17:01:21 +02:00
|
|
|
//data.publishedDate = dateFormat(snippet.publishedAt, "mmm dS, yyyy");
|
|
|
|
data.viewCount = video.viewCountText;
|
2018-03-12 04:06:30 +01:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
playerView.recommendedVideoList = playerView.recommendedVideoList.concat(data);
|
|
|
|
});
|
2018-05-14 16:39:02 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
loadingView.seen = false;
|
|
|
|
|
|
|
|
if (subscriptionView.seen === false && aboutView.seen === false && headerView.seen === false && searchView.seen === false && settingsView.seen === false && popularView.seen === false && savedView.seen === false && historyView.seen === false && channelView.seen === false && channelVideosView.seen === false) {
|
|
|
|
playerView.seen = true;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-02 00:17:33 +02:00
|
|
|
|
2018-09-19 20:34:23 +02:00
|
|
|
if (rememberHistory === true){
|
|
|
|
addToHistory(videoId);
|
|
|
|
}
|
2018-09-19 17:01:21 +02:00
|
|
|
|
|
|
|
// Hide subtitles by default
|
|
|
|
/*if (typeof (info['subtitles']) !== 'undefined' && Object.keys(info['subtitles']).length > 0) {
|
|
|
|
let textTracks = $('.videoPlayer').get(0).textTracks;
|
|
|
|
Object.keys(textTracks).forEach((track) => {
|
|
|
|
textTracks[track].mode = 'hidden';
|
|
|
|
});
|
|
|
|
}*/
|
|
|
|
|
|
|
|
window.setTimeout(checkVideoUrls, 5000, playerView.video480p, playerView.video720p);
|
2018-09-21 16:49:23 +02:00
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
});
|
2018-03-10 22:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-12 04:06:30 +01:00
|
|
|
* Open up the mini player to watch the video outside of the main application.
|
|
|
|
*
|
|
|
|
* @param {string} videoThumbnail - The URL of the video thumbnail. Used to prevent another API call.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2018-09-19 17:01:21 +02:00
|
|
|
function openMiniPlayer() {
|
|
|
|
let lastTime;
|
|
|
|
let videoHtml;
|
|
|
|
|
|
|
|
// Grabs whatever the HTML is for the current video player. Done this way to grab
|
|
|
|
// the HTML5 player (with varying qualities) as well as the YouTube embeded player.
|
|
|
|
if ($('.videoPlayer').length > 0) {
|
|
|
|
$('.videoPlayer').get(0).pause();
|
|
|
|
lastTime = $('.videoPlayer').get(0).currentTime;
|
|
|
|
videoHtml = $('.videoPlayer').get(0).outerHTML;
|
|
|
|
} else {
|
|
|
|
videoHtml = $('iframe').get(0).outerHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new browser window.
|
|
|
|
const BrowserWindow = electron.remote.BrowserWindow;
|
|
|
|
|
|
|
|
let miniPlayer = new BrowserWindow({
|
|
|
|
width: 1200,
|
|
|
|
height: 710
|
|
|
|
});
|
|
|
|
|
|
|
|
// Use the miniPlayer.html template.
|
|
|
|
$.get('templates/miniPlayer.html', (template) => {
|
|
|
|
mustache.parse(template);
|
|
|
|
const rendered = mustache.render(template, {
|
|
|
|
videoHtml: videoHtml,
|
|
|
|
videoThumbnail: playerView.thumbnail,
|
|
|
|
startTime: lastTime,
|
|
|
|
});
|
|
|
|
// Render the template to the new browser window.
|
|
|
|
miniPlayer.loadURL("data:text/html;charset=utf-8," + encodeURI(rendered));
|
|
|
|
});
|
|
|
|
}
|
2018-03-10 22:06:42 +01:00
|
|
|
|
2018-09-21 16:49:23 +02:00
|
|
|
function checkVideoSettings() {
|
|
|
|
let player = document.getElementById('videoPlayer');
|
|
|
|
|
|
|
|
if (autoplay) {
|
|
|
|
console.log(player);
|
|
|
|
player.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
player.volume = currentVolume;
|
|
|
|
}
|
|
|
|
|
2018-03-10 22:06:42 +01:00
|
|
|
/**
|
2018-03-12 04:06:30 +01:00
|
|
|
* Change the quality of the current video.
|
|
|
|
*
|
|
|
|
* @param {string} videoHtml - The HTML of the video player to be set.
|
|
|
|
* @param {string} qualityType - The Quality Type of the video. Ex: 720p, 480p
|
|
|
|
* @param {boolean} isEmbed - Optional: Value on if the videoHtml is the embeded player.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2018-07-24 02:15:20 +02:00
|
|
|
function changeQuality(url, qualityText, isEmbed = false) {
|
2018-09-19 17:01:21 +02:00
|
|
|
if (videoHtml == '') {
|
|
|
|
showToast('Video quality type is not available. Unable to change quality.')
|
|
|
|
return;
|
|
|
|
}
|
2018-07-24 03:11:56 +02:00
|
|
|
|
|
|
|
videoHtml = videoHtml.replace(/\"\;/g, '"');
|
|
|
|
|
|
|
|
ft.log('HTML Video: ', videoHtml);
|
|
|
|
ft.log('(Is the video embeded?) isEmbed: ', isEmbed);
|
|
|
|
|
|
|
|
// The YouTube API creates 2 more iFrames. This is why a boolean value is sent
|
|
|
|
// with the function.
|
|
|
|
const embedPlayer = document.getElementsByTagName('IFRAME')[0];
|
|
|
|
|
|
|
|
const html5Player = document.getElementsByClassName('videoPlayer');
|
|
|
|
|
|
|
|
ft.log('Embeded Player Element: ', embedPlayer);
|
|
|
|
ft.log('HTML5 Player Element: ', html5Player);
|
|
|
|
|
|
|
|
if (isEmbed && html5Player.length == 0) {
|
|
|
|
// The embeded player is already playing. Return.
|
|
|
|
showToast('You are already using the embeded player.')
|
|
|
|
return;
|
|
|
|
} else if (isEmbed) {
|
|
|
|
// Switch from HTML 5 player to embeded Player
|
|
|
|
html5Player[0].remove();
|
|
|
|
const mainHtml = $('#main').html();
|
|
|
|
$('#main').html(videoHtml + mainHtml);
|
|
|
|
$('#currentQuality').html(qualityType);
|
|
|
|
} else if (html5Player.length == 0) {
|
|
|
|
// Switch from embeded player to HTML 5 player
|
|
|
|
embedPlayer.remove();
|
|
|
|
let videoPlayer = document.createElement('video');
|
|
|
|
videoPlayer.className = 'videoPlayer';
|
|
|
|
videoPlayer.src = videoHtml;
|
|
|
|
videoPlayer.controls = true;
|
|
|
|
videoPlayer.autoplay = true;
|
|
|
|
$('#main').prepend(videoPlayer);
|
|
|
|
$('#currentQuality').html(qualityType);
|
|
|
|
} else {
|
|
|
|
// Switch src on HTML 5 player
|
|
|
|
const currentPlayBackTime = $('.videoPlayer').get(0).currentTime;
|
|
|
|
html5Player[0].src = videoHtml;
|
|
|
|
html5Player[0].load();
|
|
|
|
$('.videoPlayer').get(0).currentTime = currentPlayBackTime;
|
|
|
|
$('#currentQuality').html(qualityType);
|
|
|
|
}
|
2018-03-10 22:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-12 04:06:30 +01:00
|
|
|
* Change the playpack speed of the video.
|
|
|
|
*
|
|
|
|
* @param {double} speed - The playback speed of the video.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function changeVideoSpeed(speed) {
|
2018-07-24 03:11:56 +02:00
|
|
|
$('#currentSpeed').html(speed);
|
|
|
|
$('.videoPlayer').get(0).playbackRate = speed;
|
2018-03-10 22:06:42 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 22:13:01 +01:00
|
|
|
/**
|
2018-03-12 04:06:30 +01:00
|
|
|
* Change the volume of the video player
|
|
|
|
*
|
|
|
|
* @param {double} amount - The volume to increase or descrease the volume by. Will be any double between 0 and 1.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function changeVolume(amount) {
|
2018-07-24 03:11:56 +02:00
|
|
|
const videoPlayer = $('.videoPlayer').get(0);
|
|
|
|
let volume = videoPlayer.volume;
|
|
|
|
volume = volume + amount;
|
|
|
|
if (volume > 1) {
|
|
|
|
videoPlayer.volume = 1;
|
|
|
|
} else if (volume < 0) {
|
|
|
|
videoPlayer.volume = 0;
|
|
|
|
} else {
|
|
|
|
videoPlayer.volume = volume;
|
|
|
|
}
|
2018-03-10 22:06:42 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 22:13:01 +01:00
|
|
|
/**
|
2018-03-12 04:06:30 +01:00
|
|
|
* Change the duration of the current time of a video by a few seconds.
|
|
|
|
*
|
|
|
|
* @param {integer} seconds - The amount of seconds to change the video by. Integer may be positive or negative.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function changeDurationBySeconds(seconds) {
|
2018-07-24 03:11:56 +02:00
|
|
|
const videoPlayer = $('.videoPlayer').get(0);
|
|
|
|
videoPlayer.currentTime = videoPlayer.currentTime + seconds;
|
2018-03-10 22:06:42 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 22:13:01 +01:00
|
|
|
/**
|
2018-03-12 04:06:30 +01:00
|
|
|
* Change the duration of a video by a percentage of the duration.
|
|
|
|
*
|
|
|
|
* @param {double} percentage - The percentage to hop to of the video. Will be any double between 0 and 1.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function changeDurationByPercentage(percentage) {
|
2018-07-24 03:11:56 +02:00
|
|
|
const videoPlayer = $('.videoPlayer').get(0);
|
|
|
|
videoPlayer.currentTime = videoPlayer.duration * percentage;
|
2018-08-03 04:18:08 +02:00
|
|
|
}
|
2018-09-21 16:49:23 +02:00
|
|
|
|
|
|
|
function updateVolume(){
|
|
|
|
let player = document.getElementById('videoPlayer');
|
|
|
|
currentVolume = player.volume
|
|
|
|
}
|