You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用FFmpeg在Android Kotlin中给视频加黑边实现全屏?

解决FFmpeg在Android中调整视频全屏(上下加黑边)的命令问题

你的FFmpeg命令无效的核心原因是:scale=-1:$screenHeight会按原视频比例将高度拉伸到2044,此时宽度会被自动计算为约1149(远大于屏幕的576宽度),后续的pad命令试图把宽度从1149压缩到576,这不符合FFmpeg的pad规则(目标宽度不能小于输入视频宽度),所以命令执行失败。

根据你的需求,提供两种可行方案:

方案1:保持视频原宽高比,上下添加黑边填充全屏

此方案保留视频原始比例,仅在上下添加黑边适配屏幕高度,是更合理的适配方式:

fun makeVideoFullScreen(
    context: Context,
    inputVideoPath: String,
    screenWidth: Int,
    screenHeight: Int,
    listener : (outputPath: String, returnCode: Int) -> Unit
){
    val fileName = Utility.getCurrentTime()
    val externalDir = context.getExternalFilesDir(null)
    val file = File(externalDir, "temp_video${fileName}.mp4")
    val outputPath = file.absolutePath

    val cmd = arrayOf(
        "-i", inputVideoPath,
        "-vf", "scale=$screenWidth:-1, pad=$screenWidth:$screenHeight:0:(oh-ih)/2:black",
        "-c:v", "libx264",
        "-preset", "ultrafast",
        "-c:a", "copy",
        outputPath
    )

    FFmpeg.executeAsync(cmd) { _, returnCode ->
        listener(outputPath, returnCode)
    }
}

命令解释:

  • scale=$screenWidth:-1:固定视频宽度为屏幕宽度576,高度按原比例自动计算(结果仍为1024,和原视频高度一致)
  • pad=$screenWidth:$screenHeight:0:(oh-ih)/2:black:将视频填充到屏幕高度2044,水平方向无偏移(宽度一致),垂直方向自动计算上下黑边高度(2044-1024)/2=510,黑边颜色设为黑色

方案2:拉伸视频至全屏(无黑边,会破坏原比例)

如果你确实需要强制将视频拉伸到576×2044(不保留原比例),可直接使用scale命令,无需pad:

val cmd = arrayOf(
    "-i", inputVideoPath,
    "-vf", "scale=$screenWidth:$screenHeight",
    "-c:v", "libx264",
    "-preset", "ultrafast",
    "-c:a", "copy",
    outputPath
)

内容的提问来源于stack exchange,提问作者Akari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 18:57:36