24

I have several long audio files (80 minutes each; m4a) and want them split into 5- or 10-minute pieces.

I see many questions for splitting an audio file at extended pauses, tones, etc. but can't seem to find one regarding this simple operation. Does anyone know a simple way to do this?

Of course, something automatic, and command-line is okay—even preferred.

Thanks in advance!

8 Answers 8

33

With recent ffmpeg (download a static build from here):

ffmpeg -i in.m4a -f segment -segment_time 300 -c copy out%03d.m4a

This uses the segment muxer and copies the bitstreams. If your file has audio and video streams, you can disable the video stream with -vn.


Here's a one-liner, you just need Ruby and FFmpeg installed:

ruby -e '(0..4500).step(300) { |x| system "ffmpeg -ss #{x} -i in.m4a -c copy -t 300 out-#{x}.m4a"}'

Simply execute that in the same folder where in.m4a is. It'll copy the audio bitstream, so executing this will probably take less than a few seconds.

To explain:

  • 4800 seconds is the length of the audio file (80 minutes × 60 seconds), so our last split is at 4500 (4800 - 300 seconds).
  • We go from 0 to 4500, and we split every 300 seconds (5min × 60s).
  • FFmpeg will start at the time specified by -ss
  • And it'll copy for the time specified by -t
  • It'll write several output files called out-<x>.mp4, where <x> is the start time in seconds.
4
  • Thanks! Is your command for UNIX, or does it work on Windows too? I have a Cygwin installation, so it shouldn't matter, but just wondering. I'll try it when I get home. Dec 28, 2012 at 14:54
  • This natively works on Linux (and Unixes that have Ruby installed), and it should also work on Windows – you simply need Ruby and FFmpeg, but you won't need Cygwin for that. There's Ruby Installer and FFmpeg executables for Windows as well. Now, the FFmpeg command could be automated without Ruby, using Windows batch files, or PowerShell, but I'm not a Windows user, so that's why I took Ruby (because it comes with Linux and OS X). Maybe someone with Windows batch experience can rewrite the Ruby command above?
    – slhck
    Dec 28, 2012 at 15:03
  • I have Cygwin because I can't get used to the Windows command-line. I think I already have ruby and ffmpeg installed there, so I should be able to use your command. But yes, a Windows version might help someone else viewing this question. +1 for now; will accept when I try it. Dec 28, 2012 at 15:35
  • 2
    If, like me, your m4a file includes a video stream (in my case, a single image) then you need to add the -vn switch to select just the audio stream. If you want to keep the video stream you'll need to output to a file with an .mp4 extension.
    – Rob Church
    Feb 1, 2014 at 20:52
4

I was looking for a solution for this myself. The easiest way I found to do this is via MP4Box:

mp4box -split 300 infile.m4a

Where "300" is the time in seconds. MP4Box will automatically write numbered output files.

