Skip to content

Commit

Permalink
zencoder: raise errors gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioribeiro committed Nov 18, 2016
1 parent 59e6e75 commit 2d69d00
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions provider/zencoder/zencoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,49 +98,65 @@ func (z *zencoderProvider) buildOutputs(job *db.Job) ([]*zencoder.OutputSettings
zencoderOutputs = append(zencoderOutputs, &zencoderOutput)
}
if hlsOutputs > 0 {
z.optimizeOutputsForHLS(zencoderOutputs)
zencoderOutputs, err := z.optimizeOutputsForHLS(zencoderOutputs)
outputsWithHLSPlaylist := make([]*zencoder.OutputSettings, len(zencoderOutputs)+1)
copy(outputsWithHLSPlaylist, zencoderOutputs)
hlsPlaylist, err := z.buildHLSPlaylist(zencoderOutputs, hlsOutputs, job)
if err != nil {
return nil, fmt.Errorf("Error building hls master playlist: %s", err.Error())
}
outputsWithHLSPlaylist[len(zencoderOutputs)] = &hlsPlaylist
zencoderOutputs = outputsWithHLSPlaylist
return outputsWithHLSPlaylist, nil
}
return zencoderOutputs, nil
}

func (z *zencoderProvider) optimizeOutputsForHLS(outputs []*zencoder.OutputSettings) {
func (z *zencoderProvider) optimizeOutputsForHLS(outputs []*zencoder.OutputSettings) ([]*zencoder.OutputSettings, error) {
for i, hlsOutput := range outputs {
if hlsOutput.Format == "ts" {
for _, mp4Output := range outputs {
if mp4Output.Format == "mp4" && z.isOutputCompatible(hlsOutput, mp4Output) {
newHlsOutput := zencoder.OutputSettings{
Filename: hlsOutput.Filename,
Label: hlsOutput.Label,
BaseUrl: hlsOutput.BaseUrl,
Format: hlsOutput.Format,
Source: mp4Output.Label,
CopyAudio: true,
CopyVideo: true,
Type: "segmented",
if mp4Output.Format == "mp4" {
isCompatible, err := z.isOutputCompatible(hlsOutput, mp4Output)
if err != nil {
return nil, err
} else if isCompatible {
newHlsOutput := zencoder.OutputSettings{
Filename: hlsOutput.Filename,
Label: hlsOutput.Label,
BaseUrl: hlsOutput.BaseUrl,
Format: hlsOutput.Format,
Source: mp4Output.Label,
CopyAudio: true,
CopyVideo: true,
Type: "segmented",
}
outputs[i] = &newHlsOutput
}
outputs[i] = &newHlsOutput
}
}
}
}
return outputs, nil
}

func (z *zencoderProvider) isOutputCompatible(hlsOutput, mp4Output *zencoder.OutputSettings) bool {
localHlsPreset, _ := z.GetPreset(hlsOutput.Label)
func (z *zencoderProvider) isOutputCompatible(hlsOutput, mp4Output *zencoder.OutputSettings) (bool, error) {
localHlsPreset, err := z.GetPreset(hlsOutput.Label)
if err != nil {
return false, err
}
hls := localHlsPreset.(*db.LocalPreset)

localMp4Preset, _ := z.GetPreset(hlsOutput.Label)
if err != nil {
return false, err
}
mp4 := localMp4Preset.(*db.LocalPreset)

return reflect.DeepEqual(hls.Preset.Video, mp4.Preset.Video) && reflect.DeepEqual(hls.Preset.Audio, mp4.Preset.Audio) && hls.Preset.RateControl == mp4.Preset.RateControl
isCompatible := reflect.DeepEqual(hls.Preset.Video, mp4.Preset.Video) &&
reflect.DeepEqual(hls.Preset.Audio, mp4.Preset.Audio) &&
hls.Preset.RateControl == mp4.Preset.RateControl

return isCompatible, nil
}

func (z *zencoderProvider) buildHLSPlaylist(outputs []*zencoder.OutputSettings, hlsOutputs int, job *db.Job) (zencoder.OutputSettings, error) {
Expand Down

0 comments on commit 2d69d00

Please sign in to comment.