Skip to content

Commit

Permalink
1.支持字幕中英互译、输出双语字幕(百度翻译接口)
Browse files Browse the repository at this point in the history
2.支持设置输出单条音轨字幕文件
  • Loading branch information
wxbool committed Dec 17, 2019
1 parent 4f91ccf commit f2c0538
Show file tree
Hide file tree
Showing 7 changed files with 409 additions and 20 deletions.
44 changes: 43 additions & 1 deletion app/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package app
import (
"videosrt/app/aliyun"
"videosrt/app/datacache"
"videosrt/app/translate"
)

var RootDir string

var oss,engine,setings *datacache.AppCache
var oss,translates,engine,setings *datacache.AppCache

//输出文件类型
const(
Expand All @@ -22,6 +23,7 @@ func init() {
}

oss = datacache.NewAppCahce(RootDir , "oss")
translates = datacache.NewAppCahce(RootDir , "translate")
engine = datacache.NewAppCahce(RootDir , "engine")
setings = datacache.NewAppCahce(RootDir , "setings")
}
Expand All @@ -46,11 +48,25 @@ type OutputSelects struct {
Name string
}

//输出音轨类型选项
type SoundTrackSelects struct {
Id int
Name string
}

//阿里云OSS - 缓存结构
type AliyunOssCache struct {
aliyun.AliyunOss
}

//软件翻译接口 - 缓存结构
type TranslateCache struct {
translate.BaiduTranslate //百度翻译

AutoTranslation bool //中英互译(默认关闭)
BilingualSubtitles bool //输出双语字幕(默认关闭)
}

