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

Move AV's add_destination after convert_options to permit second input (e.g. watermark). #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

joshuapinter
Copy link

When you call @cli.add_destination(dst.path), AV will add various output formats automatically, based on the destination filename. You can see that here:

https://github.com/ruby-av/av/blob/f00e6295e8f175262c99f2a5c3f1d4ed75c2c436/lib/av/commands/base.rb#L134

def output_format format
  @output_format = format
  case format.to_s
  when /jpg$/, /jpeg$/, /png$/, /gif$/ # Images
    add_output_param 'f', 'image2'
    add_output_param 'vframes', '1'
  when /webm$/ # WebM
    add_output_param 'f', 'webm'
    add_output_param 'acodec', 'libvorbis'
    add_output_param 'vcodec', 'libvpx'
  when /ogv$/ # Ogg Theora
    add_output_param 'f', 'ogg'
    add_output_param 'acodec', 'libvorbis'
    add_output_param 'vcodec', 'libtheora'
  when /mp4$/
    add_output_param 'acodec', 'aac'
    add_output_param 'strict', 'experimental'
  end
end

These output params will naturally come before the output_options specified in this gem, which is probably fine most of the time but it prevents some advanced but common uses, like adding a watermark overlay to your videos.

For example, creating a watermark overlay on a video would look like this:

mp4: { 
  format: 'mp4', 
  convert_options: { 
    output: { 
      'i': 'my-watermark.png',
      'filter_complex': 'overlay=10:10',
    } 
  } 
}

However, this generates an FFMPEG command that looks like this:

ffmpeg -i my-input-video.mkv -acodec aac -strict experimental -i my-watermark.png -filter_complex overlay=10:10 -y my-output-video.mp4

That causes a problem because it tries to apply the -acodec aac to the -i my-watermark.png, which is an image and you get an error message like this:

decoding for stream 0 failed
Could not find codec parameters for stream 0 (Audio: aac, 0 channels, fltp): unspecified sample rate
Consider increasing the value for the 'analyzeduration' and 'probesize' options

By moving the add_destination after we add our convert_options to the @cli, it generates a FFMPEG command like this:

ffmpeg -i my-input-video.mkv -i my-watermark.png -filter_complex overlay=10:10 -acodec aac -strict experimental -y my-output-video.mp4

This properly applies the "default" output options from the AV gem to the output file, and everybody is happy.

When you call `@cli.add_destination(dst.path)`, AV will add various output formats automatically, based on the destination filename. You can see that here: 

https://github.com/ruby-av/av/blob/f00e6295e8f175262c99f2a5c3f1d4ed75c2c436/lib/av/commands/base.rb#L134
```ruby
def output_format format
  @output_format = format
  case format.to_s
  when /jpg$/, /jpeg$/, /png$/, /gif$/ # Images
    add_output_param 'f', 'image2'
    add_output_param 'vframes', '1'
  when /webm$/ # WebM
    add_output_param 'f', 'webm'
    add_output_param 'acodec', 'libvorbis'
    add_output_param 'vcodec', 'libvpx'
  when /ogv$/ # Ogg Theora
    add_output_param 'f', 'ogg'
    add_output_param 'acodec', 'libvorbis'
    add_output_param 'vcodec', 'libtheora'
  when /mp4$/
    add_output_param 'acodec', 'aac'
    add_output_param 'strict', 'experimental'
  end
end
```

These output params will naturally come before the `output_options` specified in this gem, which is probably fine most of the time but it prevents some advanced but common uses, like adding a watermark overlay to your videos. 

For example, creating a watermark overlay on a video would look like this: 

```ruby
mp4: { 
  format: 'mp4', 
  convert_options: { 
    output: { 
      'i': 'my-watermark.png',
      'filter_complex': 'overlay=10:10',
    } 
  } 
}
```

However, this generates an FFMPEG command that looks like this: 

```bash
ffmpeg -i my-input-video.mkv -acodec aac -strict experimental -i my-watermark.png -filter_complex overlay=10:10 -y my-output-video.mp4
```

That causes a problem because it tries to apply the `-acodec aac` to the `-i my-watermark.png`, which is an image and you get an error message like this: 

```bash
decoding for stream 0 failed
Could not find codec parameters for stream 0 (Audio: aac, 0 channels, fltp): unspecified sample rate
Consider increasing the value for the 'analyzeduration' and 'probesize' options
```

By moving the `add_destination` after we add our `convert_options` to the `@cli`, it generates a FFMPEG command like this: 

```bash
ffmpeg -i my-input-video.mkv -i my-watermark.png -filter_complex overlay=10:10 -acodec aac -strict experimental -y my-output-video.mp4
```

This properly applies the "default" output options from the AV gem to the output file, and everybody is happy.
@joshuapinter joshuapinter changed the title Move AV's add_destination after convert_options. Move AV's add_destination after convert_options to permit second input (e.g. watermark). Nov 15, 2017
slaur added a commit to slaur/paperclip-av-transcoder that referenced this pull request Oct 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant