Skip to content

Commit

Permalink
Check audio codec on uploaded videos (#1414)
Browse files Browse the repository at this point in the history
* lecture halls linked

* removed hardcoding

* Added verification through regex

* Regex fixed, no more undercase letters

* Auxiliary file created for regex

* Any removed for ESLint

* Prettier errors fixed

* Script no longer global

* Auxilliary file works now

* ESLint fixes

* Reset web directory

* Reset dependency lock

* Checked again

* Reset dependency lock again

* These seemed to have changed too

* Missed one

* Files replaced with default ones

* Something is wrong

* File revert rereverted

* Codec change, further testing needed

* Audio codec check and transcode

* Get functions for codecs merged

* getCodec more compact now

---------

Co-authored-by: Karakoc <[email protected]>
  • Loading branch information
KemalKrKX and Karakoc authored Nov 26, 2024
1 parent aa013f6 commit 62d6c15
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
11 changes: 9 additions & 2 deletions worker/worker/ffmegtools.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package worker

import (
"fmt"
"github.com/tidwall/gjson"
"os/exec"
)
Expand All @@ -14,12 +15,18 @@ func getDuration(file string) (float64, error) {
return gjson.Get(probe, "format.duration").Float(), nil
}

func getCodec(file string) (string, error) {
func getCodec(file string, codecType string) (string, error) {
probe, err := probe(file)
if err != nil {
return "", err
}
return gjson.Get(probe, "streams.0.codec_name").String(), nil
nStreams := gjson.Get(probe, "streams.#").Int()
for i := 0; i < int(nStreams); i++ {
if gjson.Get(probe, fmt.Sprintf("streams.%d.codec_type", i)).String() == codecType {
return gjson.Get(probe, fmt.Sprintf("streams.%d.codec_name", i)).String(), nil
}
}
return "", fmt.Errorf("no %s stream found", codecType)
}

func getLevel(file string) (string, error) {
Expand Down
15 changes: 13 additions & 2 deletions worker/worker/ffmpegtools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func TestGetDuration(t *testing.T) {
}
}

func TestGetCodec(t *testing.T) {
codec, err := getCodec("testvid.mp4")
func TestGetVideoCodec(t *testing.T) {
codec, err := getCodec("testvid.mp4", "video")
if err != nil {
t.Error(err)
t.FailNow()
Expand All @@ -24,6 +24,17 @@ func TestGetCodec(t *testing.T) {
}
}

func TestGetAudioCodec(t *testing.T) {
codec, err := getCodec("testvid.mp4", "audio")
if err != nil {
t.Error(err)
t.FailNow()
}
if codec != "aac" {
t.Errorf("codec should be aac but is %s", codec)
}
}

func TestGetLevel(t *testing.T) {
l, err := getLevel("testvid.mp4")
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion worker/worker/request_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,22 @@ func HandleUploadRestReq(streamInfo *pb.GetStreamInfoForUploadResponse, localFil
log.Debugf("Wrong container: %s, converting", container)
}

if codec, err := getCodec(localFile); err != nil {
if codec, err := getCodec(localFile, "video"); err != nil {
log.WithError(err).Warn("Error getting codec")
needsConversion = true
} else if codec != "h264" {
needsConversion = true
log.Debugf("wrong codec: %s, converting", codec)
}

if codec, err := getCodec(localFile, "audio"); err != nil {
log.WithError(err).Warn("Error getting codec")
needsConversion = true
} else if codec != "aac" {
needsConversion = true
log.Debugf("wrong codec: %s, converting", codec)
}

level, err := getLevel(localFile)
if err != nil {
log.WithError(err).Warn("Error getting level")
Expand Down

0 comments on commit 62d6c15

Please sign in to comment.