{"id":260,"date":"2014-10-11T03:11:09","date_gmt":"2014-10-11T03:11:09","guid":{"rendered":"http:\/\/sonny.cslu.ohsu.edu\/~gormanky\/blog\/?p=260"},"modified":"2014-10-11T03:11:09","modified_gmt":"2014-10-11T03:11:09","slug":"unix-av-club","status":"publish","type":"post","link":"https:\/\/www.wellformedness.com\/blog\/unix-av-club\/","title":{"rendered":"UNIX AV club"},"content":{"rendered":"<address>[This post was written as a supplement to\u00a0<a title=\"CS506-CS606: Research Programming @ CSLU\" href=\"http:\/\/cslu.ohsu.edu\/~gormanky\/courses\/CS606-RP\/\">CS506\/606:\u00a0Research Programming<\/a>\u00a0at OHSU.]<\/address>\n<p><a title=\"SoX\" href=\"http:\/\/sox.sourceforge.net\">SoX<\/a> and <a title=\"FFmpeg\" href=\"http:\/\/ffmpeg.org\">FFmpeg<\/a>\u00a0are fast, powerful command-line tools for manipulating audio and video data, respectively. In this short tutorial, I&#8217;ll show how to use these tools for two very common tasks: 1) resampling and 2) (de)multiplexing. Both tools are available from your favorite package manager\u00a0\u00a0(like <a title=\"Homebrew\" href=\"http:\/\/brew.sh\">Homebrew<\/a>\u00a0or\u00a0<code>apt-get<\/code>).<\/p>\n<p><strong>SoX and friends<\/strong><\/p>\n<p>SoX is a suite of programs for manipulating audio files. Commands are of the form:<\/p>\n<pre><code>sox [flag ...] infile1 [...] outfile [effect effect-options] ...<\/code><\/pre>\n<p>That is, the command <code>sox<\/code>, zero or more global flags, one or more input files, one output file, and then a list of &#8220;effects&#8221; to apply. Unlike most UNIX command-line programs, though, SoX actually cares about file extensions. If the input file is <code>in.wav<\/code> it better be a WAV file; if the output file is <code>out.flac<\/code> it will be encoded in the FLAC (&#8220;free lossless audio codec&#8221;) format.<\/p>\n<p>The simplest invocation of <code>sox<\/code> converts audio files to new formats. For instance, the following would use audio.wav to create a new FLAC file audio.flac with the same same bit depth and sample rate.<\/p>\n<pre><code>sox audio.wav audio.flac<\/code><\/pre>\n<p>Concatenating audio files is only slightly more complicated. The following would concatenate <code>01_Intro.wav<\/code> and <code>02_Untitled.wav<\/code> together into a new file <code>concatenated.wav<\/code>.<\/p>\n<pre><code>sox 01_Intro.wav 02_Untitled.wav concatenated.wav<code><\/code><\/code><\/pre>\n<h2>Resampling with SoX<\/h2>\n<p>But SoX\u00a0really shines for resampling audio. For this, use the rate effect. The following would downsample the CD-quality (44.1 kHz) audio in <code>CD.wav<\/code> to the standard sample rate used on telephones (8 kHz) and store the result in <code>telephone.wav<\/code><\/p>\n<pre><code>sox CD.wav telephone.wav rate 8k<\/code><\/pre>\n<p>There are two additional effects you may want to invoke when resampling. First, you may want to &#8220;dither&#8221; the audio. As <code>man sox<\/code> explains:<\/p>\n<blockquote><p>Dithering\u00a0is a technique\u00a0used\u00a0to maximize the dynamic range of audio stored at a particular bit-depth. Any distortion introduced by quantization is decorrelated by adding a\u00a0small\u00a0amount\u00a0of white noise to the signal.\u00a0In most cases, SoX can determine\u00a0whether\u00a0the selected\u00a0processing\u00a0requires dither and will add it during output formatting if appropriate.<\/p><\/blockquote>\n<p>The following would resample to the telephone rate with dithering (if necessary).<\/p>\n<pre><code>sox CD.wav telephone.wav rate 8k dither -s<\/code><\/pre>\n<p>Finally,\u00a0when resampling audio, you may want to invoke the gain effect to avoid clipping. This can be done using the <code>-G<\/code>\u00a0(&#8220;Gain&#8221;) global option.<\/p>\n<pre><code>sox -G CD.wav telephone.wav rate 8k dither -s<\/code><\/pre>\n<h2>(De)multiplexing with SoX<\/h2>\n<p>The SoX <code>remix<\/code> effect is useful for manipulating multichannel audio. The following would remix a multi-channel audio file <code>stereo.wav<\/code> down to mono.<\/p>\n<pre><code>sox stereo.wav mono.wav remix -<\/code><\/pre>\n<p>We also can split a stereo file into two mono files.<\/p>\n<pre><code>sox stereo.wav left.wav remix 1\nsox stereo.wav right.wav remix 2<\/code><\/pre>\n<p>Finally, we can merge two mono files together to create one stereo file using the <code>-M<\/code>\u00a0(&#8220;merge&#8221;) global option; this file should be identical to <code>stereo.wav<\/code>.<\/p>\n<pre><code>sox -M left.wav right.wav stereo2.wav<\/code><\/pre>\n<h2>Other SoX goodies<\/h2>\n<p>There are three other useful utilities in SoX: <code>soxi<\/code> prints information extracted from audio file headers, <code>play<\/code> uses the SoX libraries to play audio files, and <code>rec<\/code> records new audio files using a microphone.<\/p>\n<h1>FFmpeg<\/h1>\n<p>The FFmpeg suite\u00a0is to video files what SoX is to audio. Commands are of the form:<\/p>\n<pre><code>ffmpeg [flag ...] [-i infile1 ...] [-effect ...] [outfile]<\/code><\/pre>\n<p>The <code>-acodec<\/code> and <code>-vcodec<\/code> effects can be used to\u00a0extract the audio and video streams from a video file, respectively; this process sometimes known as\u00a0<em>demuxing\u00a0<\/em>(short for &#8220;de-multiplexing&#8221;).<\/p>\n<pre><code>ffmpeg -i both.mp4 -acodec copy -vn audio.ac3\n...\nffmpeg -i both.mp4 -vcodec copy -an video.h264\n...\n<\/code><\/pre>\n<p>We can also\u00a0<i>mux<\/i>\u00a0(&#8220;multiplex&#8221;) them back together.<\/p>\n<pre><code>ffmpeg -i video.h264 -i audio.ac3 -vcodec copy -acodec copy both2.mp4<\/code><\/pre>\n<p>Hopefully that&#8217;ll get you started. Both programs have excellent manual pages; read them!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[This post was written as a supplement to\u00a0CS506\/606:\u00a0Research Programming\u00a0at OHSU.] SoX and FFmpeg\u00a0are fast, powerful command-line tools for manipulating audio and video data, respectively. In this short tutorial, I&#8217;ll show how to use these tools for two very common tasks: 1) resampling and 2) (de)multiplexing. Both tools are available from your favorite package manager\u00a0\u00a0(like Homebrew\u00a0or\u00a0apt-get). &hellip; <a href=\"https:\/\/www.wellformedness.com\/blog\/unix-av-club\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;UNIX AV club&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-260","post","type-post","status-publish","format-standard","hentry","category-dev","category-language"],"_links":{"self":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts\/260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/comments?post=260"}],"version-history":[{"count":0,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts\/260\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/media?parent=260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/categories?post=260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/tags?post=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}