Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(upload):fixed invalid ffmpeg command in transcode feature #1236

Merged
merged 13 commits into from
Sep 30, 2023
2 changes: 1 addition & 1 deletion core/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
//====== THIS IS AUTOGENERATED FILE. DO NOT MODIFY ========

package version
const VERSIONSTR = "v1.10.0-51-g725742a3"
const VERSIONSTR = "v1.10.0-62-g0fba09a8"

22 changes: 21 additions & 1 deletion winsdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
var log logger.Logger

func main() {

}

// SetLogFile - set log file
Expand Down Expand Up @@ -260,3 +259,24 @@ func GetLookupHash(allocationID *C.char, path *C.char) *C.char {
hash := getLookupHash(C.GoString(allocationID), C.GoString(path))
return WithJSON(hash, nil)
}

// SetFFmpeg set the full file name of ffmpeg.exe
// ## Inputs:
// - fullFileName
// return
// {
// "error":"",
// "result":true,
// }
//
//export SetFFmpeg
func SetFFmpeg(fullFileName *C.char) *C.char {
f := C.GoString(fullFileName)

_, err := os.Stat(f)
if os.IsNotExist(err) {
return WithJSON(false, err)
}
sdk.CmdFFmpeg = C.GoString(fullFileName)
return WithJSON(true, nil)
}
18 changes: 14 additions & 4 deletions winsdk/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Status struct {
Error string
Completed bool
LookupHash string
wg sync.WaitGroup
wg *sync.WaitGroup
}

type StatusCallback struct {
Expand Down Expand Up @@ -73,7 +73,11 @@ func (c *StatusCallback) InProgress(allocationID, remotePath string, op int, com
s.LookupHash = lookupHash
if completedBytes >= s.TotalBytes {
s.Completed = true
s.wg.Done()
if s.wg != nil {
s.wg.Done()
s.wg = nil
}

}
}

Expand All @@ -83,7 +87,10 @@ func (c *StatusCallback) Error(allocationID string, remotePath string, op int, e
s := c.getStatus(lookupHash)
s.Error = err.Error()
s.LookupHash = lookupHash
s.wg.Done()
if s.wg != nil {
s.wg.Done()
s.wg = nil
}
}

func (c *StatusCallback) Completed(allocationID, remotePath string, filename string, mimetype string, size int, op int) {
Expand All @@ -93,7 +100,10 @@ func (c *StatusCallback) Completed(allocationID, remotePath string, filename str
s.Completed = true
s.LookupHash = lookupHash
s.CompletedBytes = s.TotalBytes
s.wg.Done()
if s.wg != nil {
s.wg.Done()
s.wg = nil
}
}

func (c *StatusCallback) CommitMetaCompleted(request, response string, err error) {
Expand Down
5 changes: 5 additions & 0 deletions winsdk/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ func GetFileMeta(allocationID, path *C.char) *C.char {
//
//export BulkUpload
func BulkUpload(allocationID, files *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: ", r)
}
}()
allocID := C.GoString(allocationID)
workdir, _ := os.UserHomeDir()
jsFiles := C.GoString(files)
Expand Down
2 changes: 2 additions & 0 deletions winsdk/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"

"github.com/0chain/gosdk/zboxcore/sdk"
lru "github.com/hashicorp/golang-lru/v2"
Expand Down Expand Up @@ -219,6 +220,7 @@ func downloadBlocks(remotePath string, f *sdk.ConsolidatedFileMeta, ra httpRange
// if os.IsNotExist(err) {
statusBar := NewStatusBar(statusDownload, key)
status := statusBar.getStatus(key)
status.wg = &sync.WaitGroup{}
status.wg.Add(1)
log.Info("win: download blocks to ", mf)
err = alloc.DownloadFileByBlock(mf, remotePath, startBlock, endBlock, numBlocks, false, statusBar, true)
Expand Down
10 changes: 10 additions & 0 deletions zboxcore/sdk/chunk_upload_vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !windows
// +build !windows

package sdk

import "syscall"

var (
sysProcAttr = &syscall.SysProcAttr{}
)
10 changes: 10 additions & 0 deletions zboxcore/sdk/chunk_upload_vars_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build windows
// +build windows

package sdk

import "syscall"

var (
sysProcAttr = &syscall.SysProcAttr{HideWindow: true}
)
7 changes: 6 additions & 1 deletion zboxcore/sdk/chunked_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -36,6 +37,7 @@ const (
)

var (
CmdFFmpeg = "ffmpeg"
// DefaultHashFunc default hash method for stream merkle tree
DefaultHashFunc = func(left, right string) string {
return coreEncryption.Hash(left + right)
Expand Down Expand Up @@ -101,12 +103,15 @@ func CreateChunkedUpload(
}

if webStreaming {
newFileReader, newFileMeta, err := TranscodeWebStreaming(fileReader, fileMeta)
newFileReader, newFileMeta, f, err := TranscodeWebStreaming(workdir, fileReader, fileMeta)
defer os.Remove(f)

if err != nil {
return nil, thrown.New("upload_failed", err.Error())
}
fileMeta = *newFileMeta
fileReader = newFileReader

}

err := ValidateRemoteFileName(fileMeta.RemoteName)
Expand Down
50 changes: 32 additions & 18 deletions zboxcore/sdk/chunked_upload_web_streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,64 @@
"bufio"
"bytes"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"strings"

thrown "github.com/0chain/errors"
"github.com/0chain/gosdk/zboxcore/logger"
)

// Converting the video file to fmp4 format for web streaming
func TranscodeWebStreaming(fileReader io.Reader, fileMeta FileMeta) (io.Reader, *FileMeta, error) {
var stdOut bytes.Buffer
func TranscodeWebStreaming(workdir string, fileReader io.Reader, fileMeta FileMeta) (io.Reader, *FileMeta, string, error) {
var stdErr bytes.Buffer

args := []string{"-i", fileMeta.Path, "-g", "30", "-f", "mp4", "-movflags", "frag_keyframe+empty_moov", "pipe:1"}
cmdFfmpeg := exec.Command("ffmpeg", args...)
outDir := filepath.Join(workdir, ".zcn", "transcode")
// create ./zcn/transcode folder if it doesn't exists
os.MkdirAll(outDir, 0644) //nolint: errcheck
fileName := filepath.Join(outDir, fileMeta.RemoteName)

cmdFfmpeg.Stdout = bufio.NewWriter(&stdOut)
cmdFfmpeg.Stderr = bufio.NewWriter(&stdErr)
logger.Logger.Info("transcode: ", fileName)

err := cmdFfmpeg.Run()
args := []string{"-i", fileMeta.Path, "-f", "mp4", "-movflags", "frag_keyframe+empty_moov+default_base_moof", fileName, "-y"}
cmd := exec.Command(CmdFFmpeg, args...)
cmd.Stderr = bufio.NewWriter(&stdErr)
cmd.SysProcAttr = sysProcAttr
err := cmd.Run()
defer cmd.Process.Kill()

Check failure on line 33 in zboxcore/sdk/chunked_upload_web_streaming.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `cmd.Process.Kill` is not checked (errcheck)

if err != nil {
logger.Logger.Error(err)
return nil, nil, thrown.New("Transcoding Failed: ", err.Error())
logger.Logger.Error(err, stdErr.String())
return nil, nil, "", thrown.New("Transcoding Failed: ", err.Error())
}

trascodedBufSlice := stdOut.Bytes()
transcodedFileReader := bytes.NewReader(trascodedBufSlice)
// open file reader with readonly
r, err := os.Open(fileName)

remoteName, remotePath := getRemoteNameAndRemotePath(fileMeta.RemoteName, fileMeta.RemotePath)
if err != nil {
return nil, nil, fileName, err
}

fi, err := r.Stat()
if err != nil {
return nil, nil, fileName, err
}

transcodedFileMeta := &FileMeta{
MimeType: "video/fmp4",
fm := &FileMeta{
MimeType: "video/mp4",
Path: fileMeta.Path,
ThumbnailPath: fileMeta.ThumbnailPath,
ActualHash: fileMeta.ActualHash,
ActualSize: int64(len(trascodedBufSlice)),
ActualSize: fi.Size(),
ActualThumbnailSize: fileMeta.ActualThumbnailSize,
ActualThumbnailHash: fileMeta.ActualThumbnailHash,
RemoteName: remoteName,
RemotePath: remotePath,
RemoteName: fileMeta.RemoteName,
RemotePath: fileMeta.RemotePath,
}

return transcodedFileReader, transcodedFileMeta, nil
return r, fm, fileName, nil
}

func getRemoteNameAndRemotePath(remoteName string, remotePath string) (string, string) {
Expand Down
Loading