-
Notifications
You must be signed in to change notification settings - Fork 485
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
12 changed files
with
419 additions
and
16 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 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 |
---|---|---|
@@ -1,6 +1,41 @@ | ||
## Devices Windows | ||
|
||
``` | ||
>ffmpeg -hide_banner -f dshow -list_options true -i video="VMware Virtual USB Video Device" | ||
[dshow @ 0000025695e52900] DirectShow video device options (from video devices) | ||
[dshow @ 0000025695e52900] Pin "Record" (alternative pin name "0") | ||
[dshow @ 0000025695e52900] pixel_format=yuyv422 min s=1280x720 fps=1 max s=1280x720 fps=10 | ||
[dshow @ 0000025695e52900] pixel_format=yuyv422 min s=1280x720 fps=1 max s=1280x720 fps=10 (tv, bt470bg/bt709/unknown, topleft) | ||
[dshow @ 0000025695e52900] pixel_format=nv12 min s=1280x720 fps=1 max s=1280x720 fps=23 | ||
[dshow @ 0000025695e52900] pixel_format=nv12 min s=1280x720 fps=1 max s=1280x720 fps=23 (tv, bt470bg/bt709/unknown, topleft) | ||
``` | ||
|
||
## Devices Mac | ||
|
||
``` | ||
% ./ffmpeg -hide_banner -f avfoundation -list_devices true -i "" | ||
[AVFoundation indev @ 0x7f8b1f504d80] AVFoundation video devices: | ||
[AVFoundation indev @ 0x7f8b1f504d80] [0] FaceTime HD Camera | ||
[AVFoundation indev @ 0x7f8b1f504d80] [1] Capture screen 0 | ||
[AVFoundation indev @ 0x7f8b1f504d80] AVFoundation audio devices: | ||
[AVFoundation indev @ 0x7f8b1f504d80] [0] Soundflower (2ch) | ||
[AVFoundation indev @ 0x7f8b1f504d80] [1] Built-in Microphone | ||
[AVFoundation indev @ 0x7f8b1f504d80] [2] Soundflower (64ch) | ||
``` | ||
|
||
## Devices Linux | ||
|
||
``` | ||
# ffmpeg -hide_banner -f v4l2 -list_formats all -i /dev/video0 | ||
[video4linux2,v4l2 @ 0x7f7de7c58bc0] Raw : yuyv422 : YUYV 4:2:2 : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960 | ||
[video4linux2,v4l2 @ 0x7f7de7c58bc0] Compressed: mjpeg : Motion-JPEG : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960 | ||
``` | ||
|
||
## Useful links | ||
|
||
- https://superuser.com/questions/564402/explanation-of-x264-tune | ||
- https://stackoverflow.com/questions/33624016/why-sliced-thread-affect-so-much-on-realtime-encoding-using-ffmpeg-x264 | ||
- https://codec.fandom.com/ru/wiki/X264_-_%D0%BE%D0%BF%D0%B8%D1%81%D0%B0%D0%BD%D0%B8%D0%B5_%D0%BA%D0%BB%D1%8E%D1%87%D0%B5%D0%B9_%D0%BA%D0%BE%D0%B4%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F | ||
- https://codec.fandom.com/ru/wiki/X264_-_описание_ключей_кодирования | ||
- https://html5test.com/ | ||
- https://trac.ffmpeg.org/wiki/Capture/Webcam | ||
- https://trac.ffmpeg.org/wiki/DirectShow |
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,63 @@ | ||
package ffmpeg | ||
|
||
import ( | ||
"bytes" | ||
"github.com/AlexxIT/go2rtc/pkg/streamer" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// https://trac.ffmpeg.org/wiki/Capture/Webcam | ||
const deviceInputPrefix = "-f avfoundation" | ||
|
||
func deviceInputSuffix(videoIdx, audioIdx int) string { | ||
video := findMedia(streamer.KindVideo, videoIdx) | ||
audio := findMedia(streamer.KindAudio, audioIdx) | ||
switch { | ||
case video != nil && audio != nil: | ||
return `"` + video.Title + `:` + audio.Title + `"` | ||
case video != nil: | ||
return `"` + video.Title + `"` | ||
case audio != nil: | ||
return `"` + audio.Title + `"` | ||
} | ||
return "" | ||
} | ||
|
||
func loadMedias() { | ||
cmd := exec.Command( | ||
tpl["bin"], "-hide_banner", "-list_devices", "true", "-f", "avfoundation", "-i", "dummy", | ||
) | ||
|
||
var buf bytes.Buffer | ||
cmd.Stderr = &buf | ||
_ = cmd.Run() | ||
|
||
var kind string | ||
|
||
lines := strings.Split(buf.String(), "\n") | ||
process: | ||
for _, line := range lines { | ||
switch { | ||
case strings.HasSuffix(line, "video devices:"): | ||
kind = streamer.KindVideo | ||
continue | ||
case strings.HasSuffix(line, "audio devices:"): | ||
kind = streamer.KindAudio | ||
continue | ||
case strings.HasPrefix(line, "dummy"): | ||
break process | ||
} | ||
|
||
// [AVFoundation indev @ 0x7fad54604380] [0] FaceTime HD Camera | ||
name := line[42:] | ||
media := loadMedia(kind, name) | ||
medias = append(medias, media) | ||
} | ||
} | ||
|
||
func loadMedia(kind, name string) *streamer.Media { | ||
return &streamer.Media{ | ||
Kind: kind, Title: name, | ||
} | ||
} |
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,36 @@ | ||
package ffmpeg | ||
|
||
import ( | ||
"github.com/AlexxIT/go2rtc/pkg/streamer" | ||
"github.com/rs/zerolog/log" | ||
"io/ioutil" | ||
"strings" | ||
) | ||
|
||
// https://trac.ffmpeg.org/wiki/Capture/Webcam | ||
const deviceInputPrefix = "-f v4l2" | ||
|
||
func deviceInputSuffix(videoIdx, audioIdx int) string { | ||
video := findMedia(streamer.KindVideo, videoIdx) | ||
return video.Title | ||
} | ||
|
||
func loadMedias() { | ||
files, err := ioutil.ReadDir("/dev") | ||
if err != nil { | ||
return | ||
} | ||
for _, file := range files { | ||
log.Trace().Msg("[ffmpeg] " + file.Name()) | ||
if strings.HasPrefix(file.Name(), streamer.KindVideo) { | ||
media := loadMedia(streamer.KindVideo, "/dev/"+file.Name()) | ||
medias = append(medias, media) | ||
} | ||
} | ||
} | ||
|
||
func loadMedia(kind, name string) *streamer.Media { | ||
return &streamer.Media{ | ||
Kind: kind, Title: name, | ||
} | ||
} |
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,59 @@ | ||
package ffmpeg | ||
|
||
import ( | ||
"bytes" | ||
"github.com/AlexxIT/go2rtc/pkg/streamer" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// https://trac.ffmpeg.org/wiki/DirectShow | ||
const deviceInputPrefix = "-f dshow" | ||
|
||
func deviceInputSuffix(videoIdx, audioIdx int) string { | ||
video := findMedia(streamer.KindVideo, videoIdx) | ||
audio := findMedia(streamer.KindAudio, audioIdx) | ||
switch { | ||
case video != nil && audio != nil: | ||
return `video="` + video.Title + `":audio=` + audio.Title + `"` | ||
case video != nil: | ||
return `video="` + video.Title + `"` | ||
case audio != nil: | ||
return `audio="` + audio.Title + `"` | ||
} | ||
return "" | ||
} | ||
|
||
func loadMedias() { | ||
cmd := exec.Command( | ||
tpl["bin"], "-hide_banner", "-list_devices", "true", "-f", "dshow", "-i", "", | ||
) | ||
|
||
var buf bytes.Buffer | ||
cmd.Stderr = &buf | ||
_ = cmd.Run() | ||
|
||
lines := strings.Split(buf.String(), "\r\n") | ||
for _, line := range lines { | ||
var kind string | ||
if strings.HasSuffix(line, "(video)") { | ||
kind = streamer.KindVideo | ||
} else if strings.HasSuffix(line, "(audio)") { | ||
kind = streamer.KindAudio | ||
} else { | ||
continue | ||
} | ||
|
||
// hope we have constant prefix and suffix sizes | ||
// [dshow @ 00000181e8d028c0] "VMware Virtual USB Video Device" (video) | ||
name := line[28 : len(line)-9] | ||
media := loadMedia(kind, name) | ||
medias = append(medias, media) | ||
} | ||
} | ||
|
||
func loadMedia(kind, name string) *streamer.Media { | ||
return &streamer.Media{ | ||
Kind: kind, Title: name, | ||
} | ||
} |
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,73 @@ | ||
package ffmpeg | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/AlexxIT/go2rtc/pkg/streamer" | ||
"github.com/rs/zerolog/log" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func getDevice(src string) (string, error) { | ||
if medias == nil { | ||
loadMedias() | ||
} | ||
|
||
input := deviceInputPrefix | ||
|
||
var videoIdx, audioIdx int | ||
if i := strings.IndexByte(src, '?'); i > 0 { | ||
query, err := url.ParseQuery(src[i+1:]) | ||
if err != nil { | ||
return "", err | ||
} | ||
for key, value := range query { | ||
switch key { | ||
case "video": | ||
videoIdx, _ = strconv.Atoi(value[0]) | ||
case "audio": | ||
audioIdx, _ = strconv.Atoi(value[0]) | ||
case "framerate": | ||
input += " -framerate " + value[0] | ||
case "resolution": | ||
input += " -video_size " + value[0] | ||
} | ||
} | ||
} | ||
|
||
input += " -i " + deviceInputSuffix(videoIdx, audioIdx) | ||
|
||
return input, nil | ||
} | ||
|
||
var medias []*streamer.Media | ||
|
||
func findMedia(kind string, index int) *streamer.Media { | ||
for _, media := range medias { | ||
if media.Kind != kind { | ||
continue | ||
} | ||
if index == 0 { | ||
return media | ||
} | ||
index-- | ||
} | ||
return nil | ||
} | ||
|
||
func handleDevices(w http.ResponseWriter, r *http.Request) { | ||
if medias == nil { | ||
loadMedias() | ||
} | ||
|
||
data, err := json.Marshal(medias) | ||
if err != nil { | ||
log.Error().Err(err).Msg("[api.ffmpeg]") | ||
return | ||
} | ||
if _, err = w.Write(data); err != nil { | ||
log.Error().Err(err).Msg("[api.ffmpeg]") | ||
} | ||
} |
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
Oops, something went wrong.