Answers.Body: <p>I have found a solution:</p>
  <pre class="lang-bash prettyprint-override"><code>ffmpeg -i input0.wav -i input1.wav \
    -filter_complex \
    &quot;[1]    aeval  = -val(0) | -val(1)                     [a]; \
     [0][a] amerge = inputs=2,pan=stereo|c0&lt;c0+c2|c1&lt;c1+c3 [b]&quot; \
    -map &quot;[b]&quot; -c:a flac output.flac
  </code></pre>
  <p>This command subtracts <code>input1.wav</code> from <code>input0.wav</code> and assumes that both inputs are stereo.</p>
  <p>See <a href="https://trac.ffmpeg.org/wiki/AudioChannelManipulation" rel="nofollow noreferrer">https://trac.ffmpeg.org/wiki/AudioChannelManipulation</a> for manipulating audio channels.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-23T05:08:22.687Z
Answers.LastActivityDate: 2023-04-23T05:08:22.687Z
Answers.UserDisplayName: Calarpo
Answers.Body: <p>Note: the examples assume the files are the same length and that various defualt options are desirable.</p>
  <h2>Invert the phase with <code>volume</code> filter</h2>
  <pre><code>FFmpeg ^
  -i audioReference.wav ^
  -i audioTarget.wav ^
  -filter_complex [1]volume=-1[streamInverted];[0][streamInverted]amix ^
  audioDelta.wav ^
  -hide_banner -loglevel level+info
  </code></pre>
  <h2>Subtract the mask with <code>amix</code> filter</h2>
  <pre><code>FFmpeg ^
  -i audioReference.wav ^
  -i audioTarget.wav ^
  -filter_complex &quot;[0][1]amix=weights=1 -1&quot; ^
  audioDelta.wav ^
  -hide_banner -loglevel level+info ^
  </code></pre>
  <p>Note well: the space-delimited values in the <code>weights</code> parameter of the <code>amix</code> filter. Pay attention to <a href="https://www.ffmpeg.org/ffmpeg-utils.html#quoting_005fand_005fescaping" rel="nofollow noreferrer">quoting and escaping</a>.</p>
  <h2>Invert the phase with <code>aeval</code> filter</h2>
  <pre><code>FFmpeg ^
  -i audioReference.wav ^
  -i audioTarget.wav ^
  -filter_complex [1]aeval=-val(ch):c=same[streamInverted];[0][streamInverted]amix ^
  audioDelta.wav ^
  -hide_banner -loglevel level+info
  </code></pre>
  <h2>Test by subtracting the signal from itself</h2>
  <pre><code>FFmpeg ^
  -i audioReference.wav ^
  -filter_complex [0]asplit[original][clone];[clone]volume=-1[test];[original][test]amix ^
  audioInterfered.wav ^
  -hide_banner -loglevel level+info
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-20T20:50:48.210Z
Answers.LastActivityDate: 2024-06-20T20:50:48.210Z
Answers.UserDisplayName: hunterhogan
Body: <p>I can do audio phase subtraction in Adobe Audition like so: <div class="youtube-embed"><div>
                  <iframe width="640px" height="395px" src="https://www.youtube.com/embed/vNvJKBg3yds?start=0"></iframe>
              </div></div></p>
  <p>How can I do this with FFmpeg? Is it possible?</p>
  <p>I've googled for audio phase subtraction, and looked at <a href="https://ffmpeg.org/ffmpeg-filters.html" rel="nofollow noreferrer">https://ffmpeg.org/ffmpeg-filters.html</a>, nothing seems to work for me.</p>
  <p>I want to do phase subtraction in batches and support for stereo audio.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-04-22T17:26:23.827Z
