-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package ffmpeg | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/url" | ||
"os/exec" | ||
|
||
"github.com/AlexxIT/go2rtc/internal/ffmpeg/hardware" | ||
"github.com/AlexxIT/go2rtc/pkg/core" | ||
"github.com/AlexxIT/go2rtc/pkg/ffmpeg" | ||
"github.com/AlexxIT/go2rtc/pkg/shell" | ||
) | ||
|
||
func TranscodeToJPEG(b []byte, query url.Values) ([]byte, error) { | ||
ffmpegArgs := parseQuery(query) | ||
cmdArgs := shell.QuoteSplit(ffmpegArgs.String()) | ||
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) | ||
cmd.Stdin = bytes.NewBuffer(b) | ||
return cmd.Output() | ||
} | ||
|
||
func parseQuery(query url.Values) *ffmpeg.Args { | ||
args := &ffmpeg.Args{ | ||
Bin: defaults["bin"], | ||
Global: defaults["global"], | ||
Input: "-i -", | ||
Codecs: []string{defaults["mjpeg"]}, | ||
Output: defaults["output/mjpeg"], | ||
} | ||
|
||
var width = -1 | ||
var height = -1 | ||
var r, hw string | ||
|
||
for k, v := range query { | ||
switch k { | ||
case "width", "w": | ||
width = core.Atoi(v[0]) | ||
case "height", "h": | ||
height = core.Atoi(v[0]) | ||
case "rotate": | ||
r = v[0] | ||
case "hardware", "hw": | ||
hw = v[0] | ||
} | ||
} | ||
|
||
if width > 0 || height > 0 { | ||
args.AddFilter(fmt.Sprintf("scale=%d:%d", width, height)) | ||
} | ||
|
||
if r != "" { | ||
switch r { | ||
case "90": | ||
args.AddFilter("transpose=1") // 90 degrees clockwise | ||
case "180": | ||
args.AddFilter("transpose=1,transpose=1") | ||
case "-90", "270": | ||
args.AddFilter("transpose=2") // 90 degrees counterclockwise | ||
} | ||
} | ||
|
||
if hw != "" { | ||
hardware.MakeHardware(args, hw, defaults) | ||
} | ||
|
||
return args | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ffmpeg | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseQuery(t *testing.T) { | ||
args := parseQuery(nil) | ||
require.Equal(t, `ffmpeg -hide_banner -i - -c:v mjpeg -f mjpeg -`, args.String()) | ||
|
||
query, err := url.ParseQuery("h=480") | ||
require.Nil(t, err) | ||
args = parseQuery(query) | ||
require.Equal(t, `ffmpeg -hide_banner -i - -c:v mjpeg -vf "scale=-1:480" -f mjpeg -`, args.String()) | ||
|
||
query, err = url.ParseQuery("hw=vaapi") | ||
require.Nil(t, err) | ||
args = parseQuery(query) | ||
require.Equal(t, `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -i - -c:v mjpeg_vaapi -vf "format=vaapi|nv12,hwupload" -f mjpeg -`, args.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters