DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Adding silence to audio track?  (Read 17,306 times)

Attronarch

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 148
Adding silence to audio track?
« on: January 09, 2016, 12:49 PM »
I have several hour-long audio tracks to which I have to add silence. It should go 30 seconds of music, 30 seconds of silence, and so on, until the end of track.

My first try was Audacity, which can add silence, but I couldn't figure it out how to add it to the whole track in same interval.

Any recommendations?

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
Re: Adding silence to audio track?
« Reply #1 on: January 09, 2016, 01:32 PM »
Someone who uses Audacity or another program may have specific instructions.  In the meantime I would suggest checking the Sourceforge forum for Audacity as someone may have asked the same question.  Also I found a write-up of 25 free audio editors.  One of them may have an automation feature as well as batch processing.

http://www.hongkiat.com/blog/25-free-digital-audio-editors/

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,175
    • Coding Snacks
    • Participated in NANY Challenge Three Times
    • Plugin Coding Medal
Re: Adding silence to audio track?
« Reply #2 on: January 09, 2016, 05:40 PM »
I made a script, try it. It requires Autohotkey and ffmpeg (and ffprobe that is included with ffmpeg).
https://autohotkey.com/
http://ffmpeg.zeranoe.com/builds/

update: fixed bugs with ffmpeg path (previously only worked when ffmpeg was in same folder as script)

update2: get the newer version here http://www.donationcoder.com/forum/index.php?topic=42184.0

Code: ( autohotkey)
#NoEnv
SetWorkingDir %A_ScriptDir%
#SingleInstance force

;30SecondSilencer
;adds 30 seconds silence every 30 seconds to an mp3 file

;-------------------------
;CHANGE THESE PARAMETERS
source = C:\folder\some music file.mp3
workdir = C:\dir\
chunksize = 30     ;length of each silence/music segment
ffmpeg = C:\ffmpeg\ffmpeg.exe 
;-------------------------

;check paths
SplitPath, source,,, ext, noext
SplitPath, ffmpeg,,fdir
if !FileExist(source) or !FileExist(workdir) or !FileExist(ffmpeg) or !FileExist(fdir "\ffprobe.exe")
 exitapp

;Clear old files
Filedelete %workdir%\30ss-silent.mp3
Filedelete %workdir%\30ss_*.mp3
Filedelete %workdir%\%noext%_30ss.mp3
Filedelete %workdir%\30ss_*.txt

;Get source duration in seconds
;http://superuser.com/a/945604
RunWait %comspec% /c ""%fdir%\ffprobe.exe" -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "%source%" >> "%workdir%\30ss_duration.txt""
FileRead, dur, %workdir%\30ss_duration.txt
length := Ceil(dur)
if !length
 exitapp
 
;split source into 30 second mp3 chunks
;http://stackoverflow.com/a/7945753
steps := Ceil(length / chunksize) ; number of steps
start = 0
ToolTip, Split chunks...
sleep 1000
Loop, %steps%
{
c := StrLen(a_index)==1 ? "00" a_index : StrLen(a_index)==2 ? "0" a_index : a_index
Run "%ffmpeg%" -ss %start% -i "%source%" -t %chunksize% "%workdir%\30ss_%c%.mp3" ,,Min
start := start + chunksize
ToolTip, %A_index% / %steps%
}

ToolTip, Processing...  ;wait for ffmpeg to make all chunks
Loop
{
	pcount = 0
	for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
		if InStr(process.Name,"ffmpeg.exe")
		 pcount++
	if pcount < 1
	 break
	ToolTip	Processing %pcount% left
	sleep 400
}

;make 30 sec silent audio mp3 file
;http://stackoverflow.com/a/32027123
ToolTip, make silent.mp3
sleep 1000
RunWait "%ffmpeg%" -f lavfi -i anullsrc -t 30 "%workdir%\30ss-silent.mp3",,Min


;Concatenate chunks and silence
;https://trac.ffmpeg.org/wiki/Concatenate
ToolTip, Concatenate...
Loop, Files, %workdir%\30ss_*.mp3
 FileAppend, file '%workdir%\%A_LoopFileName%'`nfile '%workdir%\30ss-silent.mp3'`n, %workdir%\30ss_list.txt
