how to use sox to compress mp3? - compression

I'd like to reduce the file size of some mp3 audios with sox. I think I can reduce stereo to only 1 channel (i.e. mono), reduce the sample rate and reduce the bit-depth. I find it seems sox can do none of that. Is this true or did I have done it correctly?
$ sox -r 8000 -c 1 2008-12-28-2.MP3 foo.mp3
sox WARN formats: can't set sample rate 8000; using 44100
sox WARN formats: can't set 1 channels; using 2

The arguments should go before the output file:
sox 2008-12-28-2.MP3 -r 8000 -c 1 foo.mp3
They can also be supplied as effects:
sox 2008-12-28-2.MP3 foo.mp3 remix 1 rate 8000

Related

To encode image using jbig2enc

I am trying to encode image using JBIG2 encoder that I have installed using Macports.
https://ports.macports.org/port/jbig2enc/
I have also installed leptonica from Macports:
https://ports.macports.org/port/leptonica/
The system seems to have installed it:
% jbig2 -V --version
jbig2enc 0.28
Also, from jbig2 --help I am getting this
% jbig2 --help
Usage: jbig2 [options] <input filenames...>
Options:
-b <basename>: output file root name when using symbol coding
-d --duplicate-line-removal: use TPGD in generic region coder
-p --pdf: produce PDF ready data
-s --symbol-mode: use text region, not generic coder
-t <threshold>: set classification threshold for symbol coder (def: 0.85)
-T <bw threshold>: set 1 bpp threshold (def: 188)
-r --refine: use refinement (requires -s: lossless)
-O <outfile>: dump thresholded image as PNG
-2: upsample 2x before thresholding
-4: upsample 4x before thresholding
-S: remove images from mixed input and save separately
-j --jpeg-output: write images from mixed input as JPEG
-a --auto-thresh: use automatic thresholding in symbol encoder
--no-hash: disables use of hash function for automatic thresholding
-V --version: version info
-v: be verbose
As the encoder refers to https://github.com/agl/jbig2enc for encoding the images I tried the command they have mentioned for encoding:
$ jbig2 -s feyn.tif >feyn.jb2
I ran it for an image original.jpg, This is what I am getting:
> jbig2 -s original.jpg >original.jb2
[1] 43894
zsh: command not found: gt
zsh: command not found: original.jb2
sahilsharma#Sahils-Air ~ % JBIG2 compression complete. pages:1 symbols:5 log2:3
?JB2
?|?n6?Q?6?(m?զu? Y???_?&??1???<?CJ?????#Rᮛ?O?V??:?,??i4?A?????5?;ސA??-!????5Ѧ??/=n܄?*?#|J6#?J?6?N1?n??v?"E}?.~?+????ڜ?]HO_b??~?[??????S2p𩗩????fC?????X?Z?????X=?m?????
??jN?????i????S?,j6???Br?V??F???8?w?#?6? uK?V??R?s~F-?F%?j????]j???0?!GG"'?!??)2v??K???h-???1
[1] + done jbig2 -s original.jpg
According to '--help', '-s' will do the lossless encoding.
The execution shows JBIG2 compression completed but no jb2 files have been formed.
Please help me in getting to know if the compression has taken place? Then where can I get the encoded image?
I am running this encoder to get to know the compression ratio. So I just want to know the encoded image size.
Use >, not >. The result will then be in feyn.jb2.

webm to mp3 conversion very slow with avconv

I am using this
passthru("youtube-dl -o '../temp/%(title)s.%(ext)s' --restrict-
filenames --no-part --no-progress --newline --extract-audio --audio-format mp3 -w --audio-quality 0 http://www.youtube.com$vid");
as a part of my code where I download song from youtube-dl, but after downloading it takes 14-15 seconds to convert that to mp3. Is there any way to improve that and make the conversion faster ? Like make avconv use all cores. If so, then how ?
Thank you.
try this:
avconv -i File.webm -threads 2 -acodec libmp3lame -ab 128k -vn -f mp3 file.mp3
You can use 2 or more threads and change bitrate to improve speed of Your script.
Regards

Convert PCM file decoded with opus_demo to WAV

I downloaded the Opus codec from their git repo, ran ./autogen.sh, ./configure, make, and then encoded a sample file from the opus examples page with ./opus_demo -e voip 48000 1 8000 -cbr -bandwidth NB -complexity 0 -forcemono speech_orig.wav speech_encoded.bit.
I then tried to decode it with ./opus_demo -d 48000 1 speech_encoded.bit speech_48khz.wav. When I try to play it back with VLC, it doesn't play it (it shows as playing for less than a second with no sound/feedback).
I am running Ubuntu 14.04 32-bit on a virtual machine (VirtualBox). The only link I found that was even remotely related to my problem is this, which relates to something else than the opus_demo file.
This is the output from opus_demo shell commands:
$ ./opus_demo -e voip 48000 1 8000 -cbr -bandwidth NB -complexity 0 -forcemono speech_orig.wav speech_encoded.bit
libopus 1.1.1-beta-38-gfc0276f
Encoding 48000 Hz input at 8.000 kb/s in narrowband with 960-sample frames.
average bitrate: 8.000 kb/s
maximum bitrate: 8.000 kb/s
active bitrate: 8.000 kb/s
bitrate standard deviation: 0.000 kb/s
$ ./opus_demo -d 48000 1 speech_encoded.bit speech_48khz.wav
libopus 1.1.1-beta-38-gfc0276f
Decoding with 48000 Hz output (1 channels)
average bitrate: 8.000 kb/s
maximum bitrate: 8.000 kb/s
bitrate standard deviation: 0.000 kb/s
Thanks in advance for any help!
Solved it! Posting what I did for future readers, in case someone has the same problem.
The problem was that the files I was passing into the encoder (opus_demo -e) were .wav files. The encoder strictly requires .pcm files, and the decoder strictly outputs .pcm files. In order to convert .wav files to .pcm files, I downloaded ffmpeg for windows, and ran the following commands:
To convert a .wav file into a .pcm file:
ffmpeg -i input.wav -f s16le -acodec output.pcm
To convert a .pcm file into a .wav file:
ffmpeg -f s16le -ar 48k -ac 1 -i input.pcm output.wav

apply for .pcm file as sox

I want to exchange 16kHz pcm --> 48kHz wav using sox.
however, pcm file isn't applied in sox.
so, I just changed pcm to raw,
and then
sox -r 16000 -e signed -b 16 -c 1 test.raw -r 48000 out.wav
Can I apply for pcm file not convert raw?
For the PCM file, since PCM's are headerless, you need to add '-t raw' as the first argument.
sox -t raw -r 16000 -e signed -b 16 -c 1 test.raw -r 48000 out.wav
Try that out.
Also try the different Endian settings; -L; -B; -x
though only use one at a time, and only if not using one doesn't work.
There is no need to convert the input file into raw. Sox can handle pcm files.
sox input.pcm -r 48000 output.wav
The input file can either be a .pcm or .wav.
Since .wav files have a header containing audio metadata (such as sample rate, bit precision, file length, etc), you don't have to pass any information about the input file. Hence, non need to use:
-r 16000 -e signed -b 16 -c 1
Converting pcm to raw you have just stripped down the file header.

Crop MP3 to first 30 seconds

Original Question
I want to be able to generate a new (fully valid) MP3 file from an existing MP3 file to be used as a preview -- try-before-you-buy style. The new file should only contain the first n seconds of the track.
Now, I know I could just "chop the stream" at n seconds (calculating from the bitrate and header size) when delivering the file, but this is a bit dirty and a real PITA on a VBR track. I'd like to be able to generate a proper MP3 file.
Anyone any ideas?
Answers
Both mp3split and ffmpeg are both good solutions. I chose ffmpeg as it is commonly installed on linux servers and is also easily available for windows. Here's some more good command line parameters for generating previews with ffmpeg
-t <seconds> chop after specified number of seconds
-y force file overwrite
-ab <bitrate> set bitrate e.g. -ab 96k
-ar <rate Hz> set sampling rate e.g. -ar 22050 for 22.05kHz
-map_meta_data <outfile>:<infile> copy track metadata from infile to outfile
instead of setting -ab and -ar, you can copy the original track settings, as Tim Farley suggests, with:
-acodec copy
I also recommend ffmpeg, but the command line suggested by John Boker has an unintended side effect: it re-encodes the file to the default bitrate (which is 64 kb/s in the version I have here at least). This might give your customers a false impression of the quality of your sound files, and it also takes longer to do.
Here's a command line that will slice to 30 seconds without transcoding:
ffmpeg -t 30 -i inputfile.mp3 -acodec copy outputfile.mp3
The -acodec switch tells ffmpeg to use the special "copy" codec which does not transcode. It is lightning fast.
NOTE: the command was updated based on comment from Oben Sonne
If you wish to REMOVE the first 30 seconds (and keep the remainder) then use this:
ffmpeg -ss 30 -i inputfile.mp3 -acodec copy outputfile.mp3
try:
ffmpeg -t 30 -i inputfile.mp3 outputfile.mp3
This command also works perfectly.
I cropped my music files from 20 to 40 seconds.
-y : force output file to overwrite.
ffmpeg -i test.mp3 -ss 00:00:20 -to 00:00:40 -c copy -y temp.mp3
you can use mp3cut:
cutmp3 -i foo.mp3 -O 30s.mp3 -a 0:00.0 -b 0:30.0
It's in ubuntu repo, so just: sudo apt-get install cutmp3.
You might want to try Mp3Splt.
I've used it before in a C# service that simply wrapped the mp3splt.exe win32 process. I assume something similar could be done in your Linux/PHP scenario.
I have got an error while doing the same
Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argumentStream mapping:
Fix for me was:
ffmpeg -ss 00:02:43.00 -t 00:00:10 -i input.mp3 -codec:a libmp3lame out.mp3
My package medipack is a very simple command-line app as a wrapper over ffmpeg.
you can achieve trimming your video using these commands:
medipack trim input.mp3 -s 00:00 -e 00:30 -o output.mp3
medipack trim input.mp3 -s 00:00 -t 00:30 -o output.mp3
you can view options of trim subcommand as:
srb#srb-pc:$ medipack trim -h
usage: medipack trim [-h] [-s START] [-e END | -t TIME] [-o OUTPUT] [inp]
positional arguments:
inp input video file ex: input.mp4
optional arguments:
-h, --help show this help message and exit
-s START, --start START
start time for cuting in format hh:mm:ss or mm:ss
-e END, --end END end time for cuting in format hh:mm:ss or mm:ss
-t TIME, --time TIME clip duration in format hh:mm:ss or mm:ss
-o OUTPUT, --output OUTPUT
you could also explore other options using medipack -h
srb#srb-pc:$ medipack --help
usage: medipack.py [-h] [-v] {trim,crop,resize,extract} ...
positional arguments:
{trim,crop,resize,extract}
optional arguments:
-h, --help show this help message and exit
-v, --version Display version number
you may visit my repo https://github.com/srbcheema1/medipack and checkout examples in README.