<p><strong>Original Question</strong></p>

<p>I want to be able to generate a new (fully valid) MP3 file from an existing MP3 file to be used as a preview -- try-before-you-buy style. The new file should only contain the first <em>n</em> seconds of the track.</p>

<p>Now, I know I could just "chop the stream" at <em>n</em> seconds (calculating from the bitrate and header size) when delivering the file, but this is a bit dirty and a real PITA on a VBR track. I'd like to be able to generate a proper MP3 file.</p>

<p>Anyone any ideas?</p>

<p><strong>Answers</strong></p>

<p>Both <code>mp3split</code> and <code>ffmpeg</code> are both good solutions. I chose ffmpeg as it is commonly installed on linux servers and is also <a href="http://sourceforge.net/project/showfiles.php?group_id=205275&amp;package_id=248632" rel="noreferrer">easily available for windows</a>.  Here's some more good command line parameters for generating previews with ffmpeg</p>

<ul>
<li><strong><code>-t &lt;seconds&gt;</code></strong> chop after specified number of seconds</li>
<li><strong><code>-y</code></strong> force file overwrite</li>
<li><strong><code>-ab &lt;bitrate&gt;</code></strong> set bitrate e.g. <em>-ab 96k</em></li>
<li><strong><code>-ar &lt;rate Hz&gt;</code></strong> set sampling rate e.g. <em>-ar 22050</em> for 22.05kHz</li>
<li><strong><code>-map_meta_data &lt;outfile&gt;:&lt;infile&gt;</code></strong> copy track metadata from infile to outfile</li>
</ul>

<p>instead of setting -ab and -ar, you can copy the original track settings, as Tim Farley suggests, with:</p>

<ul>
<li><strong><code>-acodec copy</code></strong></li>
</ul>

[{'answer_id': 43912, 'author': 'John Boker', 'author_id': 2847, 'author_profile': 'https://Stackoverflow.com/users/2847', 'pm_score': 4, 'selected': False, 'text': '<p>try:</p>\n\n<pre><code>ffmpeg -t 30 -i inputfile.mp3 outputfile.mp3\n</code></pre>\n'}, {'answer_id': 43914, 'author': 'Ryan Duffield', 'author_id': 2696, 'author_profile': 'https://Stackoverflow.com/users/2696', 'pm_score': 3, 'selected': False, 'text': '<p>You might want to try <a href="http://mp3splt.sourceforge.net/mp3splt_page/home.php" rel="noreferrer">Mp3Splt</a>.</p>\n\n<p>I\'ve used it before in a C# service that simply wrapped the mp3splt.exe win32 process.  I assume something similar could be done in your Linux/PHP scenario.</p>\n'}, {'answer_id': 44032, 'author': 'Tim Farley', 'author_id': 4425, 'author_profile': 'https://Stackoverflow.com/users/4425', 'pm_score': 8, 'selected': True, 'text': '<p>I also recommend ffmpeg, but the command line suggested by John Boker has an unintended side effect: it re-encodes the file to the default bitrate (which is 64 kb/s in the version I have here at least). This might give your customers a false impression of the quality of your sound files, and it also takes longer to do.</p>\n\n<p>Here\'s a command line that will slice to 30 seconds without transcoding:</p>\n\n<pre><code>ffmpeg -t 30 -i inputfile.mp3 -acodec copy outputfile.mp3\n</code></pre>\n\n<p>The -acodec switch tells ffmpeg to use the special "copy" codec which does not transcode.  It is lightning fast.</p>\n\n<p>NOTE: the command was updated based on comment from Oben Sonne</p>\n'}, {'answer_id': 2925734, 'author': 'the.jxc', 'author_id': 84949, 'author_profile': 'https://Stackoverflow.com/users/84949', 'pm_score': 6, 'selected': False, 'text': '<p>If you wish to REMOVE the first 30 seconds (and keep the remainder) then use this:</p>\n\n<pre><code>ffmpeg -ss 30 -i inputfile.mp3 -acodec copy outputfile.mp3\n</code></pre>\n'}, {'answer_id': 7465346, 'author': 'Michał Šrajer', 'author_id': 705676, 'author_profile': 'https://Stackoverflow.com/users/705676', 'pm_score': 4, 'selected': False, 'text': "<p>you can use mp3cut:</p>\n\n<pre><code>cutmp3 -i foo.mp3 -O 30s.mp3 -a 0:00.0 -b 0:30.0\n</code></pre>\n\n<p>It's in ubuntu repo, so just: <code>sudo apt-get install cutmp3</code>.</p>\n"}, {'answer_id': 40923857, 'author': 'Mithun Cheriyath', 'author_id': 7239222, 'author_profile': 'https://Stackoverflow.com/users/7239222', 'pm_score': 1, 'selected': False, 'text': '<p>I have got an error while doing the same </p>\n\n<pre><code>Invalid audio stream. Exactly one MP3 audio stream is required.\nCould not write header for output file #0 (incorrect codec parameters     ?): Invalid argumentStream mapping:\n</code></pre>\n\n<p>Fix for me was:</p>\n\n<pre><code>ffmpeg -ss 00:02:43.00 -t 00:00:10 -i input.mp3 -codec:a libmp3lame out.mp3\n</code></pre>\n'}, {'answer_id': 44536650, 'author': 'Rahul Chauhan', 'author_id': 7333306, 'author_profile': 'https://Stackoverflow.com/users/7333306', 'pm_score': 4, 'selected': False, 'text': '<p>This command also works perfectly.\nI cropped my music files from 20 to 40 seconds.</p>\n\n<blockquote>\n  <p>-y : force output file to overwrite.</p>\n</blockquote>\n\n<pre><code>ffmpeg -i test.mp3 -ss 00:00:20 -to 00:00:40 -c copy -y temp.mp3\n</code></pre>\n'}, {'answer_id': 57763520, 'author': 'srbcheema1', 'author_id': 6799074, 'author_profile': 'https://Stackoverflow.com/users/6799074', 'pm_score': 0, 'selected': False, 'text': '<p>My package <strong>medipack</strong> is a very simple command-line app as a wrapper over <code>ffmpeg</code>.</p>\n<p>you can achieve trimming your video using these commands:</p>\n<pre><code>medipack trim input.mp3 -s 00:00 -e 00:30 -o output.mp3\nmedipack trim input.mp3 -s 00:00 -t 00:30 -o output.mp3\n</code></pre>\n<p>you can view options of trim subcommand as:</p>\n<pre><code>srb@srb-pc:$ medipack trim -h\nusage: medipack trim [-h] [-s START] [-e END | -t TIME] [-o OUTPUT] [inp]\n\npositional arguments:\n  inp                   input video file ex: input.mp4\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -s START, --start START\n                        start time for cuting in format hh:mm:ss or mm:ss\n  -e END, --end END     end time for cuting in format hh:mm:ss or mm:ss\n  -t TIME, --time TIME  clip duration in format hh:mm:ss or mm:ss\n  -o OUTPUT, --output OUTPUT\n</code></pre>\n<p>you could also explore other options using <code>medipack -h</code></p>\n<pre><code>srb@srb-pc:$ medipack --help\nusage: medipack.py [-h] [-v] {trim,crop,resize,extract} ...\n\npositional arguments:\n  {trim,crop,resize,extract}\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -v, --version         Display version number\n</code></pre>\n<p>you may visit my repo <a href="https://github.com/srbcheema1/medipack" rel="nofollow noreferrer">https://github.com/srbcheema1/medipack</a> and checkout examples in README.</p>\n'}]
['https://Stackoverflow.com/questions/43890', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1820/']