Working with Fish Shell, ffmpeg, MP4Box and sox to generate audio files

More adventures with Fish shell. I have scripted the generation of a bunch of test asset audio files in mp4 format suitable for dash streaming. I have used the audio file commandline tools ffmepg MP4Box and sox in this script, they are pretty powerful and worth installing via homebrew on OSX.

Thoughts: I think that instead of quoting you can just output $variable, which is probably better coding style. I’m still working out good way to return variables from functions, it seems that you need to echo out the output and that gets picked up by the calling function, which is a bit messy.

#!/usr/bin/env fish
# move to the asset output directory..
cd ..

# Generate .m4a file for file, and all the associated .mp4 dash assets
function generateDash
  set bitrate $argv[1]
  set input_filename $argv[2]
  set output_filename "$input_filename"-"$bitrate".m4a
  ffmpeg -i "$input_filename" -ab "$bitrate"k "$output_filename"
  MP4Box -dash 10000 "$output_filename"
end

# Generate 3 second sine wave in a specified bitrate at a specified frequency
function generateWav
  set bitrate $argv[1]
  set filename $argv[2]
  set frequency $argv[3]
  set wavfilename "$filename"-"$bitrate".wav
  sox -n --norm=-3 -b "$bitrate" "$wavfilename" synth 3 sine "$frequency"
  echo $wavfilename
end

# Main loop, generate 128,192 and 320 bitrate dashed .mp4s for 16bit and 32bit 3 second sine waves
for i in (seq 24)
  set freq (math "$i * 64.5")
  set wavfilename (generateWav 16 "output$i" "$freq")
  generateDash 128 "$wavfilename"
  generateDash 192 "$wavfilename"
  generateDash 320 "$wavfilename"
  set wavfilename (generateWav 24 "output$i" "$freq")
  generateDash 128 "$wavfilename"
  generateDash 192 "$wavfilename"
  generateDash 320 "$wavfilename"
end

Converting a single M2V frame into JPEG under OSX

I needed to view a single frame of a m2v file that had been encoded by our designers for playing out on TV. The file name was .mpg but in actuality it was a single .m2v frame renamed to be a .mpg. Windows Media Player classic used to display the frame fine when I opened the file normally, under Windows XP. However now I have switched to a Mac, I have found that Quicktime and VLC refused to display the single frame. I couldn’t find a video player that would open the single frame. So I resorted to the command line version of ffmpeg, which I installed via macports, to convert this single frame to a jpg file to view as normal. This line worked a treat:

ffmpeg -i north.mpg -ss 00:00:00 -t 00:00:1 -s 1024x768 -r 1 -f mjpeg north.jpg

Where ‘north.mpg’ was the m2v file, and ‘north.jpg’ was the output jpeg.

And this:

find -name *.mpg -exec ffmpeg -i {} -ss 00:00:00 -t 00:00:1 -s 1024x768 -r 1 -f mjpeg {}.jpg ;

Will go through all the mpg files in the current directory and below, and create their jpeg single frame equivalents, ie: for north.mpg it will create north.mpg.jpg.