Skip to content

Commit

Permalink
更新说明:
Browse files Browse the repository at this point in the history
1.支持同时输出多格式文件(srt、lrc、txt)
2.支持批量对SRT字幕文件进行二次翻译、编码处理
3.支持自动清理上传至OSS的临时文件(默认开启,可关闭)
4.支持设置双语字幕的主字幕(输入/输出)
5.优化错误提示
  • Loading branch information
wxbool committed Apr 8, 2020
1 parent 601e043 commit bcddab8
Show file tree
Hide file tree
Showing 14 changed files with 1,233 additions and 381 deletions.
30 changes: 28 additions & 2 deletions app/aliyun/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type AliyunClound struct {
//阿里云录音文件识别结果集
type AliyunAudioRecognitionResult struct {
Text string //文本结果
TranslateText string //翻译文本结果
ChannelId int64 //音轨ID
BeginTime int64 //该句的起始时间偏移,单位为毫秒
EndTime int64 //该句的结束时间偏移,单位为毫秒
Expand Down Expand Up @@ -68,6 +69,7 @@ func (c AliyunClound) NewAudioFile(fileLink string) (string , *sdk.Client , erro
if err != nil {
return "" , client , err
}
client.SetConnectTimeout(time.Second * 20)

postRequest := requests.NewCommonRequest()
postRequest.Domain = DOMAIN
Expand Down Expand Up @@ -116,7 +118,7 @@ func (c AliyunClound) NewAudioFile(fileLink string) (string , *sdk.Client , erro
return taskId , client , nil
}

return "" , client , errors.New("录音文件识别请求失败 ! statusText : " + statusText)
return "" , client , errors.New("录音文件识别失败 , (" + c.GetErrorStatusTextMessage(statusText) + ")")
}


Expand Down Expand Up @@ -163,8 +165,32 @@ func (c AliyunClound) GetAudioFileResult(taskId string , client *sdk.Client , ca
}

if statusText != STATUS_SUCCESS {
return errors.New("录音文件识别失败 , (" + statusText + ")")
return errors.New("录音文件识别失败 , (" + c.GetErrorStatusTextMessage(statusText) + ")")
}

return nil
}


//获取错误信息
func (c AliyunClound) GetErrorStatusTextMessage (statusText string) string {
var code map[string]string = map[string]string{
"USER_BIZDURATION_QUOTA_EXCEED":"单日免费额度超出限制",
"FILE_DOWNLOAD_FAILED":"文件访问失败,请检查OSS存储空间访问权限",
"FILE_TOO_LARGE":"音频文件超出512MB",
"FILE_PARSE_FAILED":"音频文件解析失败,请检查音频文件是否有损坏",
"UNSUPPORTED_SAMPLE_RATE":"采样率不匹配",
"FILE_TRANS_TASK_EXPIRED":"音频文件识别任务过期,请重试",
"REQUEST_INVALID_FILE_URL_VALUE":"音频文件访问失败,请检查OSS存储空间访问权限",
"FILE_404_NOT_FOUND":"音频文件访问失败,请检查OSS存储空间访问权限",
"FILE_403_FORBIDDEN":"音频文件访问失败,请检查OSS存储空间访问权限",
"FILE_SERVER_ERROR":"音频文件访问失败,请检查请求的文件所在的服务是否可用",
"INTERNAL_ERROR":"识别内部通用错误,请稍候重试",
}

if _, ok := code[statusText]; ok {
return code[statusText]
} else {
return statusText
}
}
23 changes: 23 additions & 0 deletions app/aliyun/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ func (c AliyunOss) UploadFile(localFileName string , objectName string) (string
}


//删除oss文件
func (c AliyunOss) DeleteFile(objectName string) error {
// 创建OSSClient实例
client, err := oss.New(c.Endpoint , c.AccessKeyId , c.AccessKeySecret)
if err != nil {
return err
}
// 获取存储空间
bucket, err := client.Bucket(c.BucketName)
if err != nil {
return err
}

// 删除单个文件。objectName表示删除OSS文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// 如需删除文件夹,请将objectName设置为对应的文件夹名称。如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
err = bucket.DeleteObject(objectName)
if err != nil {
return err
}
return nil
}


//获取文件 url link
func (c AliyunOss) GetObjectFileUrl(objectFile string) string {
if strings.Index(c.BucketDomain, "http://") == -1 && strings.Index(c.BucketDomain, "https://") == -1 {
Expand Down
59 changes: 59 additions & 0 deletions app/app_tool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package app

import (
"bytes"
"strconv"
"videosrt/app/tool"
)

//拼接字幕字符串
func MakeSubtitleText(index int , startTime int64 , endTime int64 , text string , translateText string , bilingualSubtitleSwitch bool , bilingualAsc bool) string {
var content bytes.Buffer
content.WriteString(strconv.Itoa(index))
content.WriteString("\r\n")
content.WriteString(tool.SubtitleTimeMillisecond(startTime , true))
content.WriteString(" --> ")
content.WriteString(tool.SubtitleTimeMillisecond(endTime , true))
content.WriteString("\r\n")

//输出双语字幕
if bilingualSubtitleSwitch {
if bilingualAsc {
content.WriteString(text)
content.WriteString("\r\n")
content.WriteString(translateText)
} else {
content.WriteString(translateText)
content.WriteString("\r\n")
content.WriteString(text)
}
} else {
content.WriteString(text)
}

content.WriteString("\r\n")
content.WriteString("\r\n")
return content.String()
}


//拼接文本格式
func MakeText(index int , startTime int64 , endTime int64 , text string) string {
var content bytes.Buffer
content.WriteString(text)
content.WriteString("\r\n")
content.WriteString("\r\n")
return content.String()
}


//拼接歌词文本
func MakeMusicLrcText(index int , startTime int64 , endTime int64 , text string) string {
var content bytes.Buffer
content.WriteString("[")
content.WriteString(tool.MusicLrcTextMillisecond(startTime))
content.WriteString("]")
content.WriteString(text)
content.WriteString("\r\n")
return content.String()
}
54 changes: 23 additions & 31 deletions app/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ type OperateFrom struct {
BilingualSubtitleSwitch bool //是否输出双语字幕
InputLanguage int //输入字幕语言
OutputLanguage int //输出字幕语言
OutputMainSubtitleInputLanguage bool //双语主字幕(输入语言)

OutputSrt bool
OutputLrc bool
OutputTxt bool

OutputType int
OutputType *AppSetingsOutput //输出文件类型
OutputEncode int //输出文件编码
SoundTrack int //输出音轨
}
Expand All @@ -103,12 +104,18 @@ type LanguageSelects struct {
Name string
}

type AppSetingsOutput struct {
SRT bool
LRC bool
TXT bool
}

//应用配置 - 缓存结构
type AppSetings struct {
CurrentEngineId int //目前语音引擎Id
CurrentTranslateEngineId int //目前翻译引擎Id
MaxConcurrency int //任务最大处理并发数
OutputType int //输出文件类型
OutputType *AppSetingsOutput //输出文件类型
OutputEncode int //输出文件编码
SrtFileDir string //Srt文件输出目录
SoundTrack int //输出音轨
Expand All @@ -117,8 +124,10 @@ type AppSetings struct {
BilingualSubtitleSwitch bool //是否输出双语字幕
InputLanguage int //输入字幕语言
OutputLanguage int //输出字幕语言
OutputMainSubtitleInputLanguage bool //双语主字幕(输入语言)

CloseNewVersionMessage bool //关闭软件新版本提醒(默认开启)
CloseNewVersionMessage bool //关闭软件新版本提醒(默认开启)[false开启 true关闭]
CloseAutoDeleteOssTempFile bool //关闭自动删除临时音频文件(默认开启)[false开启 true关闭]
}

//任务文件列表 - 结构
Expand All @@ -128,33 +137,38 @@ type TaskHandleFile struct {

//根据配置初始化表单
func (from *OperateFrom) Init(setings *AppSetings) {
from.OutputType = new(AppSetingsOutput)
if setings.CurrentEngineId != 0 {
from.EngineId = setings.CurrentEngineId
}
if setings.CurrentTranslateEngineId != 0 {
from.TranslateEngineId = setings.CurrentTranslateEngineId
}
if setings.OutputType == 0 {
from.OutputType = OUTPUT_SRT

if !setings.OutputType.LRC && !setings.OutputType.SRT && !setings.OutputType.TXT {
from.OutputType.SRT = true
from.OutputSrt = true
} else {
from.OutputType = setings.OutputType
if setings.OutputType == OUTPUT_SRT {
if setings.OutputType.SRT {
from.OutputSrt = true
}
if setings.OutputType == OUTPUT_STRING {
if setings.OutputType.TXT {
from.OutputTxt = true
}
if setings.OutputType == OUTPUT_LRC {
if setings.OutputType.LRC {
from.OutputLrc = true
}
}

if setings.OutputEncode == 0 {
from.OutputEncode = OUTPUT_ENCODE_UTF8 //默认编码
} else {
from.OutputEncode = setings.OutputEncode
}

from.OutputMainSubtitleInputLanguage = setings.OutputMainSubtitleInputLanguage

if setings.SoundTrack == 0 {
from.SoundTrack = 1 //默认输出音轨一
} else {
Expand All @@ -177,29 +191,6 @@ func (from *OperateFrom) Init(setings *AppSetings) {
from.BilingualSubtitleSwitch = setings.BilingualSubtitleSwitch
}

//加载输出类型
func (from *OperateFrom) LoadOutputType(t int) {
if OUTPUT_SRT != t {
from.OutputSrt = false
}
if OUTPUT_LRC != t {
from.OutputLrc = false
}
if OUTPUT_STRING != t {
from.OutputTxt = false
}

if from.OutputSrt {
from.OutputType = OUTPUT_SRT
} else if from.OutputLrc {
from.OutputType = OUTPUT_LRC
} else if from.OutputTxt {
from.OutputType = OUTPUT_STRING
} else {
from.OutputType = 0
}
}

//获取 输出文件选项列表
func GetOutputOptionsSelects() []*OutputSelects {
return []*OutputSelects{
Expand Down Expand Up @@ -248,6 +239,7 @@ func GetTranslateOutputLanguageOptionsSelects() []*LanguageSelects {
//获取 应用配置
func (setings *AppSetingsAppStruct) GetCacheAppSetingsData() *AppSetings {
data := new(AppSetings)
data.OutputType = new(AppSetingsOutput)
vdata := setings.Data.Get(data)
if v, ok := vdata.(*AppSetings); ok {
return v
Expand Down
2 changes: 1 addition & 1 deletion app/datacache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (app *AppCache) Get(structs interface{}) interface{} {
if os.IsNotExist(err) {
return structs
}
panic(err)
return data
}
return data
}
Expand Down
Loading

0 comments on commit bcddab8

Please sign in to comment.