RunWait "%ffmpeg%" -nostats -loglevel 0 -f concat -i "%workdir%\30ss_list.txt" -c copy "%workdir%\%noext%_30ss.mp3" ,,Min

;clear chunks
Filedelete %workdir%\30ss_*.mp3

ToolTip, Finished!
sleep 1000

Out of curiousity what will you use the audio files for? crossfit/interval training perhaps?
« Last Edit: January 10, 2016, 09:57 AM by Nod5 »

Attronarch

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 148
Re: Adding silence to audio track?
« Reply #3 on: January 09, 2016, 06:04 PM »
Wow, thank you very much!

I've changed lines 10,11 and 13, but run into some trouble:

[REMOVED]

It is for my mother, she is Physical Education teacher, and among other things holds aerobic classes.
« Last Edit: January 10, 2016, 06:52 AM by Attronarch »

Curt

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 7,565
Re: Adding silence to audio track?
« Reply #4 on: January 09, 2016, 06:38 PM »
shouldn't the cuts fade in/out, instead of abrupt cutting?  :-\

Attronarch

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 148
Re: Adding silence to audio track?
« Reply #5 on: January 09, 2016, 08:02 PM »
shouldn't the cuts fade in/out, instead of abrupt cutting?  :-\

No, she didn't have such requirement.

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,175
    • Coding Snacks
    • Participated in NANY Challenge Three Times
    • Plugin Coding Medal
Re: Adding silence to audio track?
« Reply #6 on: January 09, 2016, 08:07 PM »
I see the problem, my bad. I fixed it in the post above. It should work now.

Attronarch

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 148
Re: Adding silence to audio track?
« Reply #7 on: January 09, 2016, 08:22 PM »
I see the problem, my bad. I fixed it in the post above. It should work now.

No errors pop out, but I also cannot find final mp3. I see work folder being filled with chunks, but after it is finished everything is deleted except:

  • 30ss-silent.mp3
  • 30ss_duration.txt

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,175
    • Coding Snacks
    • Participated in NANY Challenge Three Times
    • Plugin Coding Medal
Re: Adding silence to audio track?
« Reply #8 on: January 09, 2016, 08:27 PM »
shouldn't the cuts fade in/out, instead of abrupt cutting?  :-\
Should be doable with this https://ffmpeg.org/ffmpeg-filters.html#afade-1 , if anyone gets the hang of the correct commands line for fade in / out then post a snippet here and I'll integrate it in the script.

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,175
    • Coding Snacks
    • Participated in NANY Challenge Three Times
    • Plugin Coding Medal
Re: Adding silence to audio track?
« Reply #9 on: January 09, 2016, 08:29 PM »
No errors pop out, but I also cannot find final mp3. I see work folder being filled with chunks, but after it is finished everything is deleted except:

  • 30ss-silent.mp3
  • 30ss_duration.txt
There should be a new file named <workdir>\<source file name>_30ss.mp3

edit: I've tested it multiple times again and it works here.

the script also should create the file 30ss_list.txt , is that file present after you run the script? If so open it and check that the contents look like this
Code
file '<workdir path>\30ss_001.mp3'
file '<workdir path>\30ss-silent.mp3'
file '<workdir path>\30ss_002.mp3'
file '<workdir path>\30ss-silent.mp3'
...
« Last Edit: January 09, 2016, 08:45 PM by Nod5 »

Attronarch

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 148
Re: Adding silence to audio track?
« Reply #10 on: January 10, 2016, 06:53 AM »
Works perfect, thank you.

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,175
    • Coding Snacks
    • Participated in NANY Challenge Three Times
    • Plugin Coding Medal
Re: Adding silence to audio track?
« Reply #11 on: January 10, 2016, 09:56 AM »
newer version of 30SecondSilencer posted in the Coding Snacks section
http://www.donationcoder.com/forum/index.php?topic=42184.0

Original address: https://www.donationcoder.com/forum/index.php?topic=42179.0