/*
This file is part of FreeTube.
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.
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.
You should have received a copy of the GNU General Public License
along with FreeTube. If not, see .
*/
/*
* File for functions related to videos.
*/
/**
* Display the video player and play a video
*
* @param {string} videoId - The video ID of the video to be played.
*
* @return {Void}
*/
function playVideo(videoId, playlistId = '') {
hideViews();
playerView.playerSeen = true;
playerView.firstLoad = true;
playerView.videoId = videoId;
playerView.videoAudio = undefined;
playerView.validAudio = true;
playerView.video480p = undefined;
playerView.valid480p = true;
playerView.video720p = undefined;
playerView.valid720p = true;
playerView.videoUrl = '';
playerView.embededHtml = "";
let videoHtml = '';
const checkSavedVideo = videoIsSaved(videoId);
// 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';
}
});
youtubedlGetInfo(videoId, (data) => {
console.log(data);
let videoUrls = data.formats;
let formatUrls = data.player_response.streamingData.adaptiveFormats;
// 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(playerView.video480p);
break;
case '22':
playerView.video720p = decodeURIComponent(videoUrls[key]['url']);
// console.log(playerView.video720p);
break;
}
});
// Last adaptive format will be best the quality audio stream (migrate fully to adaptive formats later)
playerView.videoAudio = decodeURIComponent(formatUrls[formatUrls.length - 1]['url']);
if (typeof(playerView.videoAudio) === 'undefined') {
console.log(playerView.videoAudio);
playerView.validAudio = false;
}
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;
}
if (!useEmbedPlayer &&
typeof(data.player_response.captions) !== 'undefined' &&
typeof(data.player_response.captions.playerCaptionsTracklistRenderer) !== 'undefined' &&
typeof(data.player_response.captions.playerCaptionsTracklistRenderer.captionTracks) !== 'undefined') {
data.player_response.captions.playerCaptionsTracklistRenderer.captionTracks.forEach((caption) => {
let subtitleUrl = invidiousInstance + '/api/v1/captions/' + videoId + '?label=' + caption.name.simpleText;
videoHtml = videoHtml + '