//阿里云语音识别引擎 - 缓存结构
type AliyunEngineCache struct {
aliyun.AliyunClound
Expand All @@ -69,6 +85,7 @@ type AppSetings struct {
MaxConcurrency int //任务最大处理并发数
OutputType int //输出文件类型
SrtFileDir string //Srt文件输出目录
SoundTrack int //输出音轨
}

//任务文件列表 - 结构
Expand All @@ -93,6 +110,22 @@ func SetCacheAliyunOssData(data *AliyunOssCache) {
}


//获取 软件翻译接口 设置缓存
func GetCacheTranslateSettings() *TranslateCache {
data := new(TranslateCache)
vdata := translates.Get(data)
if v, ok := vdata.(*TranslateCache); ok {
return v
}
return data
}

//设置 软件翻译接口 缓存
func SetCacheTranslateSettings(data *TranslateCache) {
translates.Set(data)
}


//获取 阿里云语音识别引擎 缓存数据
func GetCacheAliyunEngineListData() *AliyunEngineListCache {
data := new(AliyunEngineListCache)
Expand Down Expand Up @@ -197,4 +230,13 @@ func GetOutputOptionsSelects() []*OutputSelects {
&OutputSelects{Id:OUTPUT_SRT , Name:"字幕文件"},
&OutputSelects{Id:OUTPUT_STRING , Name:"普通文本"},
}
}

//获取 输出音轨选项列表
func GetSoundTrackSelects() []*SoundTrackSelects {
return []*SoundTrackSelects{
&SoundTrackSelects{Id:0 , Name:"全部"},
&SoundTrackSelects{Id:1 , Name:"音轨一"},
&SoundTrackSelects{Id:2 , Name:"音轨二"},
}
}
139 changes: 138 additions & 1 deletion app/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func(mw *MyMainWindow) RunAppSetingDialog(owner walk.Form , confirmCall func(*Ap
Model: GetOutputOptionsSelects(),
},

Label{
Text: "输出音轨:",
},
ComboBox{
Value: Bind("SoundTrack", SelRequired{}),
BindingMember: "Id",
DisplayMember: "Name",
Model: GetSoundTrackSelects(),
},

Label{
Text: "任务处理并发数:",
Expand Down Expand Up @@ -188,6 +197,12 @@ func(mw *MyMainWindow) RunSpeechEngineSetingDialog(owner walk.Form , confirmCall
LineEdit{
Text: Bind("AccessKeySecret"),
},

Label{
ColumnSpan: 2,
Text: "说明:\r\n“语音识别” 目前使用的是阿里云语音服务商,请填写相关的引擎配置。",
TextColor:walk.RGB(190 , 190 , 190),
},
},
},
Composite{
Expand Down Expand Up @@ -262,7 +277,7 @@ func (mw *MyMainWindow) RunObjectStorageSetingDialog(owner walk.Form) {

Dialog{
AssignTo: &dlg,
Title: "Oss对象存储设置",
Title: "OSS对象存储设置",
DefaultButton: &acceptPB,
CancelButton: &cancelPB,
DataBinder: DataBinder{
Expand Down Expand Up @@ -311,6 +326,12 @@ func (mw *MyMainWindow) RunObjectStorageSetingDialog(owner walk.Form) {
LineEdit{
Text: Bind("BucketDomain"),
},

Label{
ColumnSpan: 2,
Text: "说明:\r\n“OSS对象存储”目前使用的是阿里云服务,请填写相关的服务配置。",
TextColor:walk.RGB(190 , 190 , 190),
},
},
},
Composite{
Expand Down Expand Up @@ -364,6 +385,122 @@ func (mw *MyMainWindow) RunObjectStorageSetingDialog(owner walk.Form) {
}.Run( owner )
}


//运行 翻译接口设置 Dialog
func (mw *MyMainWindow) RunTranslateSetingDialog(owner walk.Form) {
var translateData *TranslateCache
var dlg *walk.Dialog
var db *walk.DataBinder
var acceptPB, cancelPB *walk.PushButton

translateData = GetCacheTranslateSettings() //查询缓存数据

Dialog{
AssignTo: &dlg,
Title: "翻译设置",
DefaultButton: &acceptPB,
CancelButton: &cancelPB,
DataBinder: DataBinder{
AssignTo: &db,
Name: "oss",
DataSource: translateData,
ErrorPresenter: ToolTipErrorPresenter{},
},
MinSize: Size{500, 300},
Layout: VBox{},
Children: []Widget{
Composite{
Layout: Grid{Columns: 4},
Children: []Widget{

Label{
Text: "中英互译:",
},
CheckBox{
Checked: Bind("AutoTranslation"),
},

Label{
Text: "输出为双语字幕:",
},
CheckBox{
Checked: Bind("BilingualSubtitles"),
},


Label{
ColumnSpan: 4,
Text: "翻译接口设置:",
TextColor:walk.RGB(190 , 190 , 190),
MinSize:Size{Width:100 , Height:20},
},

Label{
Text: "AppId:",
},
LineEdit{
ColumnSpan: 3,
Text: Bind("AppId"),
},

Label{
Text: "AppSecret:",
},
LineEdit{
ColumnSpan: 3,
Text: Bind("AppSecret"),
},

Label{
ColumnSpan: 4,
Text: "说明:\r\n“文本翻译”目前使用的是百度翻译服务,请填写相关的服务配置。",
TextColor:walk.RGB(190 , 190 , 190),
MinSize:Size{Width:150 , Height:60},
},
},
},
Composite{
Layout: HBox{},
Children: []Widget{
HSpacer{},
PushButton{
AssignTo: &acceptPB,
Text: "保存",
OnClicked: func() {
if err := db.Submit(); err != nil {
log.Fatal(err)
return
}

//参数验证
if (translateData.AppId == "") {
mw.NewInformationTips("提示" , "请填写 AppId")
return
}
if (translateData.AppSecret == "") {
mw.NewInformationTips("提示" , "请填写 AccessKeyId")
return
}

//设置缓存
SetCacheTranslateSettings(translateData)

dlg.Accept()
},
},
PushButton{
AssignTo: &cancelPB,
Text: "取消",
OnClicked: func() { dlg.Cancel() },
},
},
},
},
}.Run( owner )
}



//打开 Github
func (mw *MyMainWindow) OpenAboutGithub() {
tool.OpenUrl("https://github.com/wxbool/video-srt")
Expand Down
10 changes: 10 additions & 0 deletions app/tool/tool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tool

import (
"crypto/md5"
"encoding/hex"
"math/rand"
"os"
"os/exec"
Expand Down Expand Up @@ -166,3 +168,11 @@ func VaildVideo (video string) bool {
}
return true
}


//文本md5
func Md5String(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
102 changes: 102 additions & 0 deletions app/translate/baidu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package translate

import (
"errors"
"github.com/buger/jsonparser"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"videosrt/app/tool"
)

//百度翻译
type BaiduTranslate struct {
AppId string //appid
AppSecret string //appsecret
}

//百度翻译结果集
type BaiduTranslateResult struct {
From string //翻译源语言
To string //译文语言
TransResultSrc string //翻译结果(原文)
TransResultDst string //翻译结果(译文)
ErrorCode int64 //错误码(仅当出现错误时存在)
ErrorMsg string //错误消息(仅当出现错误时存在)
}

//常量
const (
TRANS_API string = "https://fanyi-api.baidu.com/api/trans/vip/translate"
)


//百度api文档
//http://api.fanyi.baidu.com/api/trans/product/apidoc
//支持语言列表 http://api.fanyi.baidu.com/api/trans/product/apidoc#languageList
func (trans *BaiduTranslate) Translate (strings string , from string , to string) (*BaiduTranslateResult , error) {
params := &url.Values{}

params.Add("q" , strings)
params.Add("appid" , trans.AppId)
params.Add("salt" , strconv.FormatInt(tool.GetIntRandomNumber(10000 , 99999) , 10))
params.Add("from" , from)
params.Add("to" , to)
params.Add("sign" , trans.BuildSign(strings , params.Get("salt")))

return trans.CallRequest(params)
}

//生成加密sign
func (trans *BaiduTranslate) BuildSign (strings string , salt string) string {
str := trans.AppId + strings + salt + trans.AppSecret
return tool.Md5String(str)
}

//发起请求
func (trans *BaiduTranslate) CallRequest (params *url.Values ) (*BaiduTranslateResult , error) {
url := TRANS_API + "?" + params.Encode()

request, e := http.NewRequest(http.MethodGet, url , nil)
if e != nil {
return nil,e
}
//do request
response, e := http.DefaultClient.Do(request)
if e != nil {
return nil,e
}
//content
content, e := ioutil.ReadAll(response.Body)
if e != nil {
return nil,e
}

//解析数据
errorCode , _ := jsonparser.GetString(content , "error_code")
errorMsg , _ := jsonparser.GetString(content , "error_msg")
from , _ := jsonparser.GetString(content , "from")
to , _ := jsonparser.GetString(content , "to")

errorCodeInt , _ := strconv.Atoi(errorCode)

result := &BaiduTranslateResult{
ErrorCode:int64(errorCodeInt),
ErrorMsg:errorMsg,
From:from,
To:to,
}

_, _ = jsonparser.ArrayEach(content, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
result.TransResultSrc, _ = jsonparser.GetString(value, "src")
result.TransResultDst, _ = jsonparser.GetString(value, "dst")
}, "trans_result")

//翻译错误校验
if result.ErrorCode != 0 {
return nil , errors.New("翻译失败 , 错误提示(" + result.ErrorMsg + ")")
}

return result,nil
}
Loading

0 comments on commit f2c0538

Please sign in to comment.