123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- package services
- import (
- "bytes"
- "encoding/base64"
- "encoding/binary"
- "encoding/json"
- "errors"
- "eta_gn/eta_task/models"
- "eta_gn/eta_task/services/alarm_msg"
- "eta_gn/eta_task/utils"
- "fmt"
- "github.com/PuerkitoBio/goquery"
- "github.com/kgiannakakis/mp3duration/src/mp3duration"
- "html"
- "io"
- "io/ioutil"
- "os"
- "strings"
- "time"
- "unicode"
- )
- func CreateVideo(report *models.Report) (err error) {
- defer func() {
- if err != nil {
- utils.FileLog.Error("CreateVideo Err:%s", err.Error())
- go alarm_msg.SendAlarmMsg("CreateVideo, Err:"+err.Error(), 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "Err:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- // 获取基础配置, 若未配置则直接返回
- conf, e := models.GetBusinessConf()
- if e != nil {
- err = fmt.Errorf("获取基础配置失败, Err: " + e.Error())
- return
- }
- if conf[models.BusinessConfUseXf] != "true" {
- return
- }
- if conf[models.BusinessConfXfAppid] == "" || conf[models.BusinessConfXfApiKey] == "" || conf[models.BusinessConfXfApiSecret] == "" || conf[models.BusinessConfXfVcn] == "" {
- return
- }
- var xfReq XfParams
- xfReq.XfAPPID = conf[models.BusinessConfXfAppid]
- xfReq.XfAPIKey = conf[models.BusinessConfXfApiKey]
- xfReq.XfAPISecret = conf[models.BusinessConfXfApiSecret]
- ct, err := time.Parse(utils.FormatDateTime, report.CreateTime)
- createTime := ct.Format("0102")
- videoName := report.Title + "(" + createTime + ")"
- content := html.UnescapeString(report.Content)
- content = strings.Replace(content, "Powered", "", -1)
- content = strings.Replace(content, "by", "", -1)
- content = strings.Replace(content, "Froala", "", -1)
- content = strings.Replace(content, "Editor", "", -1)
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
- if err != nil {
- return
- }
- param := new(models.XfSendParam)
- param.Common.AppId = conf[models.BusinessConfXfAppid]
- param.Business.Aue = "lame"
- param.Business.Sfl = 1
- param.Business.Auf = "audio/L16;rate=16000"
- param.Business.Vcn = conf[models.BusinessConfXfVcn]
- param.Business.Speed = 50
- param.Business.Volume = 100
- param.Business.Pitch = 50
- param.Business.Bgs = 0
- param.Business.Tte = "UTF8"
- param.Business.Reg = "2"
- param.Business.Rdn = "0"
- param.Data.Status = 2
- videoContent := doc.Text()
- saveName := utils.GetRandStringNoSpecialChar(16) + ".mp3"
- savePath := "./" + saveName
- //if utils.FileIsExist(savePath) {
- // os.Remove(savePath)
- //}
- // 如果没有文本内容,那么就不生成了
- videoContent = strings.TrimSpace(videoContent)
- if videoContent == `` {
- return
- }
- contentArr := GetChineseCount(videoContent)
- for _, v := range contentArr {
- newText := v
- param.Data.Text = base64.StdEncoding.EncodeToString([]byte(newText))
- result, err := json.Marshal(param)
- if err != nil {
- return err
- }
- err = GetXfVideo(result, savePath, xfReq)
- if err != nil {
- err = errors.New("GetXfVideo Err:" + err.Error())
- utils.FileLog.Error("GetXfVideo err", err.Error())
- return err
- }
- time.Sleep(5 * time.Second)
- }
- uploadUrl, err := UploadAudioAliyun(saveName, savePath)
- if err != nil {
- err = errors.New("UploadAudioAliyun Err:" + err.Error())
- return
- }
- fileBody, err := ioutil.ReadFile(savePath)
- videoSize := len(fileBody)
- sizeFloat := (float64(videoSize) / float64(1024)) / float64(1024)
- sizeStr := utils.SubFloatToFloatStr(sizeFloat, 2)
- playSeconds, err := mp3duration.Calculate(savePath)
- if playSeconds <= 0 {
- playSeconds, err = utils.GetVideoPlaySeconds(savePath)
- if err != nil {
- err = errors.New("GetVideoPlaySeconds Err:" + err.Error())
- return
- }
- }
- if playSeconds > 0 {
- if utils.FileIsExist(savePath) {
- os.Remove(savePath)
- }
- }
- err = models.ModifyReportVideo(report.Id, uploadUrl, videoName, sizeStr, playSeconds)
- return
- }
- func GetChineseCount(str1 string) []string {
- fontArr := make([]string, 0)
- str := ""
- count := 0
- for _, char := range str1 {
- str += string(char)
- if unicode.Is(unicode.Han, char) {
- count++
- if count >= 1700 {
- fontArr = append(fontArr, str)
- str = ""
- count = 0
- }
- }
- }
- fontArr = append(fontArr, str)
- return fontArr
- }
- // BoxHeader 信息头
- type BoxHeader struct {
- Size uint32
- FourccType [4]byte
- Size64 uint64
- }
- // GetMP4Duration 获取视频时长,以秒计
- func GetMP4Duration(reader io.ReaderAt) (lengthOfTime uint32, err error) {
- var info = make([]byte, 0x10)
- var boxHeader BoxHeader
- var offset int64 = 0
- // 获取moov结构偏移
- for {
- _, err = reader.ReadAt(info, offset)
- if err != nil {
- return
- }
- boxHeader = getHeaderBoxInfo(info)
- fourccType := getFourccType(boxHeader)
- if fourccType == "moov" {
- break
- }
- // 有一部分mp4 mdat尺寸过大需要特殊处理
- if fourccType == "mdat" {
- if boxHeader.Size == 1 {
- offset += int64(boxHeader.Size64)
- continue
- }
- }
- offset += int64(boxHeader.Size)
- }
- // 获取moov结构开头一部分
- moovStartBytes := make([]byte, 0x100)
- _, err = reader.ReadAt(moovStartBytes, offset)
- if err != nil {
- return
- }
- // 定义timeScale与Duration偏移
- timeScaleOffset := 0x1C
- durationOffest := 0x20
- timeScale := binary.BigEndian.Uint32(moovStartBytes[timeScaleOffset : timeScaleOffset+4])
- Duration := binary.BigEndian.Uint32(moovStartBytes[durationOffest : durationOffest+4])
- lengthOfTime = Duration / timeScale
- return
- }
- // getHeaderBoxInfo 获取头信息
- func getHeaderBoxInfo(data []byte) (boxHeader BoxHeader) {
- buf := bytes.NewBuffer(data)
- binary.Read(buf, binary.BigEndian, &boxHeader)
- return
- }
- // getFourccType 获取信息头类型
- func getFourccType(boxHeader BoxHeader) (fourccType string) {
- fourccType = string(boxHeader.FourccType[:])
- return
- }
- // CreateReportVideo 生成报告video
- func CreateReportVideo(reportTitle, reportContent, reportTime string) (uploadUrl, videoName, sizeStr string, playSeconds float64, err error) {
- defer func() {
- if err != nil {
- utils.FileLog.Error("CreateReportVideo Err:%s", err.Error())
- go alarm_msg.SendAlarmMsg("CreateReportVideo, reportTitle:"+reportTitle+", Err:"+err.Error(), 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "CreateReportVideo, reportTitle:" + reportTitle +", Err:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- if reportContent == "" {
- return
- }
- // 获取基础配置, 若未配置则直接返回
- conf, e := models.GetBusinessConf()
- if e != nil {
- err = fmt.Errorf("获取基础配置失败, Err: " + e.Error())
- return
- }
- if conf[models.BusinessConfUseXf] != "true" {
- return
- }
- if conf[models.BusinessConfXfAppid] == "" || conf[models.BusinessConfXfApiKey] == "" || conf[models.BusinessConfXfApiSecret] == "" || conf[models.BusinessConfXfVcn] == "" {
- return
- }
- var xfReq XfParams
- xfReq.XfAPPID = conf[models.BusinessConfXfAppid]
- xfReq.XfAPIKey = conf[models.BusinessConfXfApiKey]
- xfReq.XfAPISecret = conf[models.BusinessConfXfApiSecret]
- ct, err := time.Parse(utils.FormatDateTime, reportTime)
- if err != nil {
- return
- }
- createTime := ct.Format("0102")
- videoName = reportTitle + "(" + createTime + ")"
- content := html.UnescapeString(reportContent)
- content = strings.Replace(content, "Powered", "", -1)
- content = strings.Replace(content, "by", "", -1)
- content = strings.Replace(content, "Froala", "", -1)
- content = strings.Replace(content, "Editor", "", -1)
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
- if err != nil {
- return
- }
- param := new(models.XfSendParam)
- param.Common.AppId = conf[models.BusinessConfXfAppid]
- param.Business.Aue = "lame"
- param.Business.Sfl = 1
- param.Business.Auf = "audio/L16;rate=16000"
- param.Business.Vcn = conf[models.BusinessConfXfVcn]
- param.Business.Speed = 50
- param.Business.Volume = 100
- param.Business.Pitch = 50
- param.Business.Bgs = 0
- param.Business.Tte = "UTF8"
- param.Business.Reg = "2"
- param.Business.Rdn = "0"
- param.Data.Status = 2
- videoContent := doc.Text()
- saveName := utils.GetRandStringNoSpecialChar(16) + ".mp3"
- savePath := "./" + saveName
- //if utils.FileIsExist(savePath) {
- // os.Remove(savePath)
- //}
- contentArr := GetChineseCount(videoContent)
- for _, v := range contentArr {
- newText := v
- param.Data.Text = base64.StdEncoding.EncodeToString([]byte(newText))
- result, tmpErr := json.Marshal(param)
- if tmpErr != nil {
- return
- }
- err = GetXfVideo(result, savePath, xfReq)
- if err != nil {
- err = errors.New("GetXfVideo Err:" + err.Error())
- utils.FileLog.Error("GetXfVideo err", err.Error())
- return
- }
- time.Sleep(5 * time.Second)
- }
- uploadUrl, err = UploadAudioAliyun(saveName, savePath)
- if err != nil {
- err = errors.New("UploadAudioAliyun Err:" + err.Error())
- return
- }
- fileBody, err := ioutil.ReadFile(savePath)
- videoSize := len(fileBody)
- sizeFloat := (float64(videoSize) / float64(1024)) / float64(1024)
- sizeStr = utils.SubFloatToFloatStr(sizeFloat, 2)
- playSeconds, err = mp3duration.Calculate(savePath)
- if playSeconds <= 0 {
- playSeconds, err = utils.GetVideoPlaySeconds(savePath)
- if err != nil {
- err = errors.New("GetVideoPlaySeconds Err:" + err.Error())
- return
- }
- }
- if playSeconds > 0 {
- if utils.FileIsExist(savePath) {
- os.Remove(savePath)
- }
- }
- return
- }
|