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

AWS翻译服务(实时及API)转换VTT文件时的格式异常问题咨询

解决AWS Translate转换VTT字幕的格式异常问题

问题根源

AWS Translate会将整个VTT内容视为普通文本处理,包括原本无需翻译的格式标记(如WEBVTT头部、时间轴),导致格式被意外修改。

解决方案:拆分VTT内容,仅翻译字幕文本部分

把VTT文件拆分为不可翻译的格式段(头部标识、时间轴)和可翻译的字幕文本,仅对文本部分调用翻译API,最后重新拼接成完整的VTT文件。

具体实现步骤(C#示例)

  1. 解析VTT内容:逐行区分格式行与文本行
  2. 翻译文本行:仅对字幕文本调用AWS Translate接口
  3. 重构VTT文件:将原格式行与翻译后的文本行重新组合
using Amazon.Translate;
using Amazon.Translate.Model;
using System.Collections.Generic;
using System.Linq;

public string TranslateVttContent(string vttContent, string targetLang)
{
    var lines = vttContent.Split('\n').ToList();
    var translatedLines = new List<string>();
    IAmazonTranslate translateClient = new AmazonTranslateClient(AWSAccessKey, AWSSecretKey, RegionEndpoint.EUWest1);

    foreach (var line in lines)
    {
        // 保留空行
        if (string.IsNullOrWhiteSpace(line))
        {
            translatedLines.Add(line);
            continue;
        }

        // 强制保留WEBVTT头部,不参与翻译
        if (line.StartsWith("WEBVTT", System.StringComparison.OrdinalIgnoreCase))
        {
            translatedLines.Add("WEBVTT");
            continue;
        }

        // 保留时间轴行(匹配标准时间格式),不翻译
        if (line.Contains("-->") && line.Contains(':') && line.Contains('.'))
        {
            translatedLines.Add(line);
            continue;
        }

        // 仅翻译字幕文本内容
        var translateRequest = new TranslateTextRequest
        {
            Text = line,
            SourceLanguageCode = "en",
            TargetLanguageCode = targetLang
        };

        var translateResponse = translateClient.TranslateTextAsync(translateRequest).Result;
        translatedLines.Add(translateResponse.TranslatedText);
    }

    return string.Join('\n', translatedLines);
}

各异常问题的针对性解决

  • 毫秒位置多余空格:时间轴行不再经过翻译流程,避免Translate对数字格式的误处理
  • 分隔符-->变为—>:直接保留原始时间轴格式,不会被替换为全角符号
  • 乌尔都语下WEBVTT丢失末尾T:头部标识强制固定为"WEBVTT",不参与翻译

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 01:06:19