silence out regions of audio based on a list of time stamps , using sox and python

荒凉一梦 提交于 2019-11-29 12:40:58

I would probably solve this with a zsh script and awk.

If the times are given like this:

bits

1.6 8.3
13.2 33.7
35.0 38.0
42.0 51.0
70.2 73.7
90.0 99.2
123.0 131.1

Calculate the silence bits like this:

awk '{ print $1, $2, $1 - p; p = $2 }' bits

Output:

1.6 8.3 1.6
13.2 33.7 4.9
35.0 38.0 1.3
42.0 51.0 4
70.2 73.7 19.2
90.0 99.2 16.3
123.0 131.1 23.8

You are now be able to generate the desired command-line with something like this:

args="sox "
m=file.mp3
awk '{ print $1, $2, $1 - p; p = $2 }' bits |
while read s e n; do
  args+="\"|sox -n -p trim 0 $n\" "
  args+="\"|sox $m -p trim $s =$e remix 1\" "
done
args+="out.wav"
echo "$args"

Pipe it into /bin/sh to execute:

... | sh

The output from sox should now be in out.wav.

I was able to implement with a workaround.

See : create new list from list of lists in python by grouping

What I did was create a new list containing the regions between segments and then pass it on to sox. At the moment whatever I pass to sox gets removed. So I calculated regions to be removed and then passed it on to sox. It worked pretty well.

Solution is still inverted , but I don't have to change anything in the sox.

I won't accept my answer as an answer. Hoping someone is able to come up with a solution which involves modifying sox commands and not have to recalculate segments like I did.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!