Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] Add audio transcoding with FFMPEG #47

Open
Mikescops opened this issue Jun 13, 2022 · 7 comments
Open

[Request] Add audio transcoding with FFMPEG #47

Mikescops opened this issue Jun 13, 2022 · 7 comments
Labels
enhancement New feature or request

Comments

@Mikescops
Copy link
Contributor

Currently, it's only possible to transcode video media, but it will be great to do the same thing with audio for devices that only support certain types of audio.

I made an experiment with converting everything to FLAC but so far it resulted in no audio being played...

	cmd := exec.Command(
		"ffmpeg",
		"-v", "0",
		"-re",
		"-i", in,
		"-f", "flac",
		"pipe:1",
	)
@alexballas
Copy link
Owner

alexballas commented Jun 14, 2022

Hey, thanks for raising this. Yes, that would be a nice addition. We'd just need to separate the logic from the video one. As for your example, check without the "-re" flag.

On a different note:
I also pushed a fix for an issue where it can trigger multiple versions of ffmpeg to run in parallel
334933a

@Mikescops
Copy link
Contributor Author

I tried with your fix and -re, but it still not working. Something goes wrong and it's unclear from wireshark :s

@Mikescops
Copy link
Contributor Author

This is what i did: https://github.com/alexballas/go2tv/pull/48/files

@alexballas
Copy link
Owner

I think this is a player issue. I've tested it with "gmediarender" (another handy tool to spin a DMR quickly) and it will play the encoded file only after encoding is complete. Same with BubbleUPNP. This is why I removed the -re flag. My TV on the other hand threw a "network error" which is the equivalent of "something went wrong".
I doubt it's something a proper ffmpeg audio container would fix, but I'll try out some other ffmpeg flag combinations just to be sure.

@Mikescops
Copy link
Contributor Author

Something I don't get is at which point ServeTranscodedStream opens a webserver to stream the file?

@alexballas
Copy link
Owner

In go, when setting up a HTTP server and handling requests, all you have to do is implement the http.Handler interface. https://pkg.go.dev/net/http#Handler

This mean that we're only have to deal with 2 arguments func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request)

  • ResponseWriter for writing back to the client
  • *Request to deal with request specific stuff.

Example:

func someRandomHandler(w http.ResponseWriter, r *http.Request) {
    if r.Method == "GET" {
		fmt.Fprintf(w, "Hello!")
    }
}

here we write "Hello!" back to w.

This is what I also do in Go2TV.

func ServeTranscodedStream(w io.Writer, input interface{}, ff *exec.Cmd) error { accepts an io.Writer argument which http.ResponseWriter also implements.

io.Writer is an interface that abstracts any form of data writing in Go.

When we call exec.Command we can configure the output of the command to be any io.Writer. This can be os.Stdout, normal files or even http.ResponseWriter as in our case.
The actual data copying is happening when we call ff.Run()

This is the flow:

  1. s.mux.HandleFunc(mURL.Path, s.serveMediaHandler(tvpayload, media)) configures the file path and what handler to call when an incoming request is received

  2. s.serveMediaHandler -> serveContent -> serveContentCustomType -> ServeTranscodedStream

  3. Start the actual server here _ = s.http.Serve(ln)

@alexballas alexballas added the enhancement New feature or request label Jul 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants