Demuxing audio tracks from .mp4 using mp4box - audio

I have an .mp4 file that contains multiple audio tracks. The audio tracks are encoded using the ALAC codec. I want to extract those using mp4box.
What I've tryed so far for extracting a track is this:
mp4box -single 1 "inputfile.mp4" -out "outputfile.m4a"
What happens is, it works as that a outputfile.m4a is created, but something with the header/codec/file info seems to be malformated. I can open and play the file in VLC, but it fails to open with Windows Media Player/Windows Media Foundation (which I need). So either I am doing something wrong during the extraction or mp4box puts the file in a weird format.
The output of mp4box -info -v "outputfile.mp4" looks like this:
[iso file] Current top box start before parsing 0
[iso file] Read Box type ftyp size 20 start 0
[iso file] Current top box start before parsing 20
[iso file] Read Box type mdat size 8555577 start 20
[iso file] Current top box start before parsing 8555597
[iso file] Read Box type free size 75 start 8555597
[iso file] Current top box start before parsing 8555672
[iso file] Read Box type moov size 4895 start 8555672
[iso file] Read Box type mvhd size 108 start 8555680
[iso file] Read Box type trak size 4779 start 8555788
[iso file] Read Box type tkhd size 92 start 8555796
[iso file] Read Box type mdia size 4679 start 8555888
[iso file] Read Box type mdhd size 32 start 8555896
[iso file] Read Box type hdlr size 55 start 8555928
[iso file] Read Box type minf size 4584 start 8555983
[iso file] Read Box type smhd size 16 start 8555991
[iso file] Read Box type dinf size 36 start 8556007
[iso file] Read Box type dref size 28 start 8556015
[iso file] Read Box type url size 12 start 8556031
[iso file] Read Box type stbl size 4524 start 8556043
[iso file] Read Box type stsd size 108 start 8556051
[iso file] Read Box type alac size 92 start 8556067
[iso file] Read Box type stts size 24 start 8556159
[iso file] Read Box type stsc size 40 start 8556183
[iso file] Read Box type stsz size 3936 start 8556223
[iso file] Read Box type stco size 408 start 8560159
[iso file] Read Box type alac size 36 start 28
[iso file] Read Box type btrt size 20 start 64
* Movie Info *
Timescale 600 - 1 track
Computed Duration 00:01:30.928 - Indicated Duration 00:01:30.928
Fragmented File: no
File Brand isom - version 1
Created: GMT Thu Dec 17 14:35:48 2015
Modified: GMT Thu Dec 17 14:35:48 2015
File has no MPEG4 IOD/OD
Track # 1 Info - TrackID 1 - TimeScale 44100
Media Duration 00:01:30.929 - Indicated Duration 00:01:30.929
Media Info: Language "und (und)" - Type "soun:alac" - 979 samples
Audio Track - Sample Rate 44100 - 2 channel(s)
[ISOM Tools] codec parameters not known - setting codecs string to default value "alac"
RFC6381 Codec Parameters: alac
All samples are sync
I also tryed extracting the audio tracks using the -raw flag like this:
mp4box -raw 1 "inputfile.mp4"
It creates a inputfile.mp4_track1.alac file, which I can't open in any player (not even VLC). Looking at it in a HEX editor it looks like the raw data segment (which makes sense I guess), but I can't get mp4box to put it back into a .m4a which I can play.
So I guess the question is has anybody experience with mp4box? Am I maybe missing some flag while extracting? Or is mp4box simply producing wrong results? I hope somebody can point me in the right direction. Thanks in advance!

mp4box has extracted the raw audio stream to the file inputfile.mp4_track1.alac.
I can see two reasons why Windows Media Player cannot play it whereas VLC does :
- WMP does not support raw stream and and can only play audio stream encapsulated/muxed into a file container (e.g. : .MP4, .MKV, .AVI) ;
and/or
- WMP does not support natively ALAC format and you don't have this audio codec installed on your machine (try FFDShow).

Related

SOX exiting unusually, as if the user is canceling

I am trying to read a FM signal and piping it to sox. This is exiting unusually. Any idea what's going on?
Thanks.
$ rtl_fm -f $137M -s 60k -g 45 -p 55 -E wav -E deemp -F 9 - | sox -t wav - abc.wav rate 11025
Found 1 device(s):
0: Realtek, RTL2838UHIDIR, SN: 00000001
Using device 0: Generic RTL2832U OEM
Found Rafael Micro R820T tuner
Tuner gain set to 44.50 dB.
Tuner error set to 55 ppm.
Tuned to 37480000 Hz.
Oversampling input by: 32x.
Oversampling output by: 1x.
Buffer size: 4.27ms
sox FAIL formats: can't open input `-': WAVE: RIFF header not found
Sampling at 1920000 S/s.
Output at 60000 Hz.
Signal caught, exiting!
User cancel, exiting...
WAV header needs size of the audio content. Normally it is set after the whole content was captured. In your example you pipe the content as you capture it, so header is not set.
You should either use those separately instead of piping, or, assuming this rtl command allows it, pipe raw data. This probably will require to manually specify format (sampling frequency, bitdepth, etc) of the incoming data on the sox side.

