Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Multiple options and outputs #11

Merged
merged 4 commits into from
Nov 3, 2020
Merged

Multiple options and outputs #11

merged 4 commits into from
Nov 3, 2020

Conversation

Borloxos
Copy link

For a project I'm working on, I need to be able to transcode to multiple output files, with different options, in one transcoding process.

Here is an example, of how I would expect a ffmpeg call in this context to look
ffmpeg -i 149336093 -f webm -vf scale=-2:90 -y "./output/90px.webm" -f webm -vf scale=-2:240 -y "./output/240px.webm"

As this is currently not possible with this library, I tried to implement the changes necessary for that.
The changes include

  • Changing the output attribute of Transcoder to a slice of string
  • Changing the options attribute of Transcoder to a slice of slices of string
  • Adding a new function, withAdditionalOptions, that appends a new Options object to the options attribute, instead of overwriting it
  • Changing the logic of how the ffmpeg call is built
    • The input file and the options given to Start(opts) are appended first
    • After that, if only 1 output file and no additional options were defined, just the output file is appended
    • If there are multiple output files, they are iterated over and appended, along with the options defined at the same index in t.options (Which were not being used in the code until now, as I noticed)
    • If there are more elements in the t.options slice, than there are output files in t.output, the remaining options get squashed together before the last output file
      • This behavior could also be changed to squash surplus options at the beginning of the command, I'm not sure which is better
    • This means, that the assignment of options to output files is dependent on the order, in which you call the methods on the transcoder object
    • Updating the validate function to reflect these changes
      • In particular, validate fails, if you passed more output files than options to the transcoder, as that would not make any sense
      • If you only passed 1 output file with no options it doesn't fail though, as that may be a valid ffmpeg call

These changes proved to run just fine for me in my tests. Here is the piece of code, that I tested them with in my project, it all worked out as expected:

format90 := "mp4"
overwrite90 := true
vf90 := "scale=-2:90"

options90 := &ffmpeg.Options{
	OutputFormat: &format90,
	Overwrite:    &overwrite90,
	VideoFilter:  &vf90,
}

format240 := "webm"
overwrite240 := true
vf240 := "scale=-2:240"

options240 := &ffmpeg.Options{
	OutputFormat: &format240,
	Overwrite:   &overwrite240,
	VideoFilter: &vf240,
}

overwrite := true

startOptions := &ffmpeg.Options{
	Overwrite: &overwrite,
}

ffmpegConfig := &ffmpeg.Config{
	FfmpegBinPath:   "/usr/bin/ffmpeg",
	FfprobeBinPath:  "/usr/bin/ffprobe",
	ProgressEnabled: true,
	//Verbose:         true,
}

progress, err := ffmpeg.
	New(ffmpegConfig).
	Input("/transcoder/test").
	WithAdditionalOptions(options90).
	Output("/transcoder/output/test.90.mp4").
	WithAdditionalOptions(options240).
	Output("/transcoder/output/test.240.webm").
	Start(startOptions)

if err != nil {
	log.Fatal(err)
}

for msg := range progress {
	fmt.Printf("%+v", msg)
}

If there are any problems with my PR I'll be happy to fix them. Thank you very much for your work on this library!

@xfrr
Copy link
Collaborator

xfrr commented Nov 3, 2020

Thanks to you for your contribution!

@xfrr xfrr merged commit 5980774 into floostack:master Nov 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants