<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/rss-style.xsl" type="text/xsl"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	     xmlns:dc="http://purl.org/dc/elements/1.1/"
	   xmlns:atom="http://www.w3.org/2005/Atom"
	     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>mkvmerge &#8211; Terence Eden’s Blog</title>
	<atom:link href="https://shkspr.mobi/blog/tag/mkvmerge/feed/" rel="self" type="application/rss+xml" />
	<link>https://shkspr.mobi/blog</link>
	<description>Regular nonsense about tech and its effects 🙃</description>
	<lastBuildDate>Tue, 04 Feb 2025 07:44:26 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://shkspr.mobi/blog/wp-content/uploads/2023/07/cropped-avatar-32x32.jpeg</url>
	<title>mkvmerge &#8211; Terence Eden’s Blog</title>
	<link>https://shkspr.mobi/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title><![CDATA[HOWTO: Split WAV and CUE files on Linux]]></title>
		<link>https://shkspr.mobi/blog/2019/02/howto-split-wav-and-cue-files-on-linux/</link>
					<comments>https://shkspr.mobi/blog/2019/02/howto-split-wav-and-cue-files-on-linux/#comments</comments>
				<dc:creator><![CDATA[@edent]]></dc:creator>
		<pubDate>Mon, 04 Feb 2019 20:48:18 +0000</pubDate>
				<category><![CDATA[/etc/]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[ffmpeg]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mkv]]></category>
		<category><![CDATA[mkvmerge]]></category>
		<category><![CDATA[surround sound]]></category>
		<guid isPermaLink="false">https://shkspr.mobi/blog/?p=31357</guid>

					<description><![CDATA[Mostly notes to myself, as a follow-up to this older post.  This is a 3-step process.  Add the file to an MKV  Use MKVmerge:  mkvmerge &#34;audio.wav&#34; --chapters &#34;audio.cue&#34; -o &#34;audio.mkv&#34;   You can see that chapter names have been added to the .mkv if you run ffmpeg -i or mkvinfo.  Split the MKV by chapter  This generates one file per chapter:  mkvmerge -D -S &#34;audio.mkv&#34; --split chapters:all -o…]]></description>
										<content:encoded><![CDATA[<p>Mostly notes to myself, as a follow-up to <a href="https://shkspr.mobi/blog/2015/07/splitting-a-surround-sound-audio-file-in-ubuntu/">this older post</a>.  This is a 3-step process.</p>

<h2 id="add-the-file-to-an-mkv"><a href="https://shkspr.mobi/blog/2019/02/howto-split-wav-and-cue-files-on-linux/#add-the-file-to-an-mkv">Add the file to an MKV</a></h2>

<p>Use <a href="https://mkvtoolnix.download/">MKVmerge</a>:</p>

<pre><code class="language-_">mkvmerge "audio.wav" --chapters "audio.cue" -o "audio.mkv"
</code></pre>

<p>You can see that chapter names have been added to the .mkv if you run <code>ffmpeg -i</code> or <code>mkvinfo</code>.</p>

<h2 id="split-the-mkv-by-chapter"><a href="https://shkspr.mobi/blog/2019/02/howto-split-wav-and-cue-files-on-linux/#split-the-mkv-by-chapter">Split the MKV by chapter</a></h2>

<p>This generates one file per chapter:</p>

<pre><code class="language-_">mkvmerge -D -S "audio.mkv" --split chapters:all -o "split-%02d.mkv"
</code></pre>

<p>The <code>-D</code> switch means no video will be copied. <code>-S</code> means no subtitles.</p>

<h2 id="extract-the-audio"><a href="https://shkspr.mobi/blog/2019/02/howto-split-wav-and-cue-files-on-linux/#extract-the-audio">Extract the audio</a></h2>

<p>This quickly pulls out the audio, raw, without changing anything.</p>

<pre><code class="language-_">mkvextract split-01.mkv tracks "0:output"
</code></pre>

<p>The raw audio is placed in a file called <code>output</code></p>

<p>You can also do this with:</p>

<pre><code class="language-_">ffmpeg -i "split-01.mkv" -acodec copy "whatever.wav"
</code></pre>

<p>Replace <code>.wav</code> with <code>.ac3</code> or <code>.flac</code> or whatever the audio format is.
You can transcode the audio to something else. I recommend <a href="https://opus-codec.org/">Opus</a></p>

<pre><code class="language-_">ffmpeg -i "split-01.mkv" -af aformat=channel_layouts="7.1|5.1|stereo" -b:a 1536k "whatever.opus"
</code></pre>

<p>There is a <a href="https://trac.ffmpeg.org/ticket/5718">bug with the surround sound mapping in ffmpeg</a>.</p>

<h2 id="rename-it"><a href="https://shkspr.mobi/blog/2019/02/howto-split-wav-and-cue-files-on-linux/#rename-it">Rename it</a></h2>

<p>This is bit convoluted. <code>ffprobe</code> can get the chapter name:</p>

<pre><code class="language-_">ffprobe -i "split-01.mkv" -show_chapters -loglevel error
</code></pre>

<p>The use of <code>-loglevel error</code> means you only get the data, rather than the debug cruft.</p>

<p>In order to get the title, we get <code>ffprobe</code> to spit out JSON and then get <code>jq</code> to interpret it:</p>

<pre><code class="language-_">ffprobe -i "split-01.mkv" -print_format json -show_chapters -loglevel error | jq ".chapters[0].tags.title" -r
</code></pre>

<h2 id="all-in-one-script-to-extract-the-audio-and-rename-it"><a href="https://shkspr.mobi/blog/2019/02/howto-split-wav-and-cue-files-on-linux/#all-in-one-script-to-extract-the-audio-and-rename-it">All in one script to extract the audio <em>and</em> rename it</a></h2>

<pre><code class="language-_">#!/bin/bash
AUDIO="audio.wav"
CUE="audio.cue"
MKV="audio.mkv"

# What's the audio format?
FORMAT=$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$AUDIO")

# Create the MKV
mkvmerge "$AUDIO" --chapters "$CUE" -o "$MKV"

# How many chapters are there?
JSON=$(ffprobe -i "$MKV" -print_format json -show_chapters -loglevel error)
COUNT=$(echo $JSON | jq ".chapters | length" )

# Split the MKV into chapters
mkvmerge -D -S "$MKV" --split chapters:all -o "split-%02d.mkv"

# Loop through all the created .mkv files
COUNTER=1
while [ $COUNTER -le $COUNT ]; do

  # Zero pad the counter
  printf -v ZEROTRACK "%02d" $COUNTER

  # Get the chapter name
  JSON=$(ffprobe -i "split-$ZEROTRACK.mkv" -print_format json -show_chapters -loglevel error)
  TITLE=$(echo $JSON | jq ".chapters[0].tags.title" -r)
  FILENAME="[$ZEROTRACK] $TITLE.$FORMAT"

  # Raw Audio
  mkvextract "split-$ZEROTRACK.mkv" tracks "0:$FILENAME"
  # Transcode. Optional
  #ffmpeg -i "split-$ZEROTRACK.mkv" "$FILENAME.opus"

  let COUNTER=COUNTER+1 
done

# Delete the generated MKVs. Optional
#rm *.mkv
</code></pre>

<p>Note, there is <a href="https://gitlab.com/mbunkus/mkvtoolnix/issues/2498">no single command in MKVtools to do this</a></p>
<img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=31357&HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager">]]></content:encoded>
					
					<wfw:commentRss>https://shkspr.mobi/blog/2019/02/howto-split-wav-and-cue-files-on-linux/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
