mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
only mux if needed
This commit is contained in:
parent
43fe899f2f
commit
2bc134d4a3
@ -7,7 +7,6 @@ import android.app.PendingIntent
|
|||||||
import android.app.Service
|
import android.app.Service
|
||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Context.DOWNLOAD_SERVICE
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.IntentFilter
|
import android.content.IntentFilter
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
@ -18,7 +17,6 @@ import android.os.IBinder
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
import androidx.core.app.ServiceCompat.stopForeground
|
|
||||||
import com.arthenica.ffmpegkit.FFmpegKit
|
import com.arthenica.ffmpegkit.FFmpegKit
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@ -36,6 +34,9 @@ class DownloadService : Service() {
|
|||||||
private lateinit var audioDir: File
|
private lateinit var audioDir: File
|
||||||
private lateinit var videoDir: File
|
private lateinit var videoDir: File
|
||||||
private lateinit var notification: NotificationCompat.Builder
|
private lateinit var notification: NotificationCompat.Builder
|
||||||
|
private lateinit var downloadType: String
|
||||||
|
private lateinit var libretubeDir: File
|
||||||
|
private lateinit var tempDir: File
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
IS_DOWNLOAD_RUNNING = true
|
IS_DOWNLOAD_RUNNING = true
|
||||||
@ -47,8 +48,16 @@ class DownloadService : Service() {
|
|||||||
audioUrl = intent.getStringExtra("audioUrl")!!
|
audioUrl = intent.getStringExtra("audioUrl")!!
|
||||||
extension = intent.getStringExtra("extension")!!
|
extension = intent.getStringExtra("extension")!!
|
||||||
duration = intent.getIntExtra("duration", 1)
|
duration = intent.getIntExtra("duration", 1)
|
||||||
downloadNotification(intent)
|
downloadType = if (audioUrl != "" && videoUrl != "") "mux"
|
||||||
downloadManager()
|
else if (audioUrl != "") "audio"
|
||||||
|
else if (videoUrl != "") "video"
|
||||||
|
else "none"
|
||||||
|
if (downloadType != "none") {
|
||||||
|
downloadNotification(intent)
|
||||||
|
downloadManager()
|
||||||
|
} else {
|
||||||
|
onDestroy()
|
||||||
|
}
|
||||||
|
|
||||||
return super.onStartCommand(intent, flags, startId)
|
return super.onStartCommand(intent, flags, startId)
|
||||||
}
|
}
|
||||||
@ -58,29 +67,48 @@ class DownloadService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun downloadManager() {
|
private fun downloadManager() {
|
||||||
val path = applicationContext.getExternalFilesDir(DIRECTORY_DOWNLOADS)
|
|
||||||
val folder_main = ".tmp"
|
// create folder for temporary files
|
||||||
val f = File(path, folder_main)
|
tempDir = File(
|
||||||
if (!f.exists()) {
|
applicationContext.getExternalFilesDir(DIRECTORY_DOWNLOADS),
|
||||||
f.mkdirs()
|
".tmp"
|
||||||
|
)
|
||||||
|
if (!tempDir.exists()) {
|
||||||
|
tempDir.mkdirs()
|
||||||
Log.e(TAG, "Directory make")
|
Log.e(TAG, "Directory make")
|
||||||
} else {
|
} else {
|
||||||
f.deleteRecursively()
|
tempDir.deleteRecursively()
|
||||||
f.mkdirs()
|
tempDir.mkdirs()
|
||||||
Log.e(TAG, "Directory already have")
|
Log.e(TAG, "Directory already have")
|
||||||
}
|
}
|
||||||
audioDir = File(f, "$videoId-audio")
|
|
||||||
videoDir = File(f, "$videoId-video")
|
// create LibreTube folder in Downloads
|
||||||
|
libretubeDir = File(
|
||||||
|
Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS),
|
||||||
|
"LibreTube"
|
||||||
|
)
|
||||||
|
if (!libretubeDir.exists()) libretubeDir.mkdirs()
|
||||||
|
|
||||||
|
// start download
|
||||||
try {
|
try {
|
||||||
Log.e(TAG, "Directory make")
|
|
||||||
registerReceiver(
|
registerReceiver(
|
||||||
onDownloadComplete,
|
onDownloadComplete,
|
||||||
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
|
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
|
||||||
)
|
)
|
||||||
if (videoUrl != "") {
|
when (downloadType) {
|
||||||
downloadId = downloadManagerRequest("Video", "Downloading", videoUrl, videoDir)
|
"mux" -> {
|
||||||
} else if (audioUrl != "") {
|
audioDir = File(tempDir, "$videoId-audio")
|
||||||
downloadId = downloadManagerRequest("Audio", "Downloading", audioUrl, audioDir)
|
videoDir = File(tempDir, "$videoId-video")
|
||||||
|
downloadId = downloadManagerRequest("Video", "Downloading", videoUrl, videoDir)
|
||||||
|
}
|
||||||
|
"video" -> {
|
||||||
|
videoDir = File(libretubeDir, "$videoId-video")
|
||||||
|
downloadId = downloadManagerRequest("Video", "Downloading", videoUrl, videoDir)
|
||||||
|
}
|
||||||
|
"audio" -> {
|
||||||
|
audioDir = File(libretubeDir, "$videoId-audio")
|
||||||
|
downloadId = downloadManagerRequest("Audio", "Downloading", audioUrl, audioDir)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
Log.e(TAG, "download error $e")
|
Log.e(TAG, "download error $e")
|
||||||
@ -92,22 +120,15 @@ class DownloadService : Service() {
|
|||||||
// Fetching the download id received with the broadcast
|
// Fetching the download id received with the broadcast
|
||||||
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
|
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
|
||||||
// Checking if the received broadcast is for our enqueued download by matching download id
|
// Checking if the received broadcast is for our enqueued download by matching download id
|
||||||
if (downloadId == id && videoUrl != "" && audioUrl != "") {
|
if (downloadId == id) {
|
||||||
downloadManagerRequest("Audio", "Downloading", audioUrl, audioDir)
|
if (downloadType == "mux") {
|
||||||
} else {
|
downloadManagerRequest("Audio", "Downloading", audioUrl, audioDir)
|
||||||
// create LibreTube folder in Downloads
|
|
||||||
val libreTubeDir = File(
|
|
||||||
Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS),
|
|
||||||
"LibreTube"
|
|
||||||
)
|
|
||||||
if (!libreTubeDir.exists()) {
|
|
||||||
libreTubeDir.mkdirs()
|
|
||||||
Log.e(TAG, "libreTube Directory make")
|
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "libreTube Directory already have")
|
downloadSucceededNotification()
|
||||||
|
onDestroy()
|
||||||
}
|
}
|
||||||
muxDownloadedMedia(libreTubeDir)
|
} else {
|
||||||
onDestroy()
|
muxDownloadedMedia()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -195,18 +216,25 @@ class DownloadService : Service() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun muxDownloadedMedia(targetDir: File) {
|
private fun downloadSucceededNotification() {
|
||||||
val command: String = when {
|
Log.i(TAG, "Download succeeded")
|
||||||
videoUrl == "" -> {
|
val builder = NotificationCompat.Builder(this@DownloadService, "failed")
|
||||||
"-y -i $audioDir -c copy $targetDir/$videoId-audio$extension"
|
.setSmallIcon(R.drawable.ic_download)
|
||||||
}
|
.setContentTitle(resources.getString(R.string.success))
|
||||||
audioUrl == "" -> {
|
.setContentText("success")
|
||||||
"-y -i $videoDir -c copy $targetDir/$videoId-video$extension"
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||||
}
|
createNotificationChannel(
|
||||||
else -> {
|
"success", "success", "Download succeeded",
|
||||||
"-y -i $videoDir -i $audioDir -c copy $targetDir/${videoId}$extension"
|
NotificationManager.IMPORTANCE_DEFAULT
|
||||||
}
|
)
|
||||||
|
with(NotificationManagerCompat.from(this@DownloadService)) {
|
||||||
|
// notificationId is a unique int for each notification that you must define
|
||||||
|
notify(70, builder.build())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun muxDownloadedMedia() {
|
||||||
|
val command = "-y -i $videoDir -i $audioDir -c copy $libretubeDir/${videoId}$extension"
|
||||||
notification.setContentTitle("Muxing")
|
notification.setContentTitle("Muxing")
|
||||||
FFmpegKit.executeAsync(
|
FFmpegKit.executeAsync(
|
||||||
command,
|
command,
|
||||||
@ -223,14 +251,10 @@ class DownloadService : Service() {
|
|||||||
session.failStackTrace
|
session.failStackTrace
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
val path =
|
tempDir.deleteRecursively()
|
||||||
applicationContext.getExternalFilesDir(DIRECTORY_DOWNLOADS)
|
|
||||||
val folder_main = ".tmp"
|
|
||||||
val f = File(path, folder_main)
|
|
||||||
f.deleteRecursively()
|
|
||||||
if (returnCode.toString() != "0") downloadFailedNotification()
|
if (returnCode.toString() != "0") downloadFailedNotification()
|
||||||
stopForeground(true)
|
else downloadSucceededNotification()
|
||||||
stopService(Intent(this@DownloadService, DownloadService::class.java))
|
onDestroy()
|
||||||
}, {
|
}, {
|
||||||
// CALLED WHEN SESSION PRINTS LOGS
|
// CALLED WHEN SESSION PRINTS LOGS
|
||||||
Log.e(TAG, it.message.toString())
|
Log.e(TAG, it.message.toString())
|
||||||
@ -250,10 +274,12 @@ class DownloadService : Service() {
|
|||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
try {
|
try {
|
||||||
unregisterReceiver(onDownloadComplete)
|
unregisterReceiver(onDownloadComplete)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) { }
|
||||||
}
|
|
||||||
IS_DOWNLOAD_RUNNING = false
|
IS_DOWNLOAD_RUNNING = false
|
||||||
Log.d(TAG, "dl finished!")
|
Log.d(TAG, "dl finished!")
|
||||||
|
stopForeground(true)
|
||||||
|
stopService(Intent(this@DownloadService, DownloadService::class.java))
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ class DownloadDialog : DialogFragment() {
|
|||||||
var audioUrl = arrayListOf<String>()
|
var audioUrl = arrayListOf<String>()
|
||||||
var selectedVideo = 0
|
var selectedVideo = 0
|
||||||
var selectedAudio = 0
|
var selectedAudio = 0
|
||||||
var extension = ".mkv"
|
var extension = ".mp4"
|
||||||
var duration = 0
|
var duration = 0
|
||||||
private lateinit var videoId: String
|
private lateinit var videoId: String
|
||||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||||
|
@ -30,18 +30,21 @@
|
|||||||
android:id="@+id/radioGp"
|
android:id="@+id/radioGp"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_margin="8dp">
|
android:orientation="horizontal"
|
||||||
|
android:layout_margin="8dp"
|
||||||
|
android:visibility="gone" >
|
||||||
|
|
||||||
<RadioButton
|
<RadioButton
|
||||||
android:id="@+id/mkv"
|
android:id="@+id/mkv"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text=".mkv"
|
android:text=".mkv"
|
||||||
|
android:layout_marginRight="16dp"
|
||||||
android:checked="true" />
|
android:checked="true" />
|
||||||
|
|
||||||
<RadioButton
|
<RadioButton
|
||||||
android:id="@+id/mp4"
|
android:id="@+id/mp4"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text=".mp4" />
|
android:text=".mp4" />
|
||||||
</RadioGroup>
|
</RadioGroup>
|
||||||
|
Loading…
Reference in New Issue
Block a user