LAME -- decoding and encoding audio file

I used lame for decoding from mp3 file to raw pcm file or encoding into mp3 from from raw pcm.
The question is When I use one test.0.pcm file, doing encoding and decoding over and over again(generating 0.mp3, 1.mp3,2.mp3, ... and test.1.pcm, test.2.pcm ....), although the size of for all .pcm files or all .mp3 files remain the same, but the contents are different. I tried to listen these audio files and found that 99.mp3's volume is much less than 1.mp3.
The script I use is like following:
#!/bin/bash
num=$1
last=0
now=1
for((i=0;i<num;i++));do
lame -r -b 64 -s 16000 -m m test.$last.pcm $last.mp3
lame --decode --mp3input -t -m m -s 16000 $last.mp3 test.$now.pcm
last=$now
now=$[now+1]
done
The original test.0.pcm has only 1 channel and sampling freq is 16k.
Some logs are like following, they are all same except for Replay Gain:
input: 97.mp3 (16 kHz, 1 channel, MPEG-2 Layer III)
output: test.98.pcm (16 bit, Microsoft WAVE)
skipping initial 1105 samples (encoder+decoder delay)
skipping final 47 samples (encoder padding-decoder delay)
Frame# 49/49 64 kbps
Assuming raw pcm input file
LAME 3.100 64bits (http://lame.sf.net)
polyphase lowpass filter disabled
Encoding test.98.pcm to 98.mp3
Encoding as 16 kHz single-ch MPEG-2 Layer III (4x) 64 kbps qval=3
Frame | CPU time/estim | REAL time/estim | play/CPU | ETA
49/49 (100%)| 0:00/ 0:00| 0:00/ 0:00| 88.200x| 0:00
----------------------------------------------------------------------------------------------------------
kbps mono % long %
64.0 100.0 100.0
Writing LAME Tag...done
ReplayGain: +46.1dB
Noticing ReplayGain is increasing constantly but I have no knowledge about mp3 encoding method, so I am not sure if this is the reason.
MP3 is a lossy codec. You're going to lose quality each time you encode another generation.

Big file conversion .ogv to .avi ogv2avi on Ubuntu

I'm trying to convert a 200MB .ogv file to .avi with a script I found online:
#!/bin/bash
# ogv to avi
# Call this with multiple arguments
# for example : ls *.{ogv,OGV} | xargs ogv2avi
N=$#;
echo "Converting $N files !"
for ((i=0; i<=(N-1); i++))
do
echo "converting" $1
filename=${1%.*}
mencoder "$1" -ovc xvid -oac mp3lame -xvidencopts pass=1 -o $filename.avi
shift 1
done
After this all I have to do is $ ogv2avi name_of_file.ogv
and it creates the converted.avi file.
It works great for small file, but it seems to crash for big files, and I only get around the first 3 minutes of the 30 minute recording.
Too many audio packets in the buffer: (4096 in 850860 bytes).
Maybe you are playing a non-interleaved stream/file or the codec failed?
For AVI files, try to force non-interleaved mode with the -ni option.
Flushing video frames.
Writing index...
Writing header...
ODML: vprp aspect is 16384:10142.
Setting audio delay to 0.078s.
Video stream: 784.308 kbit/s (98038 B/s) size: 21254748 bytes 216.800 secs 3000 frames
Audio stream: 87.341 kbit/s (10917 B/s) size: 2372536 bytes 217.313 secs
I had the exact same problem, and the only way i got around it (a sloppy solution but it works) is to play the .ogv video on the Ubuntu Desktop and record the square were the video is located with a desktop recorder that don't produces .ogv files(I recommend Kazam which produces .webm files). Then use Audacity to edit the audio of the output video if necessary and mix the edited audio with the output video using MkvMerge.

Capturing Image from webcam in linux

I want to capture a image through webcam in linux.
So i searched on internet and found out it's can be done using mplayer or vlc.
Following is the command for capturing image by mplayer and its corresponding error.
[root#localhost ~]# mplayer -vo png -frames 1 tv://
MPlayer SVN-r31628-4.4.4 (C) 2000-2010 MPlayer Team
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
Playing tv://.
TV file format detected.
Selected driver: v4l2
name: Video 4 Linux 2 input
author: Martin Olschewski <olschewski#zpr.uni-koeln.de>
comment: first try, more to come ;-)
v4l2: unable to open '/dev/video0': No such file or directory
v4l2: ioctl set mute failed: Bad file descriptor
v4l2: 0 frames successfully processed, 0 frames dropped.
Exiting... (End of file)
[root#localhost ~]#
Please Help me in solving this error.
I searched on net and found out about v4l2, but still can't solve the problem.
Your error is pretty clear. The webcam apparently is not registering at /dev/video0. In some instances, I've found that encoders or other video devices register under /dev/videoX, where X can be any integer. Try modifying your statement to try different video devices.
You can also try using fswebcam which has a useful feauture of skipping the first few frames
-some webcam show corrupt (or green) images for the first frame or two:
fswebcam --skip 2
It can be set to capture an image every second:
fswebcam --skip 2 --loop 1
I made it by doing this:
mplayer tv:// -tv driver=v4l2:device=/dev/video0 -fps 1 -frames 2 -sstep 100 -vo jpeg; rm 00000001.jpg ; mv 00000002.jpg capture.$(date +%F_%R).jpg.
where:
"tv:// -tv driver=v4l2:device=/dev/video0" specifies the necessary driver and video device. You need to check if that device is the one you want to capture and or exists by doing ls /dev/video*.
-fps 1 -frames 2 -sstep 100 specifies the framerate and it's set to only one per second to ensure you will have enough time to wake up properly the camera, captures 2 frames and in between frames you had 100 fps from the output dropped to help with the camera focus.
-vo jpeg stands for jpg output format images, on my computer png doesn't work with all this configuration.
rm 00000001.jpg deletes the first frame capture, because always it's ging to be obscure, green or no focused, this is caused by the small amount of time neede by the camera to wake up.
mv 00000002.jpg capture.$(date +%F_%R).jpg makes the second frame image switch name from 00000002.jpg for "capture." plus the current date time.

Optimize PDF files (with Ghostscript or other)

Is Ghostscript the best option if you want to optimize a PDF file and reduce the file size?
I need to store alot of PDF files and therefore I need to optimize and reduce the file size as much as possible
Does anyone have any experience with Ghostscript and/or other?
command line
exec('gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4
-dPDFSETTINGS=/screen -sOutputFile='.$file_new.' '.$file);
If you looking for a Free (as in 'libre') Software, Ghostscript is surely your best choice. However, it is not always easy to use -- some of its (very powerful) processing options are not easy to find documented.
Have a look at this answer, which explains how to execute a more detailed control over image resolution downsampling than what the generic -dPDFSETTINGS=/screen does (that defines a few overall defaults, which you may want to override):
How to downsample images within pdf file?
Basically, it tells you how to make Ghostscript downsample all images to a resolution of 72dpi (this value is what -dPDFSETTINGS=/screen uses -- you may want to go even lower):
-dDownsampleColorImages=true \
-dDownsampleGrayImages=true \
-dDownsampleMonoImages=true \
-dColorImageResolution=72 \
-dGrayImageResolution=72 \
-dMonoImageResolution=72 \
If you want to try if Ghostscript is able to also 'un-embed' the fonts used (sometimes it works, sometimes not -- depending on the complexity of the embedded font, and also on the font type used), you can try to add the following to your gs command:
gs \
-o output.pdf \
[...other options...] \
-dEmbedAllFonts=false \
-dSubsetFonts=true \
-dConvertCMYKImagesToRGB=true \
-dCompressFonts=true \
-c ".setpdfwrite <</AlwaysEmbed [ ]>> setdistillerparams" \
-c ".setpdfwrite <</NeverEmbed [/Courier /Courier-Bold /Courier-Oblique /Courier-BoldOblique /Helvetica /Helvetica-Bold /Helvetica-Oblique /Helvetica-BoldOblique /Times-Roman /Times-Bold /Times-Italic /Times-BoldItalic /Symbol /ZapfDingbats /Arial]>> setdistillerparams" \
-f input.pdf
Note: Be aware that downsampling image resolution will surely reduce quality (irreversibly), and dis-embedding fonts will make it difficult or impossible to display and print the PDFs unless the same fonts are installed on the machine....
Update
One option which I had overlooked in my original answer is to add
-dDetectDuplicateImages=true
to the command line. This parameter leads Ghostscript to try and detect any images which are embedded in the PDF multiple times. This can happen if you use an image as a logo or page background, and if the PDF-generating software is not optimized for this situation. This used to be the case with older versions of OpenOffice/LibreOffice (I tested the latest release of LibreOffice, v4.3.5.2, and it does no longer do such stupid things).
It also happens if you concatenate PDF files with the help of pdftk. To show you the effect, and how you can discover it, let's look at a sample PDF file:
pdfinfo p1.pdf
Producer: libtiff / tiff2pdf - 20120922
CreationDate: Tue Jan 6 19:36:34 2015
ModDate: Tue Jan 6 19:36:34 2015
Tagged: no
UserProperties: no
Suspects: no
Form: none
JavaScript: no
Pages: 1
Encrypted: no
Page size: 595 x 842 pts (A4)
Page rot: 0
File size: 20983 bytes
Optimized: no
PDF version: 1.1
Recent versions of Poppler's pdfimages utility have added support for a -list parameter, which can list all images included in a PDF file:
pdfimages -list p1.pdf
page num type width height color comp bpc enc interp objectID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------
1 0 image 423 600 rgb 3 8 jpeg no 7 0 52 52 19.2K 2.6%
This sample PDF is a 1-page document, containing an image, which is compressed with JPEG-compression, has a width of 423 pixels and a height of 600 pixels and renders at a resolution of 52 PPI on the page.
If we concatenate 3 copies of this file with the help of pdftk like so:
pdftk p1.pdf p1.pdf p1.pdf cat output p3.pdf
then the result shows these image properties via pdfimages -list:
pdfimages -list p3.pdf
page num type width height color comp bpc enc interp objectID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------
1 0 image 423 600 rgb 3 8 jpeg no 4 0 52 52 19.2K 2.6%
2 1 image 423 600 rgb 3 8 jpeg no 8 0 52 52 19.2K 2.6%
3 2 image 423 600 rgb 3 8 jpeg no 12 0 52 52 19.2K 2.6%
This shows that there are 3 identical PDF objects (with the IDs 4, 8 and 12) which are embedded in p3.pdf now. p3.pdf consists of 3 pages:
pdfinfo p3.pdf | grep Pages:
Pages: 3
Optimize PDF by replacing duplicate images with references
Now we can apply the above mentioned optimization with the help of Ghostscript
gs -o p3-optim.pdf -sDEVICE=pdfwrite -dDetectDuplicateImages=true p3.pdf
Checking:
pdfimages -list p3-optim.pdf
page num type width height color comp bpc enc interp objectID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------
1 0 image 423 600 rgb 3 8 jpeg no 10 0 52 52 19.2K 2.6%
2 1 image 423 600 rgb 3 8 jpeg no 10 0 52 52 19.2K 2.6%
3 2 image 423 600 rgb 3 8 jpeg no 10 0 52 52 19.2K 2.6%
There is still one image listed per page -- but the PDF object ID is always the same now: 10.
ls -ltrh p1.pdf p3.pdf p3-optim.pdf
-rw-r--r--# 1 kp staff 20K Jan 6 19:36 p1.pdf
-rw-r--r-- 1 kp staff 60K Jan 6 19:37 p3.pdf
-rw-r--r-- 1 kp staff 16K Jan 6 19:40 p3-optim.pdf
As you can see, the "dumb" concatentation made with pdftk increased the original file size to three times the original one. The optimization by Ghostscript brought it down by a considerable amount.
The most recent versions of Ghostscript may even apply the -dDetectDuplicateImages by default. (AFAIR, v9.02, which introduced it for the first time, didn't use it by default.)
You can obtain good results by converting from PDF to Postscript, then back to PDF using
pdf2ps file.pdf file.ps
ps2pdf -dPDFSETTINGS=/ebook file.ps file-optimized.pdf
The value of argument -dPDFSETTINGS defines the quality of the images in the resulting PDF. Options are, from low to high quality: /screen, /default, /ebook, /printer, /prepress, see http://milan.kupcevic.net/ghostscript-ps-pdf/ for a reference.
The Postscript file can become quite large, but the results are worth it. I went from a 60 MB PDF to a 140 MB Postscript file, but ended up with a 1.1 MB optimized PDF.
I use Ghostscript with following options taken from here.
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen \
-dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
You may find that pdftocairo (from Poppler) can make smaller PDFs but beware that it will strip some features (such as hyperlinks) away.
This worked for me
Convert your PDF to PS (this creates a large file
pdf2ps large.pdf very_large.ps
Convert the new PS back to a PDF
ps2pdf very_large.ps small.pdf
Source:
https://pandemoniumillusion.wordpress.com/2008/05/07/compress-a-pdf-with-pdftk/
You will lose in quality but if it's not an issue then ImageMagick's convert may proves helpful :
convert original.pdf reduced.pdf
Note that it doesn't always work : I once converted a 126 MB file into a 14 MB one using this command, but another time it doubled the size of a 350 Ko file.
Anyway it's worth giving it a try…
As mentioned in comments, of course there is no point in applying this command on a vector-based PDF, it will only be useful on rasterized images.
See also this post for related options.
Ghostscript comes with ps2pdf14 utility which can be used to optimise PDF file(s) but on some occasions size of "optimised" file may be bigger than original.
For the PDF which size is mainly due to embedded images (pdfimages -list is your friend), typically scanned documents, I would recommend the use of ocrmypdf which is quite good at optimizing, with optional OCR layer as a bonus.

Resources