31

I am trying to create a silent / empty mp3 file of (x) seconds using the command line. Quite a simple task I thought!

It seems LAME is able to do something like this, but I am unable to find anything that leads to achieving this.

Has anyone been able to do something like this?

daaawx
  • 3,273
  • 2
  • 17
  • 16
Stephen
  • 3,607
  • 1
  • 27
  • 30
  • 1
    In an ideal world I would be able to do this directly with ffmpeg and not need to use another library. – Stephen Mar 11 '11 at 18:29

4 Answers4

70

You can use this command.

ffmpeg -f lavfi -i anullsrc=r=44100:cl=mono -t <seconds> -q:a 9 -acodec libmp3lame out.mp3

Change <seconds> to a number indicating the number of seconds of silence you require (for example, 60 will create a minute).

nico_lab
  • 1,044
  • 1
  • 8
  • 10
  • 1
    do you know how to specify the bitrate of the output file? – David Schumann May 17 '18 at 15:53
  • 2
    nvm, simply remove `-q:a 9` and add `-b:a 192k` or whatever bitrate you want – David Schumann May 17 '18 at 16:07
  • Works great. Especially lovely is the ability to install ffmpeg command-line tools without previously installing the whole mass of development and deployment tools (Xcode + Home-brew and all related junk). – Motti Shneor Jan 10 '19 at 12:59
  • @DavidSchumann to have a constant bit rate (CBR) works `-b:a 64k` (bit rate >= 64000), but `-b:a 32k` not works (it create a VBR). I don't get it, what I'm missing? – Giorgio Robino Nov 09 '20 at 18:37
  • For some reason this results in a very large MP3. With the compression mp3 uses, I would expect 10 minutes of silence to be quite small, but it's 3.3MB. One minute is 236K. – jrochkind May 05 '21 at 20:53
  • If you want to have a file that's shorter than 1 second, for instance 100 ms, "-t .1" does not work but "-t 0.1" does work. – user2363777 Aug 18 '21 at 21:38
  • `ffmpeg -ar 48000 -t -f s16le -acodec pcm_s16le -ac 2 -i /dev/zero -acodec libmp3lame -aq 4 ` is significantly faster for me – frumbert Apr 30 '22 at 05:45
11

Disclaimer: This is a unix-oriented approach (although sox is cross-platform and should get it done on windows only as well).

  • You'll need sox - "the [cross-platform] Swiss Army knife of sound processing programs".

  • This wrapper perl script, helps you generate any seconds of silence: http://www.boutell.com/scripts/silence.html

    $ perl silence.pl 3 silence.wav
    

silence.pl is quite short, so I include it here, since it's public domains:

#!/usr/bin/perl

$seconds = $ARGV[0];
$file = $ARGV[1];
if ((!$seconds) || ($file eq "")) {
        die "Usage: silence seconds newfilename.wav\n";
}

open(OUT, ">/tmp/$$.dat");
print OUT "; SampleRate 8000\n";
$samples = $seconds * 8000;
for ($i = 0; ($i < $samples); $i++) {
        print OUT $i / 8000, "\t0\n";
}
close(OUT);

# Note: I threw away some arguments, which appear in the original
# script, and which did not worked (on OS X at least)
system("sox /tmp/$$.dat -c 2 -r 44100 -e signed-integer $file");
unlink("/tmp/$$.dat");

Then just lame it:

$ lame silence.wav silence.mp3
David Schumann
  • 13,380
  • 9
  • 75
  • 96
miku
  • 181,842
  • 47
  • 306
  • 310
  • This is a perfect solution SoX looks interesting but more than probably what I am looking for. I managed to get a silence using http://sourceforge.net/projects/wavi-avi2wav/. This has a pre built silence.exe which will be easier to drop into use. – Stephen Mar 11 '11 at 18:28
  • 1
    Very nice, the script can even take decimal value as input. For my requirement I needed a very short (nearly empty) wav: silence.pl 0.1 silence.wav – Andre Miras Nov 22 '16 at 11:16
  • The other answer is much more straightforward. – Jonas Sourlier Mar 06 '21 at 21:19
5

sox -n -r 44100 -c 2 silence.wav trim 0.0 3.0 - this will create a 3sec stereo silence file. Here n for null file handler, r is the sample rate and c is the number of channels.

Then just lame it:

$ lame silence.wav silence.mp3

Anees Sadeek
  • 168
  • 1
  • 10
4

Avoid the nuisance of creating a wav header, and let lame handle a raw file:

dd if=/dev/zero bs=1M count=? | lame -r - - > silence.mp3

setting ?=2 gives a 11 second file (@ standard 44KhZ, etc... parameters).

Note: this was tested on Unix; I understand there are dd and lame for windows, too.

user495100
  • 41
  • 1