Comments.Text: You can do this in something like Audacity, but why the need for batch processing? It's the kind of thing that only works if you have absolute phase-alignment to start with*. I struggle to see a use case you would have multiple examples of this type of overlay with identical start-points/phase alignment. It makes the whole question feel like a bit of an [XY Problem](https://en.wikipedia.org/wiki/XY_problem) *[I didn't watch the whole thing, but did the guy tell you it took him 20 minutes to phase-align his example before he started the video?]
Comments.UserDisplayName: Tetsujin
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-04-22T19:01:03.497Z
Comments.Text: See the [aeval](http://www.ffmpeg.org/ffmpeg-filters.html#aeval) filter.
Comments.UserDisplayName: Gyan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-04-23T04:08:33.490Z
Comments.Text: @tetsujin I want to compare audio compression using different codecs, with perfectly aligned audio files. Additionally, I want to compare audio encoding across various scenarios, which is why I need to perform this task in batches.
Comments.UserDisplayName: Calarpo
Tags: <ffmpeg><audio><signal-processing>
Title: Can FFmpeg perform audio phase subtraction?
UserDisplayName: Calarpo
ThreadText: # Can FFmpeg perform audio phase subtraction?
  
  (Asked by: username_0 on 2023-04-22)
  
  I can do audio phase subtraction in Adobe Audition like so:
  
  How can I do this with FFmpeg? Is it possible?
  
  I've googled for audio phase subtraction, and looked at <https://ffmpeg.org/ffmpeg-filters.html>, nothing seems to work for me.
  
  I want to do phase subtraction in batches and support for stereo audio.
  
  **Tags:** ffmpeg, audio, signal-processing
  
  > **Comments:**
  > **username_1**: You can do this in something like Audacity, but why the need for batch processing? It's the kind of thing that only works if you have absolute phase-alignment to start with*. I struggle to see a use case you would have multiple examples of this type of overlay with identical start-points/phase alignment. It makes the whole question feel like a bit of an [XY Problem](https://en.wikipedia.org/wiki/XY_problem) *[I didn't watch the whole thing, but did the guy tell you it took him 20 minutes to phase-align his example before he started the video?]  
  > **username_2**: See the [aeval](http://www.ffmpeg.org/ffmpeg-filters.html#aeval) filter.  
  > **username_0**: @username_1 I want to compare audio compression using different codecs, with perfectly aligned audio files. Additionally, I want to compare audio encoding across various scenarios, which is why I need to perform this task in batches.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2023-04-23. Score: 2
  
  I have found a solution:
  
  ```bash
  ffmpeg -i input0.wav -i input1.wav \
    -filter_complex \
    "[1]    aeval  = -val(0) | -val(1)                     [a]; \
     [0][a] amerge = inputs=2,pan=stereo|c0<c0+c2|c1<c1+c3 [b]" \
    -map "[b]" -c:a flac output.flac
  
  ```
  
  This command subtracts `input1.wav` from `input0.wav` and assumes that both inputs are stereo.
  
  See <https://trac.ffmpeg.org/wiki/AudioChannelManipulation> for manipulating audio channels.
  
  
  ---
  
  ### Answer by username_3 on 2024-06-20. Score: 0
  
  Note: the examples assume the files are the same length and that various defualt options are desirable.
  
  ## Invert the phase with `volume` filter
  
  ```
  FFmpeg ^
  -i audioReference.wav ^
  -i audioTarget.wav ^
  -filter_complex [1]volume=-1[streamInverted];[0][streamInverted]amix ^
  audioDelta.wav ^
  -hide_banner -loglevel level+info
  
  ```
  
  ## Subtract the mask with `amix` filter
  
  ```
  FFmpeg ^
  -i audioReference.wav ^
  -i audioTarget.wav ^
  -filter_complex "[0][1]amix=weights=1 -1" ^
  audioDelta.wav ^
  -hide_banner -loglevel level+info ^
  
  ```
  
  Note well: the space-delimited values in the `weights` parameter of the `amix` filter. Pay attention to [quoting and escaping](https://www.ffmpeg.org/ffmpeg-utils.html#quoting_005fand_005fescaping).
  
  ## Invert the phase with `aeval` filter
  
  ```
  FFmpeg ^
  -i audioReference.wav ^
  -i audioTarget.wav ^
  -filter_complex [1]aeval=-val(ch):c=same[streamInverted];[0][streamInverted]amix ^
  audioDelta.wav ^
  -hide_banner -loglevel level+info
  
  ```
  
  ## Test by subtracting the signal from itself
  
  ```
  FFmpeg ^
  -i audioReference.wav ^
  -filter_complex [0]asplit[original][clone];[clone]volume=-1[test];[original][test]amix ^
  audioInterfered.wav ^
  -hide_banner -loglevel level+info
  
  ```
  
  
  ---
  
  