<h1 id="ffmpeg-series-part-2-joining-and-splitting-files">FFMPEG Series
Part 2: Joining and Splitting files</h1>
<h2 id="joining-files">Joining Files</h2>
<p>Link: <a
href="https://trac.ffmpeg.org/wiki/Concatenate">https://trac.ffmpeg.org/wiki/Concatenate</a></p>
<h3 id="for-lossless-files-of-the-same-codecs">For lossless files of the
same codecs</h3>
<pre><code>ffmpeg -i &quot;concat:input1.ts|input2.ts|input3.ts&quot; -c copy output.ts</code></pre>
<h3 id="for-mp4-files-using-intermediate-files">For mp4 files, using
intermediate files</h3>
<pre><code>ffmpeg -i input1.mp4 -c copy intermediate1.ts
ffmpeg -i input2.mp4 -c copy intermediate2.ts
ffmpeg -i &quot;concat:intermediate1.ts|intermediate2.ts&quot; -c copy output.ts</code></pre>
<p>Brief explanation of the options used in the command:</p>
<ul>
<li><code>-i</code>: input</li>
<li><code>-c copy</code>: use codec same codecs as input</li>
</ul>
<h2 id="splitting-files">Splitting Files</h2>
<h3 id="splitting-by-duration">Splitting by Duration:</h3>
<pre><code>ffmpeg -i input.mp4 -t 00:05:00 output_part1.mp4
ffmpeg -i input.mp4 -t 00:05:00 -ss 00:05:00 output_part2.mp4</code></pre>
<p>Brief explanation of the options used in the command:</p>
<ul>
<li><code>-i</code>: Input file</li>
<li><code>-t</code>: Duration of the output file</li>
<li><code>-ss</code>: Start position of the output file (optional)</li>
</ul>
<h3 id="splitting-by-chapters-or-markers">Splitting by Chapters or
Markers:</h3>
<p>Use additional scripting to get out chapter marker times, then use
above</p>
<p>C. Splitting by Size:</p>
<pre><code>ffmpeg -i input.mp4 -map 0 -c copy -segment_size 50M -segment_times 1 output_%03d.mp4</code></pre>
<p>Brief explanation of the options used in the command:</p>
<ul>
<li><code>-map 0</code>: Select the first stream (video and audio)</li>
<li><code>-c copy</code>: Copy the stream without re-encoding
(faster)</li>
<li><code>-segment_size</code>: Size of each output file segment</li>
<li><code>-segment_times</code>: Number of segments to create</li>
<li><code>output_%03d.mp4</code>: printf-style pattern for the output
files</li>
</ul>
