-
-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6381 from Bnyro/master
feat: local streams extraction
- Loading branch information
Showing
19 changed files
with
214 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
app/src/main/java/com/github/libretube/api/StreamsExtractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.github.libretube.api | ||
|
||
import com.github.libretube.api.obj.ChapterSegment | ||
import com.github.libretube.api.obj.MetaInfo | ||
import com.github.libretube.api.obj.PreviewFrames | ||
import com.github.libretube.api.obj.StreamItem | ||
import com.github.libretube.api.obj.Streams | ||
import com.github.libretube.api.obj.Subtitle | ||
import com.github.libretube.helpers.PlayerHelper | ||
import com.github.libretube.util.NewPipeDownloaderImpl | ||
import kotlinx.datetime.toKotlinInstant | ||
import org.schabi.newpipe.extractor.NewPipe | ||
import org.schabi.newpipe.extractor.stream.StreamInfo | ||
import org.schabi.newpipe.extractor.stream.StreamInfoItem | ||
|
||
object StreamsExtractor { | ||
// val npe by lazy { | ||
// NewPipe.getService(ServiceList.YouTube.serviceId) | ||
// } | ||
|
||
init { | ||
NewPipe.init(NewPipeDownloaderImpl()) | ||
} | ||
|
||
suspend fun extractStreams(videoId: String): Streams { | ||
if (!PlayerHelper.disablePipedProxy || !PlayerHelper.localStreamExtraction) { | ||
return RetrofitInstance.api.getStreams(videoId) | ||
} | ||
|
||
val resp = StreamInfo.getInfo("https://www.youtube.com/watch?v=$videoId") | ||
return Streams( | ||
title = resp.name, | ||
description = resp.description.toString(), | ||
uploader = resp.uploaderName, | ||
uploaderAvatar = resp.uploaderAvatars.maxBy { it.height }.url, | ||
uploaderUrl = resp.uploaderUrl, | ||
uploaderVerified = resp.isUploaderVerified, | ||
uploaderSubscriberCount = resp.uploaderSubscriberCount, | ||
category = resp.category, | ||
views = resp.viewCount, | ||
likes = resp.likeCount, | ||
license = resp.licence, | ||
hls = resp.hlsUrl, | ||
dash = resp.dashMpdUrl, | ||
tags = resp.tags, | ||
metaInfo = resp.metaInfo.map { | ||
MetaInfo( | ||
it.title, | ||
it.content.content, | ||
it.urls.map { url -> url.toString() }, | ||
it.urlTexts | ||
) | ||
}, | ||
visibility = resp.privacy.name.lowercase(), | ||
duration = resp.duration, | ||
uploadTimestamp = resp.uploadDate.offsetDateTime().toInstant().toKotlinInstant(), | ||
uploaded = resp.uploadDate.offsetDateTime().toEpochSecond(), | ||
thumbnailUrl = resp.thumbnails.maxBy { it.height }.url, | ||
relatedStreams = resp.relatedItems.map { it as StreamInfoItem }.map { | ||
StreamItem( | ||
it.url, | ||
it.infoType.name, | ||
it.name, | ||
it.thumbnails.maxBy { image -> image.height }.url, | ||
it.uploaderName, | ||
it.uploaderUrl, | ||
it.uploaderAvatars.maxBy { image -> image.height }.url, | ||
it.textualUploadDate, | ||
it.duration, | ||
it.viewCount, | ||
it.isUploaderVerified, | ||
it.uploadDate?.offsetDateTime()?.toEpochSecond() ?: 0L, | ||
it.shortDescription, | ||
it.isShortFormContent, | ||
) | ||
}, | ||
chapters = resp.streamSegments.map { | ||
ChapterSegment( | ||
title = it.title, | ||
image = it.previewUrl.orEmpty(), | ||
start = it.startTimeSeconds.toLong() | ||
) | ||
}, | ||
audioStreams = emptyList(), // TODO: audio streams and video streams via DASH, currently broken anyways | ||
videoStreams = emptyList(), | ||
previewFrames = resp.previewFrames.map { | ||
PreviewFrames( | ||
it.urls, | ||
it.frameWidth, | ||
it.frameHeight, | ||
it.totalCount, | ||
it.durationPerFrame.toLong(), | ||
it.framesPerPageX, | ||
it.framesPerPageY | ||
) | ||
}, | ||
subtitles = resp.subtitles.map { | ||
Subtitle( | ||
it.content, | ||
it.format?.mimeType, | ||
it.displayLanguageName, | ||
it.languageTag, | ||
it.isAutoGenerated | ||
) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/github/libretube/util/NewPipeDownloaderImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.github.libretube.util | ||
|
||
import java.io.IOException | ||
import java.util.concurrent.TimeUnit | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import org.schabi.newpipe.extractor.downloader.Downloader | ||
import org.schabi.newpipe.extractor.downloader.Request | ||
import org.schabi.newpipe.extractor.downloader.Response | ||
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException | ||
|
||
class NewPipeDownloaderImpl : Downloader() { | ||
private val client: OkHttpClient = OkHttpClient.Builder() | ||
.readTimeout(READ_TIMEOUT_SECONDS, TimeUnit.SECONDS) | ||
.build() | ||
|
||
@Throws(IOException::class, ReCaptchaException::class) | ||
override fun execute(request: Request): Response { | ||
val url = request.url() | ||
|
||
val requestBody = request.dataToSend()?.let { | ||
it.toRequestBody(APPLICATION_JSON, 0, it.size) | ||
} | ||
|
||
val requestBuilder = okhttp3.Request.Builder() | ||
.method(request.httpMethod(), requestBody) | ||
.url(url) | ||
.addHeader(USER_AGENT_HEADER_NAME, USER_AGENT) | ||
|
||
for ((headerName, headerValueList) in request.headers()) { | ||
requestBuilder.removeHeader(headerName) | ||
for (headerValue in headerValueList) { | ||
requestBuilder.addHeader(headerName, headerValue) | ||
} | ||
} | ||
|
||
val response = client.newCall(requestBuilder.build()).execute() | ||
if (response.code == CAPTCHA_STATUS_CODE) { | ||
response.close() | ||
throw ReCaptchaException("reCaptcha Challenge requested", url) | ||
} | ||
|
||
return Response( | ||
response.code, | ||
response.message, | ||
response.headers.toMultimap(), | ||
response.body?.string(), | ||
response.request.url.toString() | ||
) | ||
} | ||
|
||
companion object { | ||
private const val USER_AGENT_HEADER_NAME = "User-Agent" | ||
private const val USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0" | ||
private const val CAPTCHA_STATUS_CODE = 429 | ||
private val APPLICATION_JSON = "application/json".toMediaType() | ||
private const val READ_TIMEOUT_SECONDS = 30L | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ allprojects { | |
repositories { | ||
google() | ||
mavenCentral() | ||
maven { setUrl("https://jitpack.io") } | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.