implemented saving subs in a file

This commit is contained in:
deniscerri 2023-04-14 00:24:51 +02:00
parent ee6eef64d4
commit 6fb8f035fe
No known key found for this signature in database
GPG key ID: 95C43D517D830350
9 changed files with 136 additions and 5 deletions

View file

@ -4,5 +4,7 @@ data class VideoPreferences (
var embedSubs: Boolean = true, var embedSubs: Boolean = true,
var addChapters: Boolean = true, var addChapters: Boolean = true,
var splitByChapters: Boolean = false, var splitByChapters: Boolean = false,
var sponsorBlockFilters: ArrayList<String> var sponsorBlockFilters: ArrayList<String>,
var writeSubs: Boolean = false,
var subsLanguages: String = "en.*,.*-orig"
) )

View file

@ -108,6 +108,7 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
fun createDownloadItemFromResult(resultItem: ResultItem, type: Type) : DownloadItem { fun createDownloadItemFromResult(resultItem: ResultItem, type: Type) : DownloadItem {
val embedSubs = sharedPreferences.getBoolean("embed_subtitles", false) val embedSubs = sharedPreferences.getBoolean("embed_subtitles", false)
val saveSubs = sharedPreferences.getBoolean("write_subtitles", false)
val addChapters = sharedPreferences.getBoolean("add_chapters", false) val addChapters = sharedPreferences.getBoolean("add_chapters", false)
val saveThumb = sharedPreferences.getBoolean("write_thumbnail", false) val saveThumb = sharedPreferences.getBoolean("write_thumbnail", false)
val embedThumb = sharedPreferences.getBoolean("embed_thumbnail", false) val embedThumb = sharedPreferences.getBoolean("embed_thumbnail", false)
@ -122,7 +123,7 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
val sponsorblock = sharedPreferences.getStringSet("sponsorblock_filters", emptySet()) val sponsorblock = sharedPreferences.getStringSet("sponsorblock_filters", emptySet())
val audioPreferences = AudioPreferences(embedThumb, false, ArrayList(sponsorblock!!)) val audioPreferences = AudioPreferences(embedThumb, false, ArrayList(sponsorblock!!))
val videoPreferences = VideoPreferences(embedSubs, addChapters, false, ArrayList(sponsorblock)) val videoPreferences = VideoPreferences(embedSubs, addChapters, false, ArrayList(sponsorblock), saveSubs)
return DownloadItem(0, return DownloadItem(0,
resultItem.url, resultItem.url,
@ -150,6 +151,7 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
fun createDownloadItemFromHistory(historyItem: HistoryItem) : DownloadItem { fun createDownloadItemFromHistory(historyItem: HistoryItem) : DownloadItem {
val embedSubs = sharedPreferences.getBoolean("embed_subtitles", false) val embedSubs = sharedPreferences.getBoolean("embed_subtitles", false)
val saveSubs = sharedPreferences.getBoolean("write_subtitles", false)
val addChapters = sharedPreferences.getBoolean("add_chapters", false) val addChapters = sharedPreferences.getBoolean("add_chapters", false)
val saveThumb = sharedPreferences.getBoolean("write_thumbnail", false) val saveThumb = sharedPreferences.getBoolean("write_thumbnail", false)
val embedThumb = sharedPreferences.getBoolean("embed_thumbnail", false) val embedThumb = sharedPreferences.getBoolean("embed_thumbnail", false)
@ -164,7 +166,7 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
val sponsorblock = sharedPreferences.getStringSet("sponsorblock_filters", emptySet()) val sponsorblock = sharedPreferences.getStringSet("sponsorblock_filters", emptySet())
val audioPreferences = AudioPreferences(embedThumb, false, ArrayList(sponsorblock!!)) val audioPreferences = AudioPreferences(embedThumb, false, ArrayList(sponsorblock!!))
val videoPreferences = VideoPreferences(embedSubs, addChapters, false, ArrayList(sponsorblock)) val videoPreferences = VideoPreferences(embedSubs, addChapters, false, ArrayList(sponsorblock), saveSubs)
return DownloadItem(0, return DownloadItem(0,
historyItem.url, historyItem.url,

View file

@ -1,19 +1,23 @@
package com.deniscerri.ytdlnis.ui.downloadcard package com.deniscerri.ytdlnis.ui.downloadcard
import android.app.Activity import android.app.Activity
import android.content.ClipboardManager
import android.content.DialogInterface import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
import android.view.Gravity
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.* import android.widget.*
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
@ -303,6 +307,60 @@ class DownloadVideoFragment(private val resultItem: ResultItem, private var curr
} }
} }
val saveSubtitles = view.findViewById<Chip>(R.id.save_subtitles)
val subtitleLanguages = view.findViewById<Chip>(R.id.subtitle_languages)
if (downloadItem.videoPreferences.writeSubs) {
saveSubtitles.isChecked = true
subtitleLanguages.visibility = View.VISIBLE
}
saveSubtitles.setOnCheckedChangeListener { compoundButton, b ->
if (saveSubtitles.isChecked) subtitleLanguages.visibility = View.VISIBLE
else subtitleLanguages.visibility = View.GONE
downloadItem.videoPreferences.writeSubs = saveSubtitles.isChecked
}
subtitleLanguages.setOnClickListener {
val builder = MaterialAlertDialogBuilder(requireContext())
builder.setTitle(getString(R.string.subtitle_languages))
val inputLayout = layoutInflater.inflate(R.layout.textinput, null)
val editText = inputLayout.findViewById<EditText>(R.id.url_edittext)
editText.setHint(R.string.subtitle_languages)
editText.setText(downloadItem.videoPreferences.subsLanguages)
editText.setSelection(editText.text.length)
builder.setView(inputLayout)
builder.setPositiveButton(
getString(R.string.ok)
) { dialog: DialogInterface?, which: Int ->
downloadItem.videoPreferences.subsLanguages = editText.text.toString()
}
// handle the negative button of the alert dialog
builder.setNegativeButton(
getString(R.string.cancel)
) { dialog: DialogInterface?, which: Int -> }
builder.setNeutralButton("?") { dialog: DialogInterface?, which: Int ->
val browserIntent =
Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/yt-dlp/yt-dlp#subtitle-options"))
startActivity(browserIntent)
}
val dialog = builder.create()
editText.doOnTextChanged { text, start, before, count ->
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = editText.text.isNotEmpty()
}
dialog.show()
val imm = context?.getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
editText!!.postDelayed({
editText.requestFocus()
imm.showSoftInput(editText, 0)
}, 300)
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = editText.text.isNotEmpty()
dialog.getButton(AlertDialog.BUTTON_NEUTRAL).gravity = Gravity.START
}
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }

View file

@ -55,6 +55,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
private var restrictFilenames: SwitchPreferenceCompat? = null private var restrictFilenames: SwitchPreferenceCompat? = null
private var mtime: SwitchPreferenceCompat? = null private var mtime: SwitchPreferenceCompat? = null
private var embedSubtitles: SwitchPreferenceCompat? = null private var embedSubtitles: SwitchPreferenceCompat? = null
private var writeSubtitles: SwitchPreferenceCompat? = null
private var embedThumbnail: SwitchPreferenceCompat? = null private var embedThumbnail: SwitchPreferenceCompat? = null
private var addChapters: SwitchPreferenceCompat? = null private var addChapters: SwitchPreferenceCompat? = null
private var writeThumbnail: SwitchPreferenceCompat? = null private var writeThumbnail: SwitchPreferenceCompat? = null
@ -117,6 +118,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
sponsorblockFilters = findPreference("sponsorblock_filters") sponsorblockFilters = findPreference("sponsorblock_filters")
mtime = findPreference("mtime") mtime = findPreference("mtime")
embedSubtitles = findPreference("embed_subtitles") embedSubtitles = findPreference("embed_subtitles")
writeSubtitles = findPreference("write_subtitles")
filenameTemplate = findPreference("file_name_template") filenameTemplate = findPreference("file_name_template")
restrictFilenames = findPreference("restrict_filenames") restrictFilenames = findPreference("restrict_filenames")
embedThumbnail = findPreference("embed_thumbnail") embedThumbnail = findPreference("embed_thumbnail")
@ -182,6 +184,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
editor.putBoolean("restrict_filenames", restrictFilenames!!.isChecked) editor.putBoolean("restrict_filenames", restrictFilenames!!.isChecked)
editor.putBoolean("mtime", mtime!!.isChecked) editor.putBoolean("mtime", mtime!!.isChecked)
editor.putBoolean("embed_subtitles", embedSubtitles!!.isChecked) editor.putBoolean("embed_subtitles", embedSubtitles!!.isChecked)
editor.putBoolean("write_subtitles", writeSubtitles!!.isChecked)
editor.putBoolean("embed_thumbnail", embedThumbnail!!.isChecked) editor.putBoolean("embed_thumbnail", embedThumbnail!!.isChecked)
editor.putBoolean("add_chapters", addChapters!!.isChecked) editor.putBoolean("add_chapters", addChapters!!.isChecked)
editor.putBoolean("write_thumbnail", writeThumbnail!!.isChecked) editor.putBoolean("write_thumbnail", writeThumbnail!!.isChecked)
@ -391,6 +394,13 @@ class SettingsFragment : PreferenceFragmentCompat() {
editor.apply() editor.apply()
true true
} }
writeSubtitles!!.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any ->
val embed = newValue as Boolean
editor.putBoolean("write_subtitles", embed)
editor.apply()
true
}
embedThumbnail!!.onPreferenceChangeListener = embedThumbnail!!.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any -> Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any ->
val embed = newValue as Boolean val embed = newValue as Boolean

View file

@ -230,6 +230,14 @@ class DownloadWorker(
} }
} }
if (downloadItem.videoPreferences.writeSubs){
request.addOption("--write-subs")
request.addOption("--write-auto-subs")
request.addOption("--sub-format", "str/ass/best")
request.addOption("--convert-subtitles", "srt")
request.addOption("--sub-langs", downloadItem.videoPreferences.subsLanguages)
}
if (downloadItem.videoPreferences.splitByChapters && downloadItem.downloadSections.isBlank()){ if (downloadItem.videoPreferences.splitByChapters && downloadItem.downloadSections.isBlank()){
request.addOption("--split-chapters") request.addOption("--split-chapters")
request.addOption("-P", tempFileDir.absolutePath) request.addOption("-P", tempFileDir.absolutePath)
@ -267,7 +275,7 @@ class DownloadWorker(
} }
YoutubeDL.getInstance().execute(request, downloadItem.id.toString()){ progress, _, line -> YoutubeDL.getInstance().execute(request, downloadItem.id.toString()){ progress, _, line ->
setProgressAsync(workDataOf("progress" to progress.toInt(), "output" to line, "id" to downloadItem.id, "log" to logDownloads)) setProgressAsync(workDataOf("progress" to progress.toInt(), "output" to line.chunked(5000).first().toString(), "id" to downloadItem.id, "log" to logDownloads))
val title: String = downloadItem.title val title: String = downloadItem.title
notificationUtil.updateDownloadNotification( notificationUtil.updateDownloadNotification(
downloadItem.id.toInt(), downloadItem.id.toInt(),

View file

@ -0,0 +1,5 @@
<vector android:height="24dp"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="?android:colorAccent" android:pathData="M19,4L5,4c-1.11,0 -2,0.9 -2,2v12c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,6c0,-1.1 -0.9,-2 -2,-2zM11,11L9.5,11v-0.5h-2v3h2L9.5,13L11,13v1c0,0.55 -0.45,1 -1,1L7,15c-0.55,0 -1,-0.45 -1,-1v-4c0,-0.55 0.45,-1 1,-1h3c0.55,0 1,0.45 1,1v1zM18,11h-1.5v-0.5h-2v3h2L16.5,13L18,13v1c0,0.55 -0.45,1 -1,1h-3c-0.55,0 -1,-0.45 -1,-1v-4c0,-0.55 0.45,-1 1,-1h3c0.55,0 1,0.45 1,1v1z"/>
</vector>

View file

@ -215,6 +215,41 @@
</HorizontalScrollView> </HorizontalScrollView>
<HorizontalScrollView
android:scrollbars="none"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.chip.ChipGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleLine="true">
<com.google.android.material.chip.Chip
android:id="@+id/save_subtitles"
style="@style/Widget.Material3.Chip.Filter.Elevated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/save_subs"
android:minWidth="30dp"
app:cornerRadius="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.chip.Chip
android:id="@+id/subtitle_languages"
style="@style/Widget.Material3.Chip.Assist.Elevated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:checked="false"
android:text="@string/subtitle_languages"/>
</com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>
</LinearLayout> </LinearLayout>

View file

@ -208,4 +208,7 @@
<string name="download_over_metered_networks_summary">Let the download worker work on any network type. If disabled, it will wait for an unmetered network to start the downloads</string> <string name="download_over_metered_networks_summary">Let the download worker work on any network type. If disabled, it will wait for an unmetered network to start the downloads</string>
<string name="metered_network_download_start_info">Download worker will start after you are connected to an unmetered network</string> <string name="metered_network_download_start_info">Download worker will start after you are connected to an unmetered network</string>
<string name="invert_selected">Invert Selected</string> <string name="invert_selected">Invert Selected</string>
<string name="save_subs">Save Subtitles</string>
<string name="save_subs_desc">Write subtitle file</string>
<string name="subtitle_languages">Subtitle languages</string>
</resources> </resources>

View file

@ -179,6 +179,14 @@
app:summary="@string/embed_subs_summary" app:summary="@string/embed_subs_summary"
app:title="@string/embed_subtitles" /> app:title="@string/embed_subtitles" />
<SwitchPreferenceCompat
android:widgetLayout="@layout/preferece_material_switch"
app:defaultValue="false"
app:icon="@drawable/ic_subtitles_alternative"
app:key="write_subtitles"
app:summary="@string/save_subs_desc"
app:title="@string/save_subs" />
<SwitchPreferenceCompat <SwitchPreferenceCompat
android:widgetLayout="@layout/preferece_material_switch" android:widgetLayout="@layout/preferece_material_switch"
app:defaultValue="true" app:defaultValue="true"