(You don't need to install the whole big GPAC application. These are the files you can extract from the archive for it to work: js.dll; libeay32.dll; libgpac.dll; MP4Box.exe; ssleay32.dll)

4

It has been a while since this question was asked, but after fooling around with Ruby and ffmpeg I decided there has to be an easier way to do this within Windows, and I found there is.

Download and install Audacity

Create a text file somewhere you can find it and paste the following string into it: (300 second intervals, ie. 5 minutes, 10 minutes would be 600 intervals, etc...)

0       300  
300     600  
600     900  
900     1200  
1200    1500  
1500    1800  
1800    2100  
2100    2400  
2400    2700  
2700    3000  
3000    3300  
3300    3600  
3600    3900  
3900    4200  
4200    4500  
4500    4800  
4800    5100  
5100    5400  
5400    5700  
5700    6000  
6000    6300  
6300    6600  
6600    6900  
6900    7200  
7200    7500  
7500    7800  
7800    8100  
8100    8400  
8400    8700  
8700    9000  
9000    9300  
9300    9600  
9600    9900  
9900    10200  
10200   10500  
10500   10800  
10800   11100  
11100   11400  
11400   11700  
11700   12000  
12000   12300  
12300   12600  
12600   12900  
12900   13200  
13200   13500  
13500   13800  
13800   14100  
14100   14400  
14400   14700  
14700   15000  

Just make sure that the numbers of intervals is greater than the total length of your audio files, I created this one which runs for over three hours, plenty long for my needs, append more to it to suit your needs.

Import the audio file into Audacity and make whatever balancing and whatever other edits you want to, then click on file>labels>import labels

Select your text file, then select the label track and the audio track at the same time and click on file>Export Multiple.

Change the filetype you want to export to and adjust the settings, then select the destination folder.

I always change the numbering pattern to Numbering After Filename Prefix and type the filename I want in the box and the program will add the numbers, hit Export and viola!

1
  • Values should be tab-separated. Also in my case, extra labels are also exported but it is a small inconvenience.
    – Mikrasya
    Mar 1, 2021 at 4:24
3

I know the original question was asking about 'how-to' via the command line, but I see all of these other programs being listed and I am kinda shocked that no one has mentioned VLC. I agree that using ffmpeg -i infile -f segment -segment_time time -c copy out%03d.wav is probably the most simple via the cmd_line, VLC can cut one or multiple files (audio & video) to very specific times (to the millisecond) and output the files in many different formats, very easily. Just open a file/files/or directory a choose Convert/Save, check the Advanced Options box and set the time. Next page allows you to choose the output format. As far as the cmd_line, I started reading through the man pages and the extremely long help section for cvlc, and have not yet figured out a simple way, like above, to do the same (If someone knows how...). You can play the audio files with cvlc filename.

2
  • Cool, thanks. It's possible that 10 years ago when I asked this question, VLC didn't have that feature yet. (Or I could be wrong.) Jul 11, 2022 at 1:19
  • 2
    OMG, I didn't even notice the date. It was on my first page, so I thought it was recent, lol! Either way, your question led to some awesome answers and things I didn't know you could do. Jul 11, 2022 at 12:59
0

MP4Box works perfect. It is always cool when you can get the things done from terminal. Faster way.

This is the download page: https://gpac.wp.imt.fr/downloads/gpac-nightly-builds/ You install the program, then you can work from terminal, nothing more needed.

Tested, command dfkt said works perfect: mp4box -split 59 infile.m4a

In my case, i wanted to split in m4a <1min, for easy google speech-to-text use.

Audacity can work ok, but in my case i always prefer work with commands than with graphical UI. Best solution so for mp4box. I'm not giving new info, the credit is for dfkt (thanks man). I'm only validating it is a great solution.

0

Using Audacity:

  1. Open the audio file to split
  2. Select > All
  3. Tools > Regular Interval Labels...
    • You can then select to create a fixed number of labels, or labels every x seconds
  4. File > Export > Export Multiple...
  • Ensure Split files based on: is set to Labels
0
import os
import tkinter as tk
from tkinter import filedialog
from pydub import AudioSegment
from pydub.utils import make_chunks

def browse_file():
    file_path.set(filedialog.askopenfilename(filetypes=[("Audio Files", "*.mp3;*.wav")]))
    
def cut_audio():
    input_file = file_path.get()
    if not input_file:
        return

    output_dir = 'outputs'
    os.makedirs(output_dir, exist_ok=True)

    cut_interval = int(cut_interval_var.get())
    audio = AudioSegment.from_file(input_file)
    chunks = make_chunks(audio, cut_interval * 1000)
    
    for i, chunk in enumerate(chunks):
        output_file = os.path.join(output_dir, f"chunk-{i}.{audio.format}")
        chunk.export(output_file, format=audio.format)

    result_label.config(text=f"Audio cut into {len(chunks)} segments")

root = tk.Tk()
root.title("Audio Cutter")

file_path = tk.StringVar()
cut_interval_var = tk.StringVar()

frame1 = tk.Frame(root)
frame1.pack(padx=10, pady=10)
frame2 = tk.Frame(root)
frame2.pack(padx=10, pady=10)
frame3 = tk.Frame(root)
frame3.pack(padx=10, pady=10)

tk.Label(frame1, text="Audio File:").pack(side=tk.LEFT)
tk.Entry(frame1, textvariable=file_path, width=40).pack(side=tk.LEFT)
tk.Button(frame1, text="Browse", command=browse_file).pack(side=tk.LEFT)

tk.Label(frame2, text="Cut Interval (seconds):").pack(side=tk.LEFT)
tk.Entry(frame2, textvariable=cut_interval_var, width=10).pack(side=tk.LEFT)

tk.Button(frame3, text="Cut Audio", command=cut_audio).pack(side=tk.LEFT)
result_label = tk.Label(frame3, text="")
result_label.pack(side=tk.LEFT)

root.mainloop()

To use this program, save it as audio_cutter.py and run it using Python. A window with a file input and a cut interval input will appear. Click "Browse" to upload an audio file, input the desired cut interval in seconds, and click "Cut Audio." The program will create a folder called "outputs" and save the audio segments in it.

1
  • Does it support all audio formats? Which codec does it use for encoding?
    – jarno
    Mar 30, 2023 at 7:29
-1

Download Audacity - It's a free audio tool that can do exactly what you need, and it's totally free and easy to use. http://audacity.sourceforge.net/

2
  • 2
    Is there a way to make Audacity do this automatically? I wouldn't want to manually chop multiple files into 16 pieces each by selecting 5-minute pieces with my mouse, cut them, go to the menu, save the files, et cetera.
    – slhck
    Dec 28, 2012 at 14:41
  • I already have Audacity, so if there is an automatic way, please let me know! Dec 28, 2012 at 14:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .