Golang with ffmpeg dynamic video Encode

huangapple go评论89阅读模式
英文:

Golang with ffmpeg dynamic video Encode

问题

我正在尝试使用Golang的ffmpeg对视频进行编码。但是无法获取视频文件,显示以下错误:

数字文字中的无效字符'-'

这是我的代码:

package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/xfrr/goffmpeg/transcoder"
)

type Encode struct {
	Video string `json:"video"`
}

func encodeFfmpeg(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	fmt.Println("Encodeing Started")
	var encode Encode
	video := json.NewDecoder(r.Body).Decode(encode)
	file, err := os.Open(video)
	if err != nil {
		log.Fatalln(err)
	}

	defer file.Close()
	buf, err := ioutil.ReadAll(file)
	if err != nil {
		log.Fatalln(err)
	}


	cmd := exec.Command("ffmpeg",
		"-i", "pipe:0", // take stdin as input
		"-c:a", "libmp3lame", // use mp3 lame codec
		"-f", "avi",
		"pipe:1",
	)

	resultBuffer := bytes.NewBuffer(make([]byte, 5*1024*1024)) // pre allocate 5MiB buffer

	cmd.Stderr = os.Stderr    // bind log stream to stderr
	cmd.Stdout = resultBuffer // stdout result will be written here

	stdin, err := cmd.StdinPipe() // Open stdin pipe
	if err != nil {
		log.Fatalln(err)
	}

	err = cmd.Start() // Start a process on another goroutine
	if err != nil {
		log.Fatalln(err)
	}

	_, err = stdin.Write(buf) // pump audio data to stdin pipe
	if err != nil {
		log.Fatalln(err)
	}

	err = stdin.Close() // close the stdin, or ffmpeg will wait forever
	if err != nil {
		log.Fatalln(err)
	}

	err = cmd.Wait() // wait until ffmpeg finish
	if err != nil {
		log.Fatalln(err)
	}

	outputFile, err := os.Create(encodeFile) // create new file
	if err != nil {
		log.Fatalln(err)
	}
	defer outputFile.Close()

	_, err = outputFile.Write(resultBuffer.Bytes()) // write result buffer to file
	if err != nil {
		log.Fatalln(err)
	}

	json.NewEncoder(w).Encode(map[string]interface{}{
		"status":     "success",
		"statusCode": 200,
		"data":       "Successfully Encoded file",
	})
}


func main() {
	router := mux.NewRouter()

	router.HandleFunc("/encode", encodeFfmpeg).Methods("POST")
	//router.HandleFunc("/rtsp", rtsp).Methods("POST")

	// config port
	fmt.Printf("Starting server at 8080 \n")
	http.ListenAndServe(":8080", router)
}

video.mp4文件在请求体中未显示。请帮助我解决这个问题。

英文:

Am trying to encode the videos with Golang ffmpeg. Am not able to get the video file, it shows this error

>invalid character '-' in numeric literal

Postman:
Golang with ffmpeg dynamic video Encode

This is my code:

package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/xfrr/goffmpeg/transcoder"
)
type Encode struct {
Video string `json:"video"`
}
func encodeFfmpeg(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Println("Encodeing Started")
var encode Encode
video := json.NewDecoder(r.Body).Decode(encode)
file, err := os.Open(video)
if err != nil {
log.Fatalln(err)
}
defer file.Close()
buf, err := ioutil.ReadAll(file)
if err != nil {
log.Fatalln(err)
}
cmd := exec.Command("ffmpeg",
"-i", "pipe:0", // take stdin as input
"-c:a", "libmp3lame", // use mp3 lame codec
"-f", "avi",
"pipe:1",
)
resultBuffer := bytes.NewBuffer(make([]byte, 5*1024*1024)) // pre allocate 5MiB buffer
cmd.Stderr = os.Stderr    // bind log stream to stderr
cmd.Stdout = resultBuffer // stdout result will be written here
stdin, err := cmd.StdinPipe() // Open stdin pipe
if err != nil {
log.Fatalln(err)
}
err = cmd.Start() // Start a process on another goroutine
if err != nil {
log.Fatalln(err)
}
_, err = stdin.Write(buf) // pump audio data to stdin pipe
if err != nil {
log.Fatalln(err)
}
err = stdin.Close() // close the stdin, or ffmpeg will wait forever
if err != nil {
log.Fatalln(err)
}
err = cmd.Wait() // wait until ffmpeg finish
if err != nil {
log.Fatalln(err)
}
outputFile, err := os.Create(encodeFile) // create new file
if err != nil {
log.Fatalln(err)
}
defer outputFile.Close()
_, err = outputFile.Write(resultBuffer.Bytes()) // write result buffer to file
if err != nil {
log.Fatalln(err)
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status":     "success",
"statusCode": 200,
"data":       "Successfully Encoded file",
})
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/encode", encodeFfmpeg).Methods("POST")
//router.HandleFunc("/rtsp", rtsp).Methods("POST")
// config port
fmt.Printf("Starting server at 8080 \n")
http.ListenAndServe(":8080", router)
}

video.mp4 file not showing in request body. Please help me to solve this issue

答案1

得分: 1

json.NewDecoder(r.Body).Decode(encode) 返回的是 error 而不是视频。另外,你可以通过以下方式从表单中访问文件:

file, header, err := r.FormFile("video")
英文:

json.NewDecoder(r.Body).Decode(encode) returns error not video and another thing is that you send your data in form so you can access file from form like:

file, header, err := r.FormFile("video")

huangapple
  • 本文由 发表于 2021年10月12日 19:08:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/69539491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定