From 7b68f9a8e6af9d4c6a9a4a2d7a43db15c35c994c Mon Sep 17 00:00:00 2001 From: felixncheng Date: Mon, 25 Nov 2024 10:40:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20media=E6=9C=8D=E5=8A=A1=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89=E5=8F=82=E6=95=B0=E5=92=8C?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E5=99=A8=20#2768?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: media服务支持自定义参数和过滤器 #2768 * feat: media服务支持自定义参数和过滤器 #2768 --- .../boot-job-worker/shell/media-process.sh | 44 ------------------- .../tencent/bkrepo/media/config/WebConfig.kt | 16 +++++++ .../bkrepo/media/service/StreamService.kt | 7 ++- .../bkrepo/media/service/TokenService.kt | 2 +- .../bkrepo/media/service/TranscodeService.kt | 3 +- .../media/stream/MediaArtifactFileConsumer.kt | 3 -- .../bkrepo/media/stream/TranscodeConfig.kt | 3 +- .../bkrepo/media/stream/TranscodeParam.kt | 3 +- .../bkrepo/media/web/PluginDelegateFilter.kt | 20 +++++++++ 9 files changed, 48 insertions(+), 53 deletions(-) delete mode 100644 src/backend/job/boot-job-worker/shell/media-process.sh create mode 100644 src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/config/WebConfig.kt create mode 100644 src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/web/PluginDelegateFilter.kt diff --git a/src/backend/job/boot-job-worker/shell/media-process.sh b/src/backend/job/boot-job-worker/shell/media-process.sh deleted file mode 100644 index 38c0e8ce68..0000000000 --- a/src/backend/job/boot-job-worker/shell/media-process.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash -get_field() { - local field_name=$1 - field_value=$(echo $DEVOPS_SCHEDULE_JOB_PARAMETERS| jq -r ".$field_name") - if [ -z "$field_value" ]; then - echo "Error: Field '$field_name' not found in input file" - exit 1 - fi - echo "$field_value" -} - -echo "1. 获取参数" -inputUrl=$(get_field inputUrl) -callbackUrl=$(get_field callbackUrl) -inputFileName=$(get_field inputFileName) -scale=$(get_field scale) -videoCodec=$(get_field videoCodec) -outputFileName=$(get_field outputFileName) - -echo "2. 下载待转码文件 - $inputFileName" -# 使用 -s 参数静默执行,-w "%{http_code}" 输出HTTP状态码 -http_status=$(curl -s -w "%{http_code}" -o $inputFileName $inputUrl) -if [ $http_status -ne 200 ];then - echo "文件下载失败[$http_status],Url: $inputUrl" - exit 1 -fi -ls -l - -echo "3. 开始转码 - $inputFileName > $outputFileName" -echo "ffmpeg -i $inputFileName -vf scale=$scale -c:a copy -c:v $videoCodec $outputFileName" -ffmpeg -i $inputFileName -vf scale=$scale -c:a copy -c:v $videoCodec $outputFileName -if [ $? -ne 0 ];then - echo "转码失败" - exit 1 -fi - -echo "4. 上传转码后的文件 - $outputFileName" -http_status=$(curl -s -w "%{http_code}" -X PUT -T $outputFileName "$callbackUrl") -if [ $http_status -ne 200 ];then - echo 文件上传失败[$http_status],Url: $callbackUrl - exit 1 -fi - -echo "转码完成 - $inputFileName" \ No newline at end of file diff --git a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/config/WebConfig.kt b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/config/WebConfig.kt new file mode 100644 index 0000000000..d3d6717aa0 --- /dev/null +++ b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/config/WebConfig.kt @@ -0,0 +1,16 @@ +package com.tencent.bkrepo.media.config + +import com.tencent.bkrepo.media.web.PluginDelegateFilter +import org.springframework.boot.web.servlet.FilterRegistrationBean +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +class WebConfig { + @Bean + fun pluginDelegateFilter(): FilterRegistrationBean { + val registrationBean = FilterRegistrationBean() + registrationBean.filter = PluginDelegateFilter() + return registrationBean + } +} diff --git a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/StreamService.kt b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/StreamService.kt index 1833726fe9..74425c05f7 100644 --- a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/StreamService.kt +++ b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/StreamService.kt @@ -59,7 +59,7 @@ class StreamService( type = RepositoryType.MEDIA, category = RepositoryCategory.LOCAL, public = false, - display = display + display = display, ) repositoryService.createRepo(createRepoRequest) val nodeCreateRequest = NodeCreateRequest( @@ -100,17 +100,20 @@ class StreamService( userId: String, remux: Boolean = false, saveType: MediaType = MediaType.RAW, + transcodeExtraParams: String? = null, ): ClientStream { val repoId = RepositoryId(projectId, repoName) val repo = ArtifactContextHolder.getRepoDetail(repoId) val credentials = repo.storageCredentials ?: storageProperties.defaultStorageCredentials() + val transcodeConfig = getTranscodeConfig(projectId) + transcodeConfig?.let { it.extraParams = transcodeExtraParams } val fileConsumer = MediaArtifactFileConsumer( storageManager, transcodeService, repo, userId, STREAM_PATH, - getTranscodeConfig(projectId), + transcodeConfig, ) val recordingListener = if (remux) { RemuxRecordingListener(credentials.upload.location, scheduler, saveType, fileConsumer) diff --git a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/TokenService.kt b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/TokenService.kt index a35226c8d6..304915854d 100644 --- a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/TokenService.kt +++ b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/TokenService.kt @@ -150,7 +150,7 @@ class TokenService( /** * 检查token并返回token信息 */ - private fun checkToken(token: String): TemporaryTokenInfo { + fun checkToken(token: String): TemporaryTokenInfo { if (token.isBlank()) { throw ErrorCodeException(ArtifactMessageCode.TEMPORARY_TOKEN_INVALID, token) } diff --git a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/TranscodeService.kt b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/TranscodeService.kt index 15734405a3..8e2719f016 100644 --- a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/TranscodeService.kt +++ b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/service/TranscodeService.kt @@ -76,7 +76,7 @@ class TranscodeService( projectId = projectId, repoName = repoName, fullPath = newArtifactInfo.getArtifactFullPath(), - metadata = originMetadata + metadata = originMetadata, ) metadataService.saveMetadata(copyRequest) val removeContext = ArtifactRemoveContext(repo, originArtifactInfo) @@ -106,6 +106,7 @@ class TranscodeService( audioCodec = audioCodec, inputFileName = artifactInfo.getResponseName(), outputFileName = outputArtifactInfo.getResponseName(), + extraParams = transcodeConfig.extraParams.orEmpty(), ) } } diff --git a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/MediaArtifactFileConsumer.kt b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/MediaArtifactFileConsumer.kt index 6d7ed3f1e7..b8b43b9505 100644 --- a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/MediaArtifactFileConsumer.kt +++ b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/MediaArtifactFileConsumer.kt @@ -8,7 +8,6 @@ import com.tencent.bkrepo.media.service.TranscodeService import com.tencent.bkrepo.repository.pojo.metadata.MetadataModel import com.tencent.bkrepo.repository.pojo.node.service.NodeCreateRequest import com.tencent.bkrepo.repository.pojo.repo.RepositoryDetail -import org.slf4j.LoggerFactory import java.io.File /** @@ -39,7 +38,6 @@ class MediaArtifactFileConsumer( storageManager.storeArtifactFile(nodeCreateRequest, file, repo.storageCredentials) if (transcodeConfig != null) { transcodeService.transcode(artifactInfo, transcodeConfig, userId) - logger.info("Add transcode task for artifact[$artifactInfo]") } } @@ -68,7 +66,6 @@ class MediaArtifactFileConsumer( } companion object { - private val logger = LoggerFactory.getLogger(MediaArtifactFileConsumer::class.java) private const val METADATA_KEY_MEDIA_START_TIME = "media.startTime" private const val METADATA_KEY_MEDIA_STOP_TIME = "media.stopTime" } diff --git a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/TranscodeConfig.kt b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/TranscodeConfig.kt index bc785920ce..774f962809 100644 --- a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/TranscodeConfig.kt +++ b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/TranscodeConfig.kt @@ -4,5 +4,6 @@ data class TranscodeConfig( var scale: String = "", // 分辨率,比如1280x720 var videoCodec: String = "", // 视频编码 var audioCodec: String = "", // 音频编码 - var jobId: String = "", // 转码任务id + var jobId: String = "", // 转码任务id, + var extraParams: String? = null, ) diff --git a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/TranscodeParam.kt b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/TranscodeParam.kt index fdf6c9ce85..ddd85e9554 100644 --- a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/TranscodeParam.kt +++ b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/stream/TranscodeParam.kt @@ -7,5 +7,6 @@ data class TranscodeParam( val videoCodec: String? = null, // 视频编码 val audioCodec: String? = null, // 音频编码 var inputFileName: String, // 源文件名 - var outputFileName: String, // 输出文件名 + var outputFileName: String, // 输出文件名, + var extraParams: String, // 额外参数 ) diff --git a/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/web/PluginDelegateFilter.kt b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/web/PluginDelegateFilter.kt new file mode 100644 index 0000000000..2b67713438 --- /dev/null +++ b/src/backend/media/biz-media/src/main/kotlin/com/tencent/bkrepo/media/web/PluginDelegateFilter.kt @@ -0,0 +1,20 @@ +package com.tencent.bkrepo.media.web + +import javax.servlet.Filter +import javax.servlet.FilterChain +import javax.servlet.ServletRequest +import javax.servlet.ServletResponse + +class PluginDelegateFilter : Filter { + override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) { + if (delegate != null) { + delegate!!.doFilter(request, response, chain) + } else { + chain.doFilter(request, response) + } + } + + companion object { + var delegate: Filter? = null + } +}