multiple outputs for ffmpeg segment transcoding

This commit is contained in:
Eric Tang
2017-08-01 15:24:28 -04:00
parent d08b663c95
commit 76b8a151cb
2 changed files with 43 additions and 14 deletions
+23 -11
View File
@@ -12,23 +12,27 @@ import (
"github.com/golang/glog"
)
//SegmentTranscoder transcodes segments individually. This is a simple wrapper for calling FFMpeg on the command line.
type FFMpegSegmentTranscoder struct {
type TranscoderProfile struct {
Bitrate string
Framerate uint
Resolution string
}
//SegmentTranscoder transcodes segments individually. This is a simple wrapper for calling FFMpeg on the command line.
type FFMpegSegmentTranscoder struct {
tProfiles []TranscoderProfile
ffmpegPath string
workDir string
}
func NewFFMpegSegmentTranscoder(bitr string, framer uint, res string, ffmpegp string, workd string) *FFMpegSegmentTranscoder {
return &FFMpegSegmentTranscoder{Bitrate: bitr, Framerate: framer, Resolution: res, ffmpegPath: ffmpegp, workDir: workd}
func NewFFMpegSegmentTranscoder(ps []TranscoderProfile, ffmpegp, workd string) *FFMpegSegmentTranscoder {
return &FFMpegSegmentTranscoder{tProfiles: ps, ffmpegPath: ffmpegp, workDir: workd}
}
func (t *FFMpegSegmentTranscoder) Transcode(d []byte) ([]byte, error) {
func (t *FFMpegSegmentTranscoder) Transcode(d []byte) ([][]byte, error) {
//Assume d is in the right format, write it to disk
inName := randName()
outName := fmt.Sprintf("out%v", inName)
// outName := fmt.Sprintf("out%v", inName)
if _, err := os.Stat(t.workDir); os.IsNotExist(err) {
err := os.Mkdir(t.workDir, 0700)
if err != nil {
@@ -45,19 +49,27 @@ func (t *FFMpegSegmentTranscoder) Transcode(d []byte) ([]byte, error) {
//Invoke ffmpeg
var cmd *exec.Cmd
//ffmpeg -i seg.ts -c:v libx264 -s 426:240 -r 30 -mpegts_copyts 1 -minrate 700k -maxrate 700k -bufsize 700k -threads 1 out3.ts
cmd = exec.Command(path.Join(t.ffmpegPath, "ffmpeg"), "-i", path.Join(t.workDir, inName), "-c:v", "libx264", "-s", t.Resolution, "-mpegts_copyts", "1", "-minrate", t.Bitrate, "-maxrate", t.Bitrate, "-bufsize", t.Bitrate, "-r", fmt.Sprintf("%d", t.Framerate), "-threads", "1", path.Join(t.workDir, outName))
args := make([]string, 0, 0)
for i, p := range t.tProfiles {
args = append(args, []string{"-c:v", "libx264", "-s", p.Resolution, "-mpegts_copyts", "1", "-minrate", p.Bitrate, "-maxrate", p.Bitrate, "-bufsize", p.Bitrate, "-r", fmt.Sprintf("%d", p.Framerate), "-threads", "1", path.Join(t.workDir, fmt.Sprintf("out%v%v", i, inName))}...)
}
cmd = exec.Command(path.Join(t.ffmpegPath, "ffmpeg"), append([]string{"-i", path.Join(t.workDir, inName)}, args...)...)
if err := cmd.Run(); err != nil {
glog.Errorf("Cannot start ffmpeg command: %v", err)
return nil, err
}
dout, err := ioutil.ReadFile(path.Join(t.workDir, outName))
if err != nil {
glog.Errorf("Cannot read transcode output: %v", err)
dout := make([][]byte, len(t.tProfiles), len(t.tProfiles))
for i, _ := range t.tProfiles {
d, err := ioutil.ReadFile(path.Join(t.workDir, fmt.Sprintf("out%v%v", i, inName)))
if err != nil {
glog.Errorf("Cannot read transcode output: %v", err)
}
dout[i] = d
os.Remove(path.Join(t.workDir, fmt.Sprintf("out%v%v", i, inName)))
}
os.Remove(path.Join(t.workDir, inName))
os.Remove(path.Join(t.workDir, outName))
return dout, nil
}