Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:ZeStream/zestream-server into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
abskrj committed Jan 12, 2023
2 parents c861ac2 + 060becd commit 726e215
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 14 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: golangci-lint
on:
push:
branches: ["master","dev"]
pull_request:
branches: ["master","dev"]
jobs:
golangci:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.19
- uses: actions/checkout@v3
- name: Tidy
run: go mod tidy
- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.46.2
args: --timeout=3m
2 changes: 2 additions & 0 deletions configs/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
issues:
new: true
2 changes: 2 additions & 0 deletions constants/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const (
const DashOutputExt = ".mpd"
const MP4Box = "MP4Box"
const DEFAULT_THUMBNAIL_TIMESTAMP = "00:00:02"
const Overlay = "overlay"
const Scale = "scale"

var AudioFileTypeMap = map[FILE_TYPE]string{
Audio192K: "_audio192k.m4a",
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"log"
"os"
"zestream-server/configs"
"zestream-server/constants"
Expand All @@ -19,11 +20,18 @@ func main() {

go service.VideoProcessConsumer(ch, q)

r.Run(":" + port)
err := r.Run(":" + port)
failOnError(err)

defer func() {
defer conn.Close()
defer ch.Close()
defer cancel()
}()
}

func failOnError(err error) {
if err != nil {
log.Fatalln(err)
}
}
2 changes: 1 addition & 1 deletion service/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ func processVideo(video *types.Video) {

utils.Fetch(video.Src, fileName)

generateDash(fileName)
generateDash(fileName, video.Watermark)
}
72 changes: 64 additions & 8 deletions service/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"strings"
"sync"
"zestream-server/constants"
"zestream-server/types"
"zestream-server/utils"

ffmpeg "github.com/u2takey/ffmpeg-go"
)

func generateDash(fileName string) {
func generateDash(fileName string, watermark types.WaterMark) {
targetFile, err := utils.GetDownloadFilePathName(fileName)
if err != nil {
log.Println(err)
Expand All @@ -32,7 +33,7 @@ func generateDash(fileName string) {

generateAudioFiles(targetFile, outputPath, &wg)

generateVideoFiles(targetFile, outputPath, &wg)
generateVideoFiles(targetFile, outputPath, watermark, &wg)

generateThumbnailFiles(targetFile, outputPath, &wg)

Expand All @@ -51,11 +52,11 @@ func generateAudioFiles(targetFile string, outputPath string, wg *sync.WaitGroup
}
}

func generateVideoFiles(targetFile string, outputPath string, wg *sync.WaitGroup) {
func generateVideoFiles(targetFile string, outputPath string, watermark types.WaterMark, wg *sync.WaitGroup) {
for fileType, filePrefix := range constants.VideoFileTypeMap {
var outputFile = outputPath + filePrefix

go generateMultiBitrateVideo(targetFile, outputFile, fileType, wg)
go generateMultiBitrateVideo(targetFile, outputFile, fileType, watermark, wg)
}
}

Expand All @@ -68,7 +69,7 @@ func generateThumbnailFiles(targetFile string, outputPath string, wg *sync.WaitG
}

func generateMultiBitrateAudio(targetFile string, outputFile string, fileType constants.FILE_TYPE, wg *sync.WaitGroup) {
ffmpeg.Input(targetFile, ffmpeg.KwArgs{
err := ffmpeg.Input(targetFile, ffmpeg.KwArgs{
constants.AudioKwargs[constants.HWAccel]: constants.FFmpegConfig[constants.HWAccel],
}).
Output(outputFile, ffmpeg.KwArgs{
Expand All @@ -78,12 +79,17 @@ func generateMultiBitrateAudio(targetFile string, outputFile string, fileType co
constants.AudioKwargs[constants.VideoExclusion]: constants.FFmpegConfig[constants.VideoExclusion],
}).
OverWriteOutput().ErrorToStdOut().Run()
if err != nil {
m := "error generating multi bitrate audio"
log.Println(m, err)
}

wg.Done()
}

func generateMultiBitrateVideo(targetFile string, outputFile string, fileType constants.FILE_TYPE, wg *sync.WaitGroup) {
ffmpeg.Input(targetFile).
func generateMultiBitrateVideo(targetFile string, outputFile string, fileType constants.FILE_TYPE, watermark types.WaterMark, wg *sync.WaitGroup) {

err := getInput(targetFile, watermark).
Output(outputFile, ffmpeg.KwArgs{
constants.VideoKwargs[constants.Preset]: constants.FFmpegConfig[constants.Preset],
constants.VideoKwargs[constants.Tune]: constants.FFmpegConfig[constants.Tune],
Expand All @@ -98,12 +104,17 @@ func generateMultiBitrateVideo(targetFile string, outputFile string, fileType co
ErrorToStdOut().
Run()

if err != nil {
m := "error generating multi bitrate video"
log.Println(m, err)
}

wg.Done()
}

// generateThumbnail generates a thumbnail at given timestamp, from the target file and write it to output file
func generateThumbnails(targetFile string, outputFile string, timeStamp string, wg *sync.WaitGroup) {
ffmpeg.Input(targetFile).
err := ffmpeg.Input(targetFile).
Output(outputFile, ffmpeg.KwArgs{
constants.VideoKwargs[constants.ScreenShot]: timeStamp,
constants.VideoKwargs[constants.VideoFrames]: constants.FFmpegConfig[constants.VideoFrames],
Expand All @@ -112,6 +123,11 @@ func generateThumbnails(targetFile string, outputFile string, timeStamp string,
ErrorToStdOut().
Run()

if err != nil {
m := "error generating thumbnail"
log.Println(m, err)
}

wg.Done()
}

Expand Down Expand Up @@ -149,10 +165,50 @@ func generateMPD(outputPath string) {
log.Println(string(o))
}

// deleteVideoFiles deletes the videos given in the path
func deleteVideoFiles(outputPath string) {
for _, filePrefix := range constants.VideoFileTypeMap {
var file = outputPath + filePrefix

utils.DeleteFile(file)
}
}

// Returns input based on if the watermark is needed or not
func getInput(targetFile string, watermark types.WaterMark) *ffmpeg.Stream {

input := ffmpeg.Input(targetFile)

if !watermark.IsEmpty() {
watermarkPath, err := utils.GetDownloadFilePathName(watermark.ID)
if err != nil {
log.Println(err)
}

filterArgs := "" + watermark.Position.X + ":" + watermark.Position.Y + ""

return ffmpeg.Filter(
[]*ffmpeg.Stream{
input,
getOverlay(watermarkPath, watermark),
}, constants.Overlay, ffmpeg.Args{filterArgs})
}
return input
}

// Returns an overlay of watermark which can be used in Filter
func getOverlay(watermarkPath string, watermark types.WaterMark) *ffmpeg.Stream {
overlayArgs := "" + watermark.Dimension.X + ":" + watermark.Dimension.Y + ""
return ffmpeg.Input(watermarkPath).Filter(constants.Scale, ffmpeg.Args{overlayArgs})
}

// checkFileExistsAndAppendToBuffer checks if the given output file exits, then appends the
// path to buffer.
func checkFileExistsAndAppendToBuffer(fileArgs *bytes.Buffer, outputPath string, fileTypes map[constants.FILE_TYPE]string) {
for _, filePrefix := range fileTypes {
var outputFile = outputPath + filePrefix
if utils.IsFileValid(outputFile) {
fileArgs.WriteString(utils.WrapStringInQuotes(outputFile))
}
}
}
44 changes: 41 additions & 3 deletions types/video.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
package types

type Video struct {
ID string `json:"id"`
Src string `json:"src"`
Type string `json:"type"`
ID string `json:"id"`
Src string `json:"src"`
Type string `json:"type"`
Watermark WaterMark `json:"watermark"`
}

type WaterMark struct {
ID string `json:"id"`
Src string `json:"src"`
Dimension Dimension `json:"dimension"`
Position Dimension `json:"position"`
}

type Dimension struct {
X string `json:"x"`
Y string `json:"y"`
}

type DIMENSION int

const (
X DIMENSION = iota
Y
)

var WaterMarkSizeMap = map[DIMENSION]string{
X: "x",
Y: "y",
}

var WaterMarkPositionMap = map[DIMENSION]string{
X: "x",
Y: "y",
}

func (w *WaterMark) IsEmpty() bool {
if w.ID == "" {
return true
}

return false
}
6 changes: 5 additions & 1 deletion utils/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ func TestFetch(t *testing.T) {

cwd, _ := os.Getwd()

os.Remove(path.Join(cwd, constants.DOWNLOAD_FILE_PATH_PREFIX))
err = os.Remove(path.Join(cwd, constants.DOWNLOAD_FILE_PATH_PREFIX))

if err != nil {
t.Fatal(err)
}
}

0 comments on commit 726e215

Please sign in to comment.