remove bloat and cc from subtitles

This commit is contained in:
LagradOst 2022-06-03 18:41:21 +02:00
parent b7b5c54c72
commit 3656c7ee11
5 changed files with 197 additions and 148 deletions

View File

@ -38,6 +38,7 @@ class CustomDecoder : SubtitleDecoder {
private const val TAG = "CustomDecoder" private const val TAG = "CustomDecoder"
private var overrideEncoding: String? = null private var overrideEncoding: String? = null
var regexSubtitlesToRemoveCaptions = false var regexSubtitlesToRemoveCaptions = false
var regexSubtitlesToRemoveBloat = false
val bloatRegex = val bloatRegex =
listOf( listOf(
Regex( Regex(
@ -151,13 +152,15 @@ class CustomDecoder : SubtitleDecoder {
) )
realDecoder?.let { decoder -> realDecoder?.let { decoder ->
decoder.dequeueInputBuffer()?.let { buff -> decoder.dequeueInputBuffer()?.let { buff ->
if (regexSubtitlesToRemoveCaptions && decoder::class.java != SsaDecoder::class.java) { if (decoder::class.java != SsaDecoder::class.java) {
captionRegex.forEach { rgx -> if (regexSubtitlesToRemoveCaptions)
str = str.replace(rgx, "\n") captionRegex.forEach { rgx ->
} str = str.replace(rgx, "\n")
bloatRegex.forEach { rgx -> }
str = str.replace(rgx, "\n") if (regexSubtitlesToRemoveBloat)
} bloatRegex.forEach { rgx ->
str = str.replace(rgx, "\n")
}
} }
buff.data = ByteBuffer.wrap(str.toByteArray(charset(UTF_8))) buff.data = ByteBuffer.wrap(str.toByteArray(charset(UTF_8)))
@ -177,13 +180,15 @@ class CustomDecoder : SubtitleDecoder {
if (!inputString.isNullOrBlank()) { if (!inputString.isNullOrBlank()) {
var str: String = inputString var str: String = inputString
if (regexSubtitlesToRemoveCaptions && realDecoder!!::class.java != SsaDecoder::class.java) { if (realDecoder!!::class.java != SsaDecoder::class.java) {
captionRegex.forEach { rgx -> if (regexSubtitlesToRemoveCaptions)
str = str.replace(rgx, "\n") captionRegex.forEach { rgx ->
} str = str.replace(rgx, "\n")
bloatRegex.forEach { rgx -> }
str = str.replace(rgx, "\n") if (regexSubtitlesToRemoveBloat)
} bloatRegex.forEach { rgx ->
str = str.replace(rgx, "\n")
}
} }
inputBuffer.data = ByteBuffer.wrap(str.toByteArray(charset(UTF_8))) inputBuffer.data = ByteBuffer.wrap(str.toByteArray(charset(UTF_8)))
} }

View File

@ -9,6 +9,8 @@ import com.google.android.exoplayer2.ui.SubtitleView
import com.google.android.exoplayer2.util.MimeTypes import com.google.android.exoplayer2.util.MimeTypes
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import com.lagradost.cloudstream3.SubtitleFile import com.lagradost.cloudstream3.SubtitleFile
import com.lagradost.cloudstream3.ui.player.CustomDecoder.Companion.regexSubtitlesToRemoveBloat
import com.lagradost.cloudstream3.ui.player.CustomDecoder.Companion.regexSubtitlesToRemoveCaptions
import com.lagradost.cloudstream3.ui.subtitles.SaveCaptionStyle import com.lagradost.cloudstream3.ui.subtitles.SaveCaptionStyle
import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment.Companion.fromSaveToStyle import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment.Companion.fromSaveToStyle
import com.lagradost.cloudstream3.utils.UIHelper.toPx import com.lagradost.cloudstream3.utils.UIHelper.toPx
@ -109,6 +111,8 @@ class PlayerSubtitleHelper {
} }
fun setSubStyle(style: SaveCaptionStyle) { fun setSubStyle(style: SaveCaptionStyle) {
regexSubtitlesToRemoveBloat = style.removeBloat
regexSubtitlesToRemoveCaptions = style.removeCaptions
subtitleView?.context?.let { ctx -> subtitleView?.context?.let { ctx ->
subStyle = style subStyle = style
subtitleView?.setStyle(ctx.fromSaveToStyle(style)) subtitleView?.setStyle(ctx.fromSaveToStyle(style))

View File

@ -57,6 +57,8 @@ data class SaveCaptionStyle(
@JsonProperty("elevation") var elevation: Int, @JsonProperty("elevation") var elevation: Int,
/**in sp**/ /**in sp**/
@JsonProperty("fixedTextSize") var fixedTextSize: Float?, @JsonProperty("fixedTextSize") var fixedTextSize: Float?,
@JsonProperty("removeCaptions") var removeCaptions: Boolean = false,
@JsonProperty("removeBloat") var removeBloat: Boolean = true,
) )
const val DEF_SUBS_ELEVATION = 20 const val DEF_SUBS_ELEVATION = 20
@ -397,6 +399,15 @@ class SubtitlesFragment : Fragment() {
} }
} }
subtitles_remove_bloat?.isChecked = state.removeBloat
subtitles_remove_bloat?.setOnCheckedChangeListener { _, b ->
state.removeBloat = b
}
subtitles_remove_captions?.isChecked = state.removeCaptions
subtitles_remove_captions?.setOnCheckedChangeListener { _, b ->
state.removeCaptions = b
}
subs_font_size.setOnLongClickListener { _ -> subs_font_size.setOnLongClickListener { _ ->
state.fixedTextSize = null state.fixedTextSize = null
//textView.context.updateState() // font size not changed //textView.context.updateState() // font size not changed

View File

@ -1,195 +1,222 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/subs_root" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:id="@+id/subs_root"
android:layout_height="match_parent" android:layout_width="match_parent"
android:background="?attr/primaryBlackBackground"> android:layout_height="match_parent"
android:background="?attr/primaryBlackBackground">
<LinearLayout <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_rowWeight="1" android:orientation="vertical">
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp" <TextView
android:paddingStart="20dp" android:layout_width="match_parent"
android:paddingEnd="20dp" android:layout_height="wrap_content"
android:text="@string/subtitles_settings" android:layout_rowWeight="1"
android:textColor="?attr/textColor" android:layout_marginTop="20dp"
android:textSize="20sp" android:layout_marginBottom="10dp"
android:textStyle="bold" /> android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@string/subtitles_settings"
android:textColor="?attr/textColor"
android:textSize="20sp"
android:textStyle="bold" />
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="75sp"> android:layout_height="75sp">
<ImageView <ImageView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:contentDescription="@string/preview_background_img_des" android:contentDescription="@string/preview_background_img_des"
android:scaleType="centerCrop" android:scaleType="centerCrop"
android:src="@drawable/subtitles_preview_background" /> android:src="@drawable/subtitles_preview_background" />
<com.google.android.exoplayer2.ui.SubtitleView <com.google.android.exoplayer2.ui.SubtitleView
android:id="@+id/subtitle_text" android:id="@+id/subtitle_text"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="center" android:layout_gravity="center"
android:foregroundGravity="center" /> android:foregroundGravity="center" />
</FrameLayout> </FrameLayout>
<TextView <TextView
android:id="@+id/subs_font" android:id="@+id/subs_font"
style="@style/SettingsItem" style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusDown="@id/subs_font_size" android:nextFocusDown="@id/subs_font_size"
android:text="@string/subs_font" /> android:text="@string/subs_font" />
<TextView <TextView
android:id="@+id/subs_font_size" android:id="@+id/subs_font_size"
style="@style/SettingsItem" style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_font" android:nextFocusUp="@id/subs_font"
android:nextFocusDown="@id/subs_text_color" android:nextFocusDown="@id/subs_text_color"
android:text="@string/subs_font_size" /> android:text="@string/subs_font_size" />
<TextView <TextView
android:id="@+id/subs_text_color" android:id="@+id/subs_text_color"
style="@style/SettingsItem" style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_font_size" android:nextFocusUp="@id/subs_font_size"
android:nextFocusDown="@id/subs_outline_color" android:nextFocusDown="@id/subs_outline_color"
android:text="@string/subs_text_color" /> android:text="@string/subs_text_color" />
<TextView <TextView
android:id="@+id/subs_outline_color" android:id="@+id/subs_outline_color"
style="@style/SettingsItem" style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_text_color" android:nextFocusUp="@id/subs_text_color"
android:nextFocusDown="@id/subs_background_color" android:nextFocusDown="@id/subs_background_color"
android:text="@string/subs_outline_color" /> android:text="@string/subs_outline_color" />
<TextView <TextView
android:id="@+id/subs_background_color" android:id="@+id/subs_background_color"
style="@style/SettingsItem" style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_outline_color" android:nextFocusUp="@id/subs_outline_color"
android:nextFocusDown="@id/subs_window_color" android:nextFocusDown="@id/subs_window_color"
android:text="@string/subs_background_color" /> android:text="@string/subs_background_color" />
<TextView <TextView
android:id="@+id/subs_window_color" android:id="@+id/subs_window_color"
style="@style/SettingsItem" style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_background_color" android:nextFocusUp="@id/subs_background_color"
android:nextFocusDown="@id/subs_edge_type" android:nextFocusDown="@id/subs_edge_type"
android:text="@string/subs_window_color" /> android:text="@string/subs_window_color" />
<TextView <TextView
android:id="@+id/subs_edge_type" android:id="@+id/subs_edge_type"
style="@style/SettingsItem" style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_window_color" android:nextFocusUp="@id/subs_window_color"
android:nextFocusDown="@id/subs_subtitle_elevation" android:nextFocusDown="@id/subs_subtitle_elevation"
android:text="@string/subs_edge_type" /> android:text="@string/subs_edge_type" />
<TextView <TextView
android:id="@+id/subs_subtitle_elevation" android:id="@+id/subs_subtitle_elevation"
style="@style/SettingsItem" style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_edge_type" android:nextFocusUp="@id/subs_edge_type"
android:nextFocusDown="@id/subs_auto_select_language" android:nextFocusDown="@id/subs_auto_select_language"
android:text="@string/subs_subtitle_elevation" /> android:text="@string/subs_subtitle_elevation" />
<TextView <TextView
android:id="@+id/subs_auto_select_language" android:id="@+id/subs_auto_select_language"
style="@style/SettingsItem" style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_subtitle_elevation" android:nextFocusUp="@id/subs_subtitle_elevation"
android:nextFocusDown="@id/subs_download_languages" android:nextFocusDown="@id/subs_download_languages"
android:text="@string/subs_auto_select_language" /> android:text="@string/subs_auto_select_language" />
<TextView <TextView
android:id="@+id/subs_download_languages" android:id="@+id/subs_download_languages"
style="@style/SettingsItem" style="@style/SettingsItem"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_auto_select_language" android:nextFocusUp="@id/subs_auto_select_language"
android:nextFocusDown="@id/apply_btt" android:nextFocusDown="@id/subtitles_remove_bloat"
android:text="@string/subs_download_languages" /> android:text="@string/subs_download_languages" />
<com.google.android.material.switchmaterial.SwitchMaterial
android:nextFocusUp="@id/subs_download_languages"
android:nextFocusDown="@id/subtitles_remove_captions"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:fontFamily="@font/google_sans"
style="@style/SettingsItem"
app:drawableEndCompat="@null"
android:id="@+id/subtitles_remove_bloat"
android:text="@string/subtitles_remove_bloat"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.switchmaterial.SwitchMaterial
android:nextFocusUp="@id/subtitles_remove_bloat"
android:nextFocusDown="@id/apply_btt"
android:nextFocusLeft="@id/apply_btt"
android:nextFocusRight="@id/cancel_btt"
android:fontFamily="@font/google_sans"
style="@style/SettingsItem"
app:drawableEndCompat="@null"
android:id="@+id/subtitles_remove_captions"
android:text="@string/subtitles_remove_captions"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_rowWeight="1" android:layout_rowWeight="1"
android:gravity="center" android:gravity="center"
android:text="@string/subs_hold_to_reset_to_default" android:text="@string/subs_hold_to_reset_to_default"
android:textColor="?attr/textColor" android:textColor="?attr/textColor"
android:textSize="14sp" /> android:textSize="14sp" />
<TextView <TextView
android:id="@+id/subs_import_text" android:id="@+id/subs_import_text"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_rowWeight="1" android:layout_rowWeight="1"
android:gravity="center" android:gravity="center"
android:text="@string/subs_import_text" android:text="@string/subs_import_text"
android:textColor="?attr/textColor" android:textColor="?attr/textColor"
android:textSize="14sp" /> android:textSize="14sp" />
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="60dp" android:layout_height="60dp"
android:layout_gravity="bottom" android:layout_gravity="bottom"
android:gravity="bottom|end" android:gravity="bottom|end"
android:orientation="horizontal"> android:orientation="horizontal">
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/apply_btt" android:id="@+id/apply_btt"
style="@style/WhiteButton" style="@style/WhiteButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_gravity="center_vertical|end" android:layout_gravity="center_vertical|end"
android:nextFocusRight="@id/cancel_btt" android:nextFocusRight="@id/cancel_btt"
android:nextFocusUp="@id/subs_download_languages" android:nextFocusUp="@id/subtitles_remove_captions"
android:text="@string/sort_apply" android:text="@string/sort_apply"
android:visibility="visible"> android:visibility="visible">
<requestFocus /> <requestFocus />
</com.google.android.material.button.MaterialButton> </com.google.android.material.button.MaterialButton>
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/cancel_btt" android:id="@+id/cancel_btt"
style="@style/BlackButton" style="@style/BlackButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_gravity="center_vertical|end" android:layout_gravity="center_vertical|end"
android:nextFocusLeft="@id/apply_btt" android:nextFocusLeft="@id/apply_btt"
android:nextFocusUp="@id/subs_download_languages" android:nextFocusUp="@id/subtitles_remove_captions"
android:text="@string/sort_cancel" /> android:text="@string/sort_cancel" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>

View File

@ -523,4 +523,6 @@
<string name="title">Title</string> <string name="title">Title</string>
<string name="resolution">Resolution</string> <string name="resolution">Resolution</string>
<string name="error_invalid_id">Invalid id</string> <string name="error_invalid_id">Invalid id</string>
<string name="subtitles_remove_captions">Remove closed captions from subtitles</string>
<string name="subtitles_remove_bloat">Remove bloat from subtitles</string>
</resources> </resources>