Showing posts with label ffmpeg. Show all posts
Showing posts with label ffmpeg. Show all posts

2013-12-01

Reversing an audio file on the command line

While reading about credit card readers, I stumbled upon the concept of backmasking.  Thus, the question naturally arose: how would one play back an audio file in reverse on a Mac?  This is the best one-liner I could come up with:
input=~/Downloads/audio.ogg; channels=2; rate=44100; tempfile=test.wav
ffmpeg -i $input -f s16le - | xxd -p -c$((channels * 2)) | tail -r | xxd -r -p | ffmpeg  -f s16le -ar $rate -ac $channels -i - -y $tempfile && afplay $tempfile
Unfortunately, this requires creating a temporary output file, and knowing the rate and number of channels of the audio file.  To break down the operations:

  1. the first ffmpeg command converts the file into raw audio data (signed shorts)
  2. the first xxd converts the audio data for a sample from each channel into an ascii hexadecimal line
  3. tail -r is the reversion command
  4. the second xxd reverses the ascii line into raw data
  5. the second ffmpeg creates a temporary audio file from the raw data, given the rate and channel count
  6. afplay plays the file out