Create a silent mp3 from the command line
Asked Answered
W

5

37

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?

Widget answered 11/3, 2011 at 17:28 Comment(1)
In an ideal world I would be able to do this directly with ffmpeg and not need to use another library.Widget
H
12

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
Hydride answered 11/3, 2011 at 17:46 Comment(3)
This is a perfect solution SoX looks interesting but more than probably what I am looking for. I managed to get a silence using sourceforge.net/projects/wavi-avi2wav. This has a pre built silence.exe which will be easier to drop into use.Widget
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.wavWolbrom
The other answer is much more straightforward.Procurator
H
77

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).

Hypogeous answered 23/5, 2013 at 13:37 Comment(7)
do you know how to specify the bitrate of the output file?Fleenor
nvm, simply remove -q:a 9 and add -b:a 192k or whatever bitrate you wantFleenor
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).Nephogram
@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?Evolution
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.Muticous
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.Kayleen
ffmpeg -ar 48000 -t <duration> -f s16le -acodec pcm_s16le -ac 2 -i /dev/zero -acodec libmp3lame -aq 4 <filename> is significantly faster for meBetweentimes
H
12

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
Hydride answered 11/3, 2011 at 17:46 Comment(3)
This is a perfect solution SoX looks interesting but more than probably what I am looking for. I managed to get a silence using sourceforge.net/projects/wavi-avi2wav. This has a pre built silence.exe which will be easier to drop into use.Widget
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.wavWolbrom
The other answer is much more straightforward.Procurator
A
7

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

Anytime answered 9/4, 2019 at 10:8 Comment(0)
S
5

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.

Stacee answered 23/5, 2013 at 12:55 Comment(1)
dd if=/dev/zero bs=1 count=100 | lame -s 8 -r --bitwidth 8 --signed -b 8 - a.mp3Gamali
U
0

Another solution is to set volume to zero on existing file

ffmpeg -i existing_music_file.wav -t 4 -filter:a "volume=0.0001" silent.wav
Unbounded answered 14/1 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.