feat:视频链第三批——相册定位器+五段编排器:
DouyinSavedVideoLocator(媒体层): - MediaStore 定位「保存本地」产物:时间窗(date_modified>=保存时刻)+命名前缀 (share_%.mp4)双条件——只靠前缀会误取用户历史 share_* 文件,时间窗是硬条件 - 候选含 uri/displayName/size/duration/relativePath,date_modified 降序最新优先 - 查询异常(权限拒绝)返回空表不假装成功 DouyinVideoChain(媒体层编排器): - 五段串联:LOCATE→IMPORT→VALIDATE→AUDIO→ASR,失败短路 stage 归因 - ASR 失败降级不短路(transcriptFailure 记录,标题链 OCR 兜底) - 音频临时文件转写后即删(成功 Outcome.audioPath 恒空) VideoTitleComposer.buildEvidence 伴生工厂: - 纯函数三字段组装(从 DouyinVideoChain 薄转发收敛,避免 Context 依赖扩散) 测试修正教训:JVM 单测不可构造 android.content.Context——lazy 占位在构造 传参时即求值、强转赋值即检查,两类代理均不可行;纯函数应挂在无依赖的 伴生对象上(buildEvidence 迁移),编排 run() 留真机 instrumentation 覆盖 验证:369 项 JVM 全过(+9);assembleDebug 通过 APK: F21FA949301D7DDDA6A909CAA6B008FDD6023F7D87BC989CF45150AE08E13350 Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
@@ -32,6 +32,20 @@ object VideoTitleComposer {
|
|||||||
var failureReason: String = ""
|
var failureReason: String = ""
|
||||||
private set
|
private set
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标题证据组装工厂(编排层便捷入口):按「转录优先」口径填三字段。
|
||||||
|
* 纯函数不触依赖;DouyinVideoChain 等编排层用它把 Outcome 转成证据。
|
||||||
|
*/
|
||||||
|
fun buildEvidence(
|
||||||
|
transcriptText: String,
|
||||||
|
hotListTitle: String,
|
||||||
|
ocrDetailText: String,
|
||||||
|
): VideoEvidence = VideoEvidence(
|
||||||
|
transcriptText = transcriptText,
|
||||||
|
hotListTitle = hotListTitle,
|
||||||
|
ocrDetailText = ocrDetailText,
|
||||||
|
)
|
||||||
|
|
||||||
/** 生成标题候选并核验;null=模型失败或全不合格(调用方降级/跳过)。 */
|
/** 生成标题候选并核验;null=模型失败或全不合格(调用方降级/跳过)。 */
|
||||||
suspend fun composeTitles(
|
suspend fun composeTitles(
|
||||||
evidence: VideoEvidence,
|
evidence: VideoEvidence,
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package com.haorui.app.crossplatform.media
|
||||||
|
|
||||||
|
import android.content.ContentUris
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import android.provider.MediaStore
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 抖音「保存本地」相册定位器(2026-11-19,douyin-save-local-file-shape 取证口径)。
|
||||||
|
*
|
||||||
|
* 职责:在 MediaStore 中定位抖音刚保存的视频条目,交给 [SourceMediaProvider] 导入。
|
||||||
|
* 只做**查询**(把 MediaStore 行转成 content URI + 候选清单),不做搬运——
|
||||||
|
* 搬运/去重/校验仍是 SourceMediaProvider 与 VideoFileValidator 的职责。
|
||||||
|
*
|
||||||
|
* 定位双条件(取证实锤):
|
||||||
|
* 1. 时间窗:`date_modified >= savedSinceMs`(保存动作发生时刻)——排除用户历史视频;
|
||||||
|
* 2. 命名前缀:`_display_name LIKE 'share_%.mp4'`——抖音保存命名
|
||||||
|
* `share_<32位MD5><13位毫秒时间戳>.mp4`(DCIM/Camera);
|
||||||
|
* 相册可能残留用户更早的 share_* 文件,只靠前缀会误取——时间窗是硬条件。
|
||||||
|
*/
|
||||||
|
class DouyinSavedVideoLocator(private val context: Context) {
|
||||||
|
|
||||||
|
/** 候选条目(按 date_modified 降序——最新优先)。 */
|
||||||
|
data class Candidate(
|
||||||
|
val uri: Uri,
|
||||||
|
val displayName: String,
|
||||||
|
val sizeBytes: Long,
|
||||||
|
val dateModifiedSec: Long,
|
||||||
|
val durationMs: Long,
|
||||||
|
val relativePath: String?,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询保存时间窗之后的 share_*.mp4 候选。
|
||||||
|
* @param savedSinceMs 保存动作开始时刻(epoch ms;与 date_modified 秒比较时取 floor)
|
||||||
|
* @return 候选列表(可能为空=未找到;调用方提示用户/降级)。
|
||||||
|
*/
|
||||||
|
fun findSavedVideos(savedSinceMs: Long): List<Candidate> {
|
||||||
|
val savedSinceSec = savedSinceMs / 1000
|
||||||
|
val projection = arrayOf(
|
||||||
|
MediaStore.Video.Media._ID,
|
||||||
|
MediaStore.Video.Media.DISPLAY_NAME,
|
||||||
|
MediaStore.Video.Media.SIZE,
|
||||||
|
MediaStore.Video.Media.DATE_MODIFIED,
|
||||||
|
MediaStore.Video.Media.DURATION,
|
||||||
|
MediaStore.Video.Media.RELATIVE_PATH,
|
||||||
|
)
|
||||||
|
val selection = "${MediaStore.Video.Media.DISPLAY_NAME} LIKE ? AND " +
|
||||||
|
"${MediaStore.Video.Media.DATE_MODIFIED} >= ?"
|
||||||
|
val selectionArgs = arrayOf("share_%.mp4", savedSinceSec.toString())
|
||||||
|
val sortOrder = "${MediaStore.Video.Media.DATE_MODIFIED} DESC"
|
||||||
|
val out = mutableListOf<Candidate>()
|
||||||
|
try {
|
||||||
|
context.contentResolver.query(
|
||||||
|
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
|
||||||
|
projection, selection, selectionArgs, sortOrder,
|
||||||
|
)?.use { cursor ->
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
val id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID))
|
||||||
|
val name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)) ?: ""
|
||||||
|
val size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE))
|
||||||
|
val dateSec = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_MODIFIED))
|
||||||
|
val durationMs = try {
|
||||||
|
cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
0L // DURATION 个别机型可能缺列,0=未知(VideoFileValidator 会再校验)
|
||||||
|
}
|
||||||
|
val relPath = try {
|
||||||
|
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.RELATIVE_PATH))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
out += Candidate(
|
||||||
|
uri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id),
|
||||||
|
displayName = name,
|
||||||
|
sizeBytes = size,
|
||||||
|
dateModifiedSec = dateSec,
|
||||||
|
durationMs = durationMs,
|
||||||
|
relativePath = relPath,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
// 查询失败(权限拒绝等)返回空——调用方按「未找到」处理,不假装成功
|
||||||
|
return emptyList()
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 首个候选的 content URI(时间窗内最新一条);无候选返回 null。 */
|
||||||
|
fun firstSavedUri(savedSinceMs: Long): Uri? = findSavedVideos(savedSinceMs).firstOrNull()?.uri
|
||||||
|
}
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package com.haorui.app.crossplatform.media
|
||||||
|
|
||||||
|
import com.haorui.app.crossplatform.compose.AsrTranscriber
|
||||||
|
import com.haorui.app.crossplatform.compose.VideoTitleComposer
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 抖音视频链编排器(2026-11-19):保存本地产物 → ASR 标题素材。
|
||||||
|
*
|
||||||
|
* 串联五段(各段职责独立,失败即短路并给出可归因原因):
|
||||||
|
* 相册定位(DouyinSavedVideoLocator)→ 私有目录导入(SourceMediaProvider)
|
||||||
|
* → 文件硬校验(VideoFileValidator)→ 音轨提取(AudioTrackExtractor)
|
||||||
|
* → ASR 转写(AsrTranscriber)→ 视频标题素材(VideoTitleComposer.VideoEvidence)
|
||||||
|
*
|
||||||
|
* 设计口径:
|
||||||
|
* - 任何一段失败返回 Failed,reason 归因到段名——任务侧按未知/跳过处理,不硬重试;
|
||||||
|
* - 转写失败降级:ASR 失败仍返回 success(transcriptText 空 + failureReason 记录),
|
||||||
|
* 标题链可走 OCR 降级路线(VideoTitleComposer 内置降级)——音频是增益不是硬依赖;
|
||||||
|
* - 音频临时文件转写后即删(素材生命周期与草稿核对口径一致);
|
||||||
|
* - 视频 SHA-256 返回给调用方(幂等键/去重联动)。
|
||||||
|
*/
|
||||||
|
class DouyinVideoChain(
|
||||||
|
private val mediaProvider: SourceMediaProvider,
|
||||||
|
private val locator: DouyinSavedVideoLocator,
|
||||||
|
private val logger: (String) -> Unit = {},
|
||||||
|
) {
|
||||||
|
|
||||||
|
/** 编排结果。 */
|
||||||
|
data class Outcome(
|
||||||
|
val ok: Boolean,
|
||||||
|
val stage: String = "",
|
||||||
|
val reason: String = "",
|
||||||
|
val videoPath: String = "",
|
||||||
|
val videoSha256: String = "",
|
||||||
|
val audioPath: String = "",
|
||||||
|
val transcriptText: String = "",
|
||||||
|
val transcriptFailure: String = "",
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从「保存本地」产物跑完整链到标题素材。
|
||||||
|
* @param savedSinceMs 保存动作开始时刻(epoch ms,定位时间窗起点)
|
||||||
|
* @param asrApiKey dashscope Key(复用 qwen 配置;空=跳过转写走 OCR 降级)
|
||||||
|
*/
|
||||||
|
fun run(
|
||||||
|
savedSinceMs: Long,
|
||||||
|
hotListTitle: String,
|
||||||
|
ocrDetailText: String,
|
||||||
|
asrApiKey: String,
|
||||||
|
asrWorkspaceId: String = "",
|
||||||
|
): Outcome {
|
||||||
|
// 1) 相册定位
|
||||||
|
val candidate = locator.findSavedVideos(savedSinceMs).firstOrNull()
|
||||||
|
?: return Outcome(false, stage = "LOCATE", reason = "时间窗内未找到 share_*.mp4(保存未发生或权限拒绝)")
|
||||||
|
logger("视频链 LOCATE:${candidate.displayName} ${candidate.sizeBytes / 1024}KB")
|
||||||
|
|
||||||
|
// 2) 私有目录导入(搬运+SHA-256+去重)
|
||||||
|
val imported = when (val r = mediaProvider.importFrom(candidate.uri, "douyin-saved")) {
|
||||||
|
is SourceMediaProvider.Result.Imported -> r
|
||||||
|
is SourceMediaProvider.Result.Rejected -> return Outcome(false, stage = "IMPORT", reason = r.reason)
|
||||||
|
}
|
||||||
|
logger("视频链 IMPORT:sha=${imported.sha256.take(12)}…")
|
||||||
|
|
||||||
|
// 3) 文件硬校验(MP4/双轨/时长窗口)
|
||||||
|
val check = VideoFileValidator.validate(imported.localPath)
|
||||||
|
if (!check.ok) {
|
||||||
|
return Outcome(false, stage = "VALIDATE", reason = check.reason, videoPath = imported.localPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4) 音轨提取
|
||||||
|
val audioPath = imported.localPath.removeSuffix(".mp4") + ".m4a"
|
||||||
|
val audio = AudioTrackExtractor.extract(imported.localPath, audioPath)
|
||||||
|
if (!audio.ok) {
|
||||||
|
return Outcome(
|
||||||
|
false, stage = "AUDIO", reason = audio.reason,
|
||||||
|
videoPath = imported.localPath, videoSha256 = imported.sha256,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
logger("视频链 AUDIO:${audio.sizeBytes / 1024}KB ${audio.durationMs}ms")
|
||||||
|
|
||||||
|
// 5) ASR 转写(失败降级不短路——标题链有 OCR 兜底)
|
||||||
|
var transcript = ""
|
||||||
|
var transcriptFailure = ""
|
||||||
|
if (asrApiKey.isNotBlank()) {
|
||||||
|
val bytes = try {
|
||||||
|
java.io.File(audioPath).readBytes()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
if (bytes == null) {
|
||||||
|
transcriptFailure = "音频文件读取失败"
|
||||||
|
} else {
|
||||||
|
val result = AsrTranscriber.transcribe(bytes, "m4a", asrApiKey, asrWorkspaceId)
|
||||||
|
if (result.text != null) {
|
||||||
|
transcript = result.text
|
||||||
|
logger("视频链 ASR:转写 ${result.text.length} 字")
|
||||||
|
} else {
|
||||||
|
transcriptFailure = result.failureReason
|
||||||
|
logger("视频链 ASR 失败(降级 OCR 路线):${result.failureReason}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
transcriptFailure = "ASR Key 未配置(走 OCR 降级)"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 音频临时文件转写后即删(无论成败——素材生命周期口径)
|
||||||
|
runCatching { java.io.File(audioPath).delete() }
|
||||||
|
|
||||||
|
// 6) 标题素材组装(VideoTitleComposer 消费)
|
||||||
|
return Outcome(
|
||||||
|
ok = true,
|
||||||
|
stage = "DONE",
|
||||||
|
videoPath = imported.localPath,
|
||||||
|
videoSha256 = imported.sha256,
|
||||||
|
audioPath = "",
|
||||||
|
transcriptText = transcript,
|
||||||
|
transcriptFailure = transcriptFailure,
|
||||||
|
).also {
|
||||||
|
logger("视频链 DONE:视频 ${imported.localPath};转录 ${transcript.length} 字" +
|
||||||
|
(if (transcriptFailure.isNotBlank()) ";转录降级[$transcriptFailure]" else ""))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
package com.haorui.app.crossplatform.media
|
||||||
|
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertNotNull
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 抖音视频链编排器测试(纯 JVM)。
|
||||||
|
*
|
||||||
|
* 范围口径:DouyinVideoChain.run() 依赖真实 Context(MediaStore 查询 + contentResolver
|
||||||
|
* 搬运),JVM 单测无法构造——该方法的编排顺序/短路归因由真机 instrumentation 覆盖
|
||||||
|
* (与 M5 MediaCases 同款模式)。本文件测 JVM 可执行的部分:
|
||||||
|
* - Outcome 段位契约(stage 语义/短路字段/音频即删语义);
|
||||||
|
* - buildTitleEvidence 组装契约(转录优先/OCR 降级字段传递)。
|
||||||
|
* 五段各自的核心逻辑已有独立测试:AudioTrackExtractor(真机)、AsrTranscriber(11 项)、
|
||||||
|
* VideoTitleComposer(13 项)、SourceMediaProvider(M5 22 项)、VideoFileValidator(M5)。
|
||||||
|
*/
|
||||||
|
class DouyinVideoChainTest {
|
||||||
|
|
||||||
|
// ── Outcome 段位契约 ─────────────────────────────
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `LOCATE失败短路_无后续字段`() {
|
||||||
|
val o = DouyinVideoChain.Outcome(ok = false, stage = "LOCATE", reason = "未找到")
|
||||||
|
assertFalse(o.ok)
|
||||||
|
assertEquals("LOCATE", o.stage)
|
||||||
|
assertTrue(o.videoPath.isEmpty())
|
||||||
|
assertTrue(o.videoSha256.isEmpty())
|
||||||
|
assertTrue(o.audioPath.isEmpty())
|
||||||
|
assertTrue(o.transcriptText.isEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `IMPORT失败带不出sha`() {
|
||||||
|
val o = DouyinVideoChain.Outcome(ok = false, stage = "IMPORT", reason = "来源不可读")
|
||||||
|
assertTrue(o.videoSha256.isEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `VALIDATE失败保留视频路径但无sha`() {
|
||||||
|
// 编排实现:VALIDATE 短路时 videoPath 已知(导入完成),sha 未返回
|
||||||
|
val o = DouyinVideoChain.Outcome(
|
||||||
|
ok = false, stage = "VALIDATE", reason = "无音轨",
|
||||||
|
videoPath = "/data/douyin-saved-123.mp4",
|
||||||
|
)
|
||||||
|
assertFalse(o.videoPath.isEmpty())
|
||||||
|
assertTrue(o.videoSha256.isEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `AUDIO失败带视频路径与sha`() {
|
||||||
|
val o = DouyinVideoChain.Outcome(
|
||||||
|
ok = false, stage = "AUDIO", reason = "无音轨",
|
||||||
|
videoPath = "/data/x.mp4", videoSha256 = "abc123",
|
||||||
|
)
|
||||||
|
assertFalse(o.ok)
|
||||||
|
assertFalse(o.videoSha256.isEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `成功Outcome音频即删语义`() {
|
||||||
|
// 成功 Outcome 的 audioPath 恒空(临时文件转写后删除)
|
||||||
|
val o = DouyinVideoChain.Outcome(
|
||||||
|
ok = true, stage = "DONE",
|
||||||
|
videoPath = "/data/x.mp4", videoSha256 = "abc",
|
||||||
|
transcriptText = "转写文本",
|
||||||
|
)
|
||||||
|
assertTrue(o.ok)
|
||||||
|
assertTrue(o.audioPath.isEmpty())
|
||||||
|
assertFalse(o.transcriptText.isEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `转写降级不占成功位`() {
|
||||||
|
// ASR 失败仍是 ok=true(标题链 OCR 兜底),transcriptFailure 记录原因
|
||||||
|
val o = DouyinVideoChain.Outcome(
|
||||||
|
ok = true, stage = "DONE",
|
||||||
|
videoPath = "/data/x.mp4", videoSha256 = "abc",
|
||||||
|
transcriptText = "", transcriptFailure = "转写被拒绝:Invalid API-key",
|
||||||
|
)
|
||||||
|
assertTrue(o.ok)
|
||||||
|
assertTrue(o.transcriptText.isEmpty())
|
||||||
|
assertTrue(o.transcriptFailure.contains("Invalid API-key"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── 标题证据组装契约(逻辑在 VideoTitleComposer.buildEvidence,编排层转发) ──
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `标题证据组装_三字段传递`() {
|
||||||
|
val evidence = com.haorui.app.crossplatform.compose.VideoTitleComposer.buildEvidence(
|
||||||
|
transcriptText = "峰会现场领导人合影", hotListTitle = "APEC峰会开幕", ocrDetailText = "OCR正文",
|
||||||
|
)
|
||||||
|
assertEquals("峰会现场领导人合影", evidence.transcriptText)
|
||||||
|
assertEquals("APEC峰会开幕", evidence.hotListTitle)
|
||||||
|
assertEquals("OCR正文", evidence.ocrDetailText)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `标题证据组装_转录为空OCR兜底字段仍传递`() {
|
||||||
|
val evidence = com.haorui.app.crossplatform.compose.VideoTitleComposer.buildEvidence(
|
||||||
|
transcriptText = "", hotListTitle = "热榜", ocrDetailText = "OCR降级文本",
|
||||||
|
)
|
||||||
|
assertEquals("", evidence.transcriptText)
|
||||||
|
assertEquals("OCR降级文本", evidence.ocrDetailText)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `标题证据组装_转录与热榜同时存在`() {
|
||||||
|
val evidence = com.haorui.app.crossplatform.compose.VideoTitleComposer.buildEvidence(
|
||||||
|
transcriptText = "音频内容", hotListTitle = "热榜词条标题", ocrDetailText = "",
|
||||||
|
)
|
||||||
|
assertEquals("音频内容", evidence.transcriptText)
|
||||||
|
assertEquals("热榜词条标题", evidence.hotListTitle)
|
||||||
|
assertEquals("", evidence.ocrDetailText)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user