How to convert any mp3 file to .wav 16khz mono 16bit - mp3

Please, help to choose solution for converting any mp3 file to special .wav - I'm a newbie with Linux command line tools, so It's hard for me right now.
I need to get wav with 16khz mono 16bit sound properties from any mp3 file. I was trying
ffmpeg -i 111.mp3 -ab 16k out.wav,
but I got wav with the same rate as mp3 (22k).
Please, help to construct right command line

kdazzle's solution is almost there - it still output a stereo wav, here is a slightly modified version that generate mono:
ffmpeg -i 111.mp3 -acodec pcm_s16le -ac 1 -ar 16000 out.wav
also, if this is for pre-processing speech data for sphinx 4 see here: Convert audio files for CMU Sphinx 4 input

Try this:
ffmpeg -i 111.mp3 -acodec pcm_s16le -ar 16000 out.wav

Use this example:
import os
from pydub import AudioSegment
import numpy as np
from tqdm import tqdm
for src in tqdm (mp3_files):
des = src.replace('.mp3','.wav')
try:
sound = AudioSegment.from_mp3(src)
sound.set_channels(1)
sound = sound.set_frame_rate(16000)
sound = sound.set_channels(1)
sound.export(des, format="wav")
except:
print(src)
continue

Related

"RunTime Error: Failed to load audio" for mp3 file (waveform, torchaudio)

No matter how I import my audio file (through uploading it on google colab, importing it through google drive), I keep getting the same error. Could it be a path issue, and if so, how could I go about fixing it? When I run an "iPython.display", it displays the audio and I'm able to play it, but I'm not sure why torchaudio cannot load it.
Thanks in advance :)
waveform, sample_rate = torchaudio.load("Default-20220816-113844.mp3")
waveform = waveform.to(device)
if sample_rate != bundle.sample_rate:
waveform = torchaudio.functional.resample(waveform, sample_rate, bundle.sample_rate)
Assuming ("Default-20220816-113844.mp3") is not being found because of the path
On Colab to find the path click on the "..." (that appears on the right hand side of objects in the file browser when you hover over them) and choose COPY PATH to copy the complete path, the value will be copied without quotation marks.
At the time of the question is posted, Google Colab has pytorch==1.12 and torchaudio==0.12 pre-installed, so I assume you are getting the error with these versions.
Starting from TorchAudio 0.12, mp3 decoding requires FFmpeg.
When "sox_io" backend is used, first it tries to load audio using libsox, and when it fails, it further tries to load it with FFmpeg.
In Google Colab, you can run the following command to install the supported version.
!add-apt-repository -y ppa:savoury1/ffmpeg4
!apt-get -qq install -y ffmpeg
See also: https://pytorch.org/audio/0.12.1/tutorials/streaming_api_tutorial.html

How to snappy compress a file using a python script

I am trying to compress in snappy format a csv file using a python script and the python-snappy module. This is my code so far:
import snappy
d = snappy.compress("C:\\Users\\my_user\\Desktop\\Test\\Test_file.csv")
with open("compressed_file.snappy", 'w') as snappy_data:
snappy_data.write(d)
snappy_data.close()
This code actually creates a snappy file, but the snappy file created only contains a string: "C:\Users\my_user\Desktop\Test\Test_file.csv"
So I am a bit lost on getting my csv compressed. I got it done working on windows cmd with this command:
python -m snappy -c Test_file.csv compressed_file.snappy
But I need it to be done as a part of a python script, so working on cmd is not fine for me.
Thank you very much,
Álvaro
You are compressing the plain string, as the compress function takes raw data.
There are two ways to compress snappy data - as one block and the other as streaming (or framed) data
This function will compress a file using framed method
import snappy
def snappy_compress(path):
path_to_store = path+'.snappy'
with open(path, 'rb') as in_file:
with open(path_to_store, 'w') as out_file:
snappy.stream_compress(in_file, out_file)
out_file.close()
in_file.close()
return path_to_store
snappy_compress('testfile.csv')
You can decompress from command line using:
python -m snappy -d testfile.csv.snappy testfile_decompressed.csv
It should be noted that the current framing used by python / snappy is not compatible with the framing used by Hadoop

