ffmpeg-go icon indicating copy to clipboard operation
ffmpeg-go copied to clipboard

take audio from mp4 video using ffmpeg

Open nakem1 opened this issue 2 years ago • 1 comments

I'm trying to take audio from an mp4 video. I don't want to create extra local files, but I can't pass the input otherwise.

I transfer the created file and return the audio to io.Reader(audio var):

telegramFile, err := r.bot.GetFile(&msg.VideoNote.File)
if err != nil {
    // handling error
}
fileBytes, err := io.ReadAll(telegramFile)
if err != nil {
    // handling error
}

inputFile := fmt.Sprintf("%s.mp4", msg.VideoNote.File.FileID)
err = ioutil.WriteFile(inputFile, fileBytes, 0o600)
if err != nil {
    // handling error
}

audio := bytes.NewBuffer(nil)
err = ffmpeg.Input(
    inputFile,
).
    Output("pipe:1", ffmpeg.KwArgs{"format": "wav"}).WithOutput(audio).Run()
if err != nil {
    // handling error
}

I am sending io.Reader(telegramFile var) and get io.Reader(audio var):

telegramFile, err := r.bot.GetFile(&msg.VideoNote.File)
if err != nil {
    // handling error
}

audio := bytes.NewBuffer(nil)
err = ffmpeg.Input(
    "pipe:0", ffmpeg.KwArgs{"format": "mp4"},
).
    Output("pipe:1", ffmpeg.KwArgs{"format": "wav"}).WithInput(telegramFile).WithOutput(audio).Run()
if err != nil {
    // handling error
}

the first code works, but I need the second one. The conversion is performed, but the audio is invalid. Too few bytes.

here is an example of using input -> output https://github.com/u2takey/ffmpeg-go/blob/8af5c3849807dcda756406a7b2b3631a5c90d53b/ffmpeg_test.go#L330

nakem1 avatar Apr 11 '22 13:04 nakem1

@nakem1 I've stumbled on the same problem. ffmpeg can't process mp4 files with pipes due to mp4 format structure. Here is a more detailed answer on SO https://stackoverflow.com/questions/62823746/ffmpeg-piping-doesnt-work-with-mp4-files

yoihito avatar Apr 03 '23 20:04 yoihito