Skip to content

Commit

Permalink
Add ability to playback downloaded videos
Browse files Browse the repository at this point in the history
Videos that are downloaded will always show their downloaded version and not fetch either API.

#348
  • Loading branch information
MarmadileManteater committed Jul 16, 2024
1 parent c01abe6 commit e03c16f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
44 changes: 44 additions & 0 deletions src/renderer/helpers/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,47 @@ export async function addToDownloadQueue(downloadRequest) {
event.data = downloadRequest
window.dispatchEvent(event)
}

export async function getDownloadedVideos() {
const videoDirectory = await getVideosDirectory()
const videos = await videoDirectory.listFiles()
return videos
}

export async function getVideoInformationDownloaded(videoId, component) {
const videos = await getDownloadedVideos()
const matchingVideoFolders = videos.filter(video => video.fileName === videoId)
if (matchingVideoFolders.length > 0) {
const filesContainedInVideoDirectory = await matchingVideoFolders[0].listFiles()
const dataFiles = filesContainedInVideoDirectory.filter(file => file.fileName === 'data.json')
if (dataFiles.length > 0) {
const response = JSON.parse(await readFile(dataFiles[0].uri))
// now to dump all of the data pulled out of the download prompt data back into the page
component.videoTitle = response.title
component.channelId = response.channelId
component.channelName = response.channelName
component.channelThumbnail = response.channelThumbnail
component.videoPublished = response.published
component.channelSubscriptionCountText = response.channelSubscriptionCountText
component.videoLikeCount = response.likes
component.videoLengthSeconds = response.lengthSeconds
component.thumbnail = response.thumbnail
component.videoChapters = response.chapters
component.videoDescription = response.description
component.videoDescriptionHtml = response.descriptionHtml
component.videoSourceList = [
{
url: response.uri,
qualityLabel: 'unknown'// TODO pass through information from prompt
}
]

component.activeFormat = 'legacy'
component.activeSourceList = component.videoSourceList
component.audioSourceList = null
component.dashSrc = null
component.isLoading = false
return response
}
}
}
47 changes: 31 additions & 16 deletions src/renderer/views/Watch/Watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
youtubeImageUrlToInvidious
} from '../../helpers/api/invidious'
import {
createMediaSession, getDownloadFormats
createMediaSession, getDownloadFormats, getVideoInformationDownloaded
} from '../../helpers/android'
import android from 'android'

Expand Down Expand Up @@ -275,18 +275,26 @@ export default defineComponent({
this.checkIfPlaylist()
this.checkIfTimestamp()

switch (this.backendPreference) {
case 'local':
this.getVideoInformationLocal(this.videoId)
break
case 'invidious':
this.getVideoInformationInvidious(this.videoId)
const promise = process.env.IS_ANDROID && this.$store.getters.getDownloadBehavior === 'download'
? getVideoInformationDownloaded(this.videoId, this)
: new Promise((resolve, _reject) => resolve())

promise.then((downloadedInfo) => {
if (downloadedInfo === undefined) {
switch (this.backendPreference) {
case 'local':
this.getVideoInformationLocal(this.videoId)
break
case 'invidious':
this.getVideoInformationInvidious(this.videoId)

if (this.forceLocalBackendForLegacy) {
this.getVideoInformationLocal(this.videoId)
if (this.forceLocalBackendForLegacy) {
this.getVideoInformationLocal(this.videoId)
}
break
}
break
}
}
})
},
async thumbnail() {
if (process.env.IS_ANDROID) {
Expand Down Expand Up @@ -369,12 +377,19 @@ export default defineComponent({

this.checkIfPlaylist()
this.checkIfTimestamp()
const promise = process.env.IS_ANDROID && this.$store.getters.getDownloadBehavior === 'download'
? getVideoInformationDownloaded(this.videoId, this)
: new Promise((resolve, _reject) => resolve())

if (!process.env.SUPPORTS_LOCAL_API || this.backendPreference === 'invidious') {
this.getVideoInformationInvidious()
} else {
this.getVideoInformationLocal()
}
promise.then((downloadedInfo) => {
if (downloadedInfo === undefined) {
if (!process.env.SUPPORTS_LOCAL_API || this.backendPreference === 'invidious') {
this.getVideoInformationInvidious()
} else {
this.getVideoInformationLocal()
}
}
})

window.addEventListener('beforeunload', this.handleWatchProgress)
},
Expand Down

0 comments on commit e03c16f

Please sign in to comment.