regex for files recognition for ffmpeg command

I'm trying to use ffmpeg command to create a time-lapse from a bunch of images in a folder. The images are named in the format -
folder_number;timestamp.jpg
eg,
0;1423116000.jpg
0;1423137600.jpg
I run the following command,
ffmpeg -i %*.jpg -q:v 2 output.mpeg
the video is getting formed but it is a 30seconds video with only the first image in the folder.
I only assume the regex i'm using - %*.jpg is wrong. Please help.
ps: I'm newbie to ffmpeg
I used the command
ffmpeg -f image2 -i 0;%*.jpg -q:v 2 output.mpeg
This did not work.
Renamed the files to have an underscore instead of semicolon.
Used the same command
ffmpeg -f image2 -i 0_%*.jpg -q:v 2 output.mpeg
Worked.

Can streamio-ffmpeg gem be used for metadata extraction from mp3 file?

Can we use 'streamio-ffmpeg gem' for extracting mp3 songs metadata(sample rate, duration etc) and write changed metadata back to mp3 file, if so than how to do that, please recommend some tutorials and codes ??
system("ffmpeg -i input_file -acodec mp3 -f mp3 -y output.mp3")
above command directly returns you output.mp3 file with only audio.
tested with ffmpeg version N-68500-g3ba1050

mp3 to gsm for asterisk

In our application we allow user to upload audio file. currently we support .wav format and we use "sox" to convert the .wav file to gsm for asterisk. We would like to have mp3 support as well. Any recommendation on how to get this done ?
Your help is highly appreciated. Thanks you.
Surely you can still use sox to convert mp3 to gsm/sln/alaw/ulaw format files
Look at http://sox.sourceforge.net/soxformat.html for more info on formats sox support
Here is alot of info about asterisk file conversion.
http://www.voip-info.org/wiki/view/Asterisk+sound+files
And for sure you can use asterisk(if it compiled with mp3 support)
asterisk -rx "file convert filename.mp3 filename.gsm"
If you want convert using sox, you have recompile sox with libmad support. Here is script:
cd /usr/src/
wget -c "http://downloads.sourceforge.net/mad/libmad-0.15.1b.tar.gz?modtime=1076976000&big_mirror=0"
wget -c 'http://downloads.sourceforge.net/mad/libid3tag-0.15.1b.tar.gz?modtime=1076976000&big_mirror=0'
wget -c "http://sourceforge.net/projects/sox/files/sox/14.3.2/sox-14.3.2.tar.gz/download"
wget -c http://downloads.sourceforge.net/project/lame/lame/3.98.4/lame-3.98.4.tar.gz
wget -c "http://downloads.sourceforge.net/mad/madplay-0.15.2b.tar.gz?modtime=1077580800&big_mirror=0"
CPU=4
for i in libid3tag libmad madplay lame sox
do
cd /usr/src/
tar -xzf $i-*.gz
cd /usr/src/$i*
./configure;
make -j $CPU
make install
done;
echo /usr/local/lib/ >>/etc/ld.so.conf
ldconfig
sox
ln -s /usr/local/bin/sox /usr/bin/sox
I used a software called "switch" from NCH Software and work very well to GSM conversion and with Elastix PBX. The only problem is on Elastix GUI the upload that dont work on System Records, so I had to upload using SCP aka SFTP over SSH.
http://www.nch.com.au/switch/
Solution 1:-
localhost*CLI> help file convert
Usage: file convert
Convert from file_in to file_out. If an absolute path is not given, the
default Asterisk sounds directory will be used.
Example:
file convert tt-monkeys.gsm tt-monkeys.ulaw
Solution 2:- There are some online tool available which can work for you.
https://convertio.co/
It can convert your file in almost 20 formats. e.g gsm, wav, cdma,wma etc.