-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
why
committed
Apr 16, 2024
1 parent
e4997cb
commit eafff0d
Showing
70 changed files
with
7,111 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/xiaoyv/bangumi/special/subtitle/SubtitleToolActivity.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,45 @@ | ||
package com.xiaoyv.bangumi.special.subtitle | ||
|
||
import android.view.MenuItem | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.annotation.CallSuper | ||
import com.xiaoyv.bangumi.databinding.ActivitySubtitleToolBinding | ||
import com.xiaoyv.blueprint.base.mvvm.normal.BaseViewModelActivity | ||
import com.xiaoyv.common.kts.initNavBack | ||
import com.xiaoyv.common.widget.dialog.AnimeLoadingDialog | ||
import com.xiaoyv.widget.callback.setOnFastLimitClickListener | ||
import com.xiaoyv.widget.dialog.UiDialog | ||
|
||
class SubtitleToolActivity : | ||
BaseViewModelActivity<ActivitySubtitleToolBinding, SubtitleToolViewModel>() { | ||
|
||
private val selectSubtitleLauncher = | ||
registerForActivityResult(ActivityResultContracts.GetContent()) { | ||
viewModel.loadSubtitleFromMedia(it ?: return@registerForActivityResult) | ||
} | ||
|
||
override fun initView() { | ||
binding.toolbar.initNavBack(this) | ||
} | ||
|
||
override fun initData() { | ||
|
||
} | ||
|
||
override fun initListener() { | ||
binding.btnSubtitle.setOnFastLimitClickListener { | ||
selectSubtitleLauncher.launch("*/*") | ||
} | ||
} | ||
|
||
override fun onCreateLoadingDialog(): UiDialog { | ||
return AnimeLoadingDialog(this) | ||
} | ||
|
||
@CallSuper | ||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
item.initNavBack(this) | ||
return super.onOptionsItemSelected(item) | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
app/src/main/java/com/xiaoyv/bangumi/special/subtitle/SubtitleToolViewModel.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,95 @@ | ||
package com.xiaoyv.bangumi.special.subtitle | ||
|
||
import android.net.Uri | ||
import androidx.media3.common.DataReader | ||
import androidx.media3.common.Format | ||
import androidx.media3.common.MediaItem | ||
import androidx.media3.common.MimeTypes | ||
import androidx.media3.common.util.UnstableApi | ||
import androidx.media3.extractor.DefaultExtractorInput | ||
import androidx.media3.extractor.DefaultExtractorsFactory | ||
import androidx.media3.extractor.ExtractorOutput | ||
import androidx.media3.extractor.PositionHolder | ||
import androidx.media3.extractor.mp4.Mp4Extractor | ||
import androidx.media3.extractor.text.DefaultSubtitleParserFactory | ||
import androidx.media3.extractor.text.SubtitleParser | ||
import com.xiaoyv.blueprint.base.mvvm.normal.BaseViewModel | ||
import com.xiaoyv.blueprint.kts.launchUI | ||
import com.xiaoyv.blueprint.kts.toJson | ||
import com.xiaoyv.common.kts.debugLog | ||
import com.xiaoyv.common.kts.inputStream | ||
import com.xiaoyv.widget.kts.errorMsg | ||
import com.xiaoyv.widget.kts.showToastCompat | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
@UnstableApi | ||
class SubtitleToolViewModel : BaseViewModel() { | ||
|
||
fun loadSubtitleFromMedia(uri: Uri) { | ||
launchUI(error = { | ||
it.printStackTrace() | ||
|
||
}) { | ||
// SubtitleExtractor | ||
withContext(Dispatchers.IO) { | ||
val parserFactory = DefaultSubtitleParserFactory() | ||
val create = parserFactory.create( | ||
Format.Builder() | ||
.setSampleMimeType(MimeTypes.TEXT_SSA) | ||
.build() | ||
) | ||
create.parse(uri.inputStream().readBytes(), SubtitleParser.OutputOptions.allCues()) { | ||
|
||
debugLog { it.toJson() } | ||
} | ||
// | ||
// val extractors = DefaultExtractorsFactory() | ||
// .setSubtitleParserFactory(parserFactory) | ||
// .createExtractors(uri, emptyMap()) | ||
// extractors.first() | ||
// val mp4Extractor = Mp4Extractor(parserFactory) | ||
// mp4Extractor.init(ExtractorOutput.PLACEHOLDER) | ||
// mp4Extractor.read(DefaultExtractorInput(DataReader), PositionHolder()) | ||
// mp4Extractor.getSeekPoints() | ||
// debugLog { "Extractors: ${extractors.size}" } | ||
} | ||
} | ||
} | ||
|
||
fun loadSubtitleFile(uri: Uri) { | ||
launchUI(error = { | ||
it.printStackTrace() | ||
|
||
showToastCompat(it.errorMsg) | ||
}) { | ||
/* withContext(Dispatchers.IO) { | ||
val fileName = uri.parseFileName(context.contentResolver) | ||
debugLog { "文件名:$fileName" } | ||
when { | ||
fileName.endsWith(".ass", true) -> { | ||
val parser = ASSParser() | ||
val subtitle = uri.inputStream().use { parser.parse(it, fileName) } | ||
debugLog { subtitle.toString() } | ||
FileIOUtils.writeFileFromString( | ||
PathUtils.getExternalAppFilesPath() + "/subtitle.ass", | ||
subtitle.toString() | ||
) | ||
} | ||
fileName.endsWith(".srt", true) -> { | ||
val parser = SRTParser() | ||
val subtitle = uri.inputStream().use { parser.parse(it, fileName) } | ||
debugLog { subtitle.toString() } | ||
} | ||
else -> throw IllegalArgumentException("请选择 .ass 或 .srt 字幕文件") | ||
} | ||
}*/ | ||
} | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:fitsSystemWindows="true"> | ||
|
||
<com.xiaoyv.common.widget.appbar.AnimeAppBarLayout | ||
android:id="@+id/app_bar" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:fitsSystemWindows="true" | ||
app:theme="@style/Theme.Bangumi.AppBarOverlay"> | ||
|
||
<com.xiaoyv.common.widget.appbar.AnimeToolbar | ||
android:id="@id/toolbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="?attr/actionBarSize" | ||
app:layout_collapseMode="pin" | ||
app:popupTheme="@style/Theme.Bangumi.PopupOverlay" | ||
app:title="字幕工具" /> | ||
|
||
</com.xiaoyv.common.widget.appbar.AnimeAppBarLayout> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
app:layout_behavior="@string/appbar_scrolling_view_behavior"> | ||
|
||
<com.xiaoyv.common.widget.button.AnimeButton | ||
android:id="@+id/btn_subtitle" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginHorizontal="@dimen/ui_layout_margin" | ||
android:layout_marginTop="@dimen/ui_layout_margin" | ||
android:paddingVertical="@dimen/ui_size_12" | ||
android:text="一键翻译字幕文件" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
</androidx.coordinatorlayout.widget.CoordinatorLayout> |
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
Oops, something went wrong.