From 8e86b35477852389703f627bbf96513ceb202b79 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 3 Aug 2022 09:05:34 +0200 Subject: [PATCH] remove ffmpeg and allow downloading by video name --- app/build.gradle | 2 - .../libretube/dialogs/DownloadDialog.kt | 20 ++++-- .../libretube/fragments/PlayerFragment.kt | 4 +- .../libretube/services/DownloadService.kt | 68 +++---------------- app/src/main/res/layout/dialog_download.xml | 28 +++++++- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/advanced_settings.xml | 9 --- 7 files changed, 52 insertions(+), 80 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9067f4ccd..3380516fb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -97,8 +97,6 @@ dependencies { // Do not update jackson annotations! It does not supports < API 26. implementation libs.jacksonAnnotations - implementation libs.mobileffmpeg - coreLibraryDesugaring libs.desugaring implementation libs.cronet.embedded implementation libs.cronet.okhttp diff --git a/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt b/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt index 60afa6c48..5dbf63ea7 100644 --- a/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt @@ -4,6 +4,7 @@ import android.app.Dialog import android.content.Intent import android.os.Bundle import android.util.Log +import android.view.View import android.widget.ArrayAdapter import android.widget.Toast import androidx.appcompat.app.AppCompatActivity @@ -41,6 +42,16 @@ class DownloadDialog : DialogFragment() { binding.title.text = ThemeHelper.getStyledAppName(requireContext()) + binding.audioRadio.setOnClickListener { + binding.videoSpinner.visibility = View.GONE + binding.audioSpinner.visibility = View.VISIBLE + } + + binding.videoRadio.setOnClickListener { + binding.audioSpinner.visibility = View.GONE + binding.videoSpinner.visibility = View.VISIBLE + } + builder.setView(binding.root) builder.create() } ?: throw IllegalStateException("Activity cannot be null") @@ -118,14 +129,15 @@ class DownloadDialog : DialogFragment() { if (binding.audioSpinner.size >= 1) binding.audioSpinner.setSelection(1) binding.download.setOnClickListener { - val selectedAudioUrl = audioUrl[binding.audioSpinner.selectedItemPosition] - val selectedVideoUrl = vidUrl[binding.videoSpinner.selectedItemPosition] + val selectedAudioUrl = + if (binding.audioRadio.isChecked) audioUrl[binding.audioSpinner.selectedItemPosition] else "" + val selectedVideoUrl = + if (binding.videoRadio.isChecked) vidUrl[binding.videoSpinner.selectedItemPosition] else "" val intent = Intent(context, DownloadService::class.java) - intent.putExtra("videoId", videoId) + intent.putExtra("videoName", streams.title) intent.putExtra("videoUrl", selectedVideoUrl) intent.putExtra("audioUrl", selectedAudioUrl) - intent.putExtra("duration", duration) context?.startService(intent) dismiss() } diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index c2de71680..f5dcfc7b5 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -1278,7 +1278,7 @@ class PlayerFragment : Fragment() { // get the name of the currently played chapter private fun getCurrentChapterIndex(): Int { val currentPosition = exoPlayer.currentPosition - var chapterIndex: Int? = null + var chapterIndex = 0 chapters.forEachIndexed { index, chapter -> // check whether the chapter start is greater than the current player position @@ -1287,7 +1287,7 @@ class PlayerFragment : Fragment() { chapterIndex = index } } - return chapterIndex!! + return chapterIndex } private fun setMediaSource( diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index 266225939..a0d7c5cf3 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -17,7 +17,6 @@ import android.os.IBinder import android.util.Log import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat -import com.arthenica.ffmpegkit.FFmpegKit import com.github.libretube.DOWNLOAD_CHANNEL_ID import com.github.libretube.DOWNLOAD_FAILURE_NOTIFICATION_ID import com.github.libretube.DOWNLOAD_PENDING_NOTIFICATION_ID @@ -35,11 +34,9 @@ class DownloadService : Service() { private lateinit var notification: NotificationCompat.Builder private var downloadId: Long = -1 - private lateinit var videoId: String + private lateinit var videoName: String private lateinit var videoUrl: String private lateinit var audioUrl: String - private lateinit var extension: String - private var duration: Int = 0 private var downloadType: Int = 3 private lateinit var audioDir: File @@ -52,13 +49,11 @@ class DownloadService : Service() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - videoId = intent?.getStringExtra("videoId")!! + videoName = intent?.getStringExtra("videoName")!! videoUrl = intent.getStringExtra("videoUrl")!! audioUrl = intent.getStringExtra("audioUrl")!! - duration = intent.getIntExtra("duration", 1) - extension = PreferenceHelper.getString(PreferenceKeys.DOWNLOAD_VIDEO_FORMAT, ".mp4")!! - downloadType = if (audioUrl != "" && videoUrl != "") DownloadType.MUX - else if (audioUrl != "") DownloadType.AUDIO + + downloadType = if (audioUrl != "") DownloadType.AUDIO else if (videoUrl != "") DownloadType.VIDEO else DownloadType.NONE if (downloadType != DownloadType.NONE) { @@ -115,18 +110,8 @@ class DownloadService : Service() { IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE) ) when (downloadType) { - DownloadType.MUX -> { - audioDir = File(tempDir, "$videoId-audio") - videoDir = File(tempDir, "$videoId-video") - downloadId = downloadManagerRequest( - getString(R.string.video), - getString(R.string.downloading), - videoUrl, - videoDir - ) - } DownloadType.VIDEO -> { - videoDir = File(libretubeDir, "$videoId-video") + videoDir = File(libretubeDir, videoName) downloadId = downloadManagerRequest( getString(R.string.video), getString(R.string.downloading), @@ -135,7 +120,7 @@ class DownloadService : Service() { ) } DownloadType.AUDIO -> { - audioDir = File(libretubeDir, "$videoId-audio") + audioDir = File(libretubeDir, videoName) downloadId = downloadManagerRequest( getString(R.string.audio), getString(R.string.downloading), @@ -146,6 +131,7 @@ class DownloadService : Service() { } } catch (e: IllegalArgumentException) { Log.e(TAG, "download error $e") + downloadFailedNotification() } } @@ -166,11 +152,6 @@ class DownloadService : Service() { downloadSucceededNotification() onDestroy() } - } else { - try { - muxDownloadedMedia() - } catch (e: Exception) { - } } } } @@ -233,7 +214,7 @@ class DownloadService : Service() { val builder = NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID) .setSmallIcon(R.drawable.ic_download) .setContentTitle(resources.getString(R.string.success)) - .setContentText(getString(R.string.fail)) + .setContentText(getString(R.string.downloadsucceeded)) .setPriority(NotificationCompat.PRIORITY_HIGH) with(NotificationManagerCompat.from(this@DownloadService)) { // notificationId is a unique int for each notification that you must define @@ -241,39 +222,6 @@ class DownloadService : Service() { } } - private fun muxDownloadedMedia() { - val command = "-y -i $videoDir -i $audioDir -c copy $libretubeDir/${videoId}$extension" - notification.setContentTitle("Muxing") - FFmpegKit.executeAsync( - command, - { session -> - val state = session.state - val returnCode = session.returnCode - // CALLED WHEN SESSION IS EXECUTED - Log.d( - TAG, - String.format( - "FFmpeg process exited with state %s and rc %s.%s", - state, - returnCode, - session.failStackTrace - ) - ) - tempDir.deleteRecursively() - if (returnCode.toString() != "0") downloadFailedNotification() - else downloadSucceededNotification() - onDestroy() - }, - { - // CALLED WHEN SESSION PRINTS LOGS - Log.e(TAG, it.message.toString()) - } - ) { - // CALLED WHEN SESSION GENERATES STATISTICS - Log.e(TAG + "stat", it.time.toString()) - } - } - override fun onDestroy() { try { unregisterReceiver(onDownloadComplete) diff --git a/app/src/main/res/layout/dialog_download.xml b/app/src/main/res/layout/dialog_download.xml index b77541111..471ef9f5d 100644 --- a/app/src/main/res/layout/dialog_download.xml +++ b/app/src/main/res/layout/dialog_download.xml @@ -4,7 +4,6 @@ android:layout_height="wrap_content" android:orientation="vertical"> - + + + + + + + + + android:layout_margin="8dp" + android:visibility="gone" />