Archive for the ‘ffmpeg/mplayer/mencoder’ Category

Play your entire music collection - Mplayer One Liner

Saturday, April 16th, 2011

Sometimes just plain lazy, and want just to play music. Run this command in the background (ALT+F2) or in a terminal for playback of all your mp3/ogg/m4a/wma, shuffled, looped, normalised. What’s nice about this is that the shuffle algorithym does a good job and with a large collection of music you don’t get repeats. I found that trying the same thing with vlc meant the shuffle was  always the same, and so you always started with the same music. :( Anyway the one-liner:

find ~/Music \( -iname “*\.mp3″ -o -iname “*.m4a” -o -iname “*.ogg” -o -iname “*.wma” \) -exec mplayer -nocache -af volnorm -shuffle -loop 0 ‘{}’ + &

Change “~/Music” to suit.

Even better put this in a script, and link to a keyboard shortcut, or add to your startup?

Now running the one liner from a terminal takes a bit of stopping, you’ll need to CTRL+C a couple of times in quick succession to close find then mplayer. Or you can run another one-liner:

killall -15 find mplayer

If you run the command from a script, you’ll have to shut down the script first, and then kill off the running instances of find and mplayer. Best to also do this from a script as well. Say the script you use to run is called singles.sh, let’s create another called endsingles.sh which contains:

#!/bin/bash

killall -15 singles.sh
killall -15 find
killall -15 mplayer

I am sure someone clever could bind this all together in one script, based upon a keyboard shortcut toggle or something…

Jinamp Party Piece

Saturday, February 26th, 2011

Jinamp Party Piece

Ok, daughter having birthday party, kicking us out of the house for the evening. Last year, I set her up with Audacious on the htpc so that she and her guests could chose music to play, with a couple of backup options of using xbmc or a DVD full of mp3s. This year we have a new machine running as htpc, no DVD drive. Also, daughter just wants a shuffled playlist to run, with no intervention.

A few weeks ago I was mooching about on KMandla’s new wiki of CLI apps, and came across jinamp , which is a sort of background player. What it does is takes the feed of a directory or playlist, shuffles it, then sequences it to a player of its or your choice, then disappears off into the background.

So I built up a playlist using audacious, called it pop.m3u and saved it to a shared place on the network. I needed to tidy up this list to just have file paths and names so:

sort pop.m3u > pop.lst

*.lst is the default file format for playlists with jinamp. Next I just opened up pop.lst and deleted all the EXTINF lines, which were now nicely sorted to the top. So now the playlist contents looked like this:

/path/to/song1.mp3
/path/to/song2.mp3
/path/to/song3.mp3

Order doesn’t matter because it is going to get shuffled anyway ;)

So downloaded and installed the latest (and pretty old) version of jinamp

sudo dpkg -i jinamp_1.0-1.i386.deb

I also had to install mplayer, as I was going to use this as my player:

sudo apt-get install mplayer

Added just one line to mplayer’s ~/.mplayer/config file for now:

af=volnorm

in the hope that this would help to normalise sound levels (seems to do something)

Right ready to test now, here is the basic command to get it running:

jinamp -p /usr/bin/mpayer /path/to/pop.lst

There is a little pause, and then music starts playing, you are back at the command prompt, and can no longer interact with the program. If you look in htop, you will see an entry for jinamp and also one for mplayer playing the file.

I set up the htpc with a fullscreen terminal, which would just serve to confuse (no fiddling) and amaze (how cool and geeky!) 17 year olds :) Had to run this with X, as for some reason I can’t get a VT over hdmi.

But of course you need to be able to interact with jinamp, to stop it (it will just keep on going!) or to skip to the next song. You might also want to display the title and artist of the current playing song as well. This is where jinamp-ctl comes in.

jinamp-ctl stop

will stop the thing running and close the player.

jinamp-ctl next

will move you to the next song on “jinamps” list.

jinamp-ctl query

will show you the name of the currently playing song (along with its full path!)

So I set about figuring out how to start the player, and then show the name of the currently playing song in the middle of the screen, without the mp3 extension, no cursor or mouse cursor, in a lovely cyan colour with a black background. I needed a script!

To remove the terminal cursor you have to type:

setterm -cursor off

To remove the mouse cursor, the easiest solution I found was to install “unclutter” and then run it, removes the idle cursor from the screen after 5 seconds by default. Further usage and investigation shows that unclutter runs at start up so if you like having it in your session, you don’t need the command. If you want to choose when to start unclutter, then edit the /etc/default/unclutter file and change true to false.

sudo apt-get install unclutter

unclutter

To change the colour of the screen text I needed “tput” which is already installed by default.

tput setaf 6

makes text cyan

tput setab 0

makes the background black (already is but useful to know)

tput bold adds

bold to the text to make it stand out more.
More on this here

Next was to strip the filename back to you the Artist and Title from the output of jinamp-ctl query. I did this by piping jinamp’s output into xargs and basename as follows:

jinamp-ctl query | xargs -0 -n1 -imp3 basename mp3 | cut -d”.”

It took a fair bit of googling to find all the elements required for this :)

Then I needed this output to update regularly. Too tricky for me to figure out what was required when the song actually changed, so I just went for a routine that fired every five seconds:

sleep 5

Finally, the solution to centre the text output on the screen. This takes a bit of trial and error, I am sure it can be figured out programatically rather than adjusting numbers, but for my purposes, this worked. The htpc screen is running at 800×600 (because the htpc is also linked to another older TV that can’t cope with more) so a terminal at full screen has @ 80 columns and 30 lines. I found that:

perl -e ‘print “\n”x10′;

added enough blank lines, and then piping all the output through to:

sed  -e :a -e ’s/^.\{1,57\}$/ & /;ta’

put the text more or less in the centre. ( I also set terminal font size to 16 ). Life is too short for me to learn and understand sed :)

So the final script called jinamp.sh looks like this:

#! /bin/bash#hides cursor
setterm -cursor off &

#hides mouse cursor after 5 seconds
unclutter & # (only needed if you haven’t rebooted)

#starts jinamp with named playlist

jinamp -p /usr/bin/mplayer “/media/music/burn/pop.lst”

#displays playing file every five seconds when jinamp is running

tput setaf 6;
tput setab 0;
tput bold;
while [ 1 ]; do
jinamp-ctl query | xargs -0 -n1 -imp3 basename mp3 | cut -d”.” -f1 && perl -e ‘print “\n”x10′; sleep 5; done | sed  -e :a -e ’s/^.\{1,57\}$/ & /;ta’

This is all fine, but daughter might wish to move to the next song. To save her fiddling about at the htpc with wireless mini keyboards and remotes, I simply set her up with a putty ssh session into the htpc so that she could type jinamp-ctl next.

Party is tonight, so we’ll see how well it goes. Had the thing running all morning without a hitch, but will have to write instructions in case of power failure, or some oik unplugs the htpc, etc…..

PS2 Eyetoy Webcam on Xubuntu 10.10 Recording and Playback

Sunday, January 9th, 2011

More fun to be had with your PS2 eyetoy as webcam. This all was tested using the silver eyetoy, but I see no reason why the black one won’t function just the same.
(Remember this is without pulseaudio!)
(a copy of the post I made on Ubuntuforums)
Recording Sound:

First off, define which device is generating the sound from the microphone

Code:

aplay -l

This tells me my eyetoy microphone is card 1, device 0, which translates to plughw:1,0. To record sound from the mic enter this command in a terminal:

Code:

arecord -f cd -t wav -c 2 -D plughw:1,0 foobar.wav

This records a wav file at CD quality.

Video Playback:

Lots of choices here:

Code:

mplayer tv:// -tv device=/dev/video0

Code:

cvlc v4l2:///dev/video0

and the monster that is gstreamer (you’ll need to ensure gstreamer-tools is installed)

Code:

gst-launch v4l2src device=/dev/video0 ! 'video/x-raw-yuv,width=640,height=480,framerate=30/1' ! xvimagesink

Capturing Video - Video and Sound:

This proved harder than I thought as vlc, ffmpeg and mencoder flatly refused to do the job with all the command lines I found out on google.

Using gstreamer again, although this is a bit unwieldy:
Capture Video only (no concurrent playback):

Code:

gst-launch v4l2src device=/dev/video0 ! 'video/x-raw-yuv,width=640,height=480,framerate=30/1' ! queue ! videorate! 'video/x-raw-yuv,framerate=30/1' ! theoraenc ! queue ! oggmux ! filesink location=output.ogg

Capture Video and Sound (with concurrent playback):

Code:

gst-launch v4l2src ! 'video/x-raw-yuv,width=640,height=480,framerate=30/1' ! tee name=t_vid ! queue ! videoflip method=horizontal-flip ! xvimagesink sync=false t_vid. ! queue ! videorate ! 'video/x-raw-yuv,framerate=30/1' ! queue ! mux. alsasrc device=plughw:1,0 ! audio/x-raw-int,rate=48000,channels=2,depth=16 ! queue ! audioconvert ! queue ! mux. avimux name=mux ! filesink location=output.avi

FFMPEG
Capture Video and Sound (variants on):

Code:

ffmpeg -t 10 -f video4linux2 -s 640x480 -r 15 -i /dev/video0 -f alsa -ac 2 -i plughw:1,0  -aspect 4:3 -s 720x400 -qscale 3 video-with-sound.flv

Code:

ffmpeg -t 10 -f video4linux2 -s 640x480 -r ntsc -i /dev/video0 -f alsa -ac 2 -i plughw:1,0 -qscale 3 video-with-sound.avi

Just video:

Code:

ffmpeg -f alsa video4linux2 -s 720x480 -r 30000/1001 -i /dev/video0 -sameq -aspect 4:3 -f avi -vcodec mjpeg -r 30000/1001 -y output.avi

Enjoy

Acer Aspire 110L Netbook CLI Installation

Monday, January 3rd, 2011

Did this a couple of years ago, forget what I did, so wrote it down this time. Had a bit of a fight getting the framebuffer up and running, but finally found the right blog post!

Distro:
Xubuntu 10.04 LTS ALT CD

Wired Connection (to start with)

#Apps
moc
mc
mplayer
fbi
htop
e-links
alpine

#Installs
openssh-server
screen
portmap
nfs-common
hwinfo
alsaequal + caps + libasound2-dev + libasound2-plugins + swh-plugins
aria2
wicd-curses
alsa-utils
build-essential
v86d

#Tweaks to Framebuffer

#edit /etc/default/grub
#add as follows:

GRUB_COMMAND_LINE_DEFAULT= nomodeset video=intelfb:mode_option=1024×600-24,mtrr=3,scroll=ywrap  (plus whatever else you want there)

GRUB_GFXMODE=1024×600
GRUB_GFXPAYLOAD_LINUX=1024×600

#in terminal:
sudo update-grub2

#edit /etc/initramfs-tools/conf.d/splash
#and add:
FRAMEBUFFER=yes
#or in terminal:
echo FRAMEBUFFER=y | sudo tee /etc/initramfs-tools/conf.d/splash
#then:
sudo update-initramfs -u

#reboot

#Tweaks to wicd
#It fails to start the network connection manager on installation added static IP, but it still takes a minute or two to make the connection, after that solid and quick to try manual config next

#Tweaks to Grub2
#see above for framebuffer
#only other change was to reduce grub timeout to 3:
#in terminal:
sudo nano /etc/default/grub
#edit as below:

GRUB_TIMEOUT=3

#in terminal:
sudo update-grub2

#Tweaks to mplayer
#Found that I needed to use vo=fbdev2 to play back movies on the framebuffer
#Also doesn’t completely take over when scale=1024:-3 & fs=yes is used so having to run setterm -cursor off blank 0,
#however in most situations will use video at fullscreen
#config file (in ~/.mplayer/config) as follows:
#in terminal:
nano ~/.mplayer/config
#add the following:

###
#gives proportionate scaling and centres on screen
#fs=yes
#vf=scale=1024:-3
####
#gives fullscreen :)
vf=scale=1024:600
###
vo=fbdev2
dr=yes
double=yes
framedrop=yes
ontop=yes
cache=8192
lirc=no
alang=en
slang=en
ao=alsa
#cuts down on the chatter - comment out if you need to debug
really-quiet=yes

#Tweaks to NFS
#Of course ensure portmap and nfs-common are installed on the AA0, and you have a working nfs server serving up shares you can connect to!
#Just did the usual, created mount points in /media and setup nfs shares in my fstab
#for example:
#in terminal:
sudo mkdir /media/films

#in fstab:

myserver:/films    /media/films    nfs    rsize=8192,wsize=8192,timeo=14,intr,bg

#Tweaks to AA0
#Fan Control - basically shuts it up
#acerfand / acer_ec.pl
#see acer fan post

#Hot plugging for SD cards
#in terminal:
sudo nano /etc/modprobe.d/aa0-fix-sd-slots.conf
#add the following content:

# Enable USB card reader
options pciehp pciehp_force=1
install sdhci for i in 2381 2382 2383 2384; do /usr/bin/setpci -d 197b:$i AE=47; done; /sbin/modprobe –ignore-install sdhci

#Then edit /etc/modules:
#in terminal:
sudo nano /etc/modules
#add the following to the end of the file:

#fixes SD slots
pciehp

#reboot

#Tweaks to alsaequal

#Create/edit ~/.asoundrc
#in terminal:
nano ~/.asoundrc

ctl.equal {
type equal;
}

pcm.plugequal {
type equal;
slave.pcm “plug:dmix”;
}

#switch the commenting of the next two lines if you don’t want alsaequal as your default soundcard
#pcm.equal {
pcm!.default {
type plug;
slave.pcm plugequal;
}

#force-reload all your alsa modules:
#in terminal:
sudo alsa force-reload
#and start EQing with:
#in terminal:
alsamixer -D equal

#starting up moc with mocp, then playing music, press q, moc goes away but music carries on, then:
#in terminal:
alsamixer -D equal
#and play with the settings until you are happy

#alsaequal does tend to use a fair few cpu cycles, moc running at @ 25% CPU.

#Tweaks to

I’ll have a proper play with alpine and elinks later

Recording with ALSA staring me in the face (and midi playback)

Tuesday, December 28th, 2010

Finding the right app to record stuff is a bit of a pain on linux, and playing back midi files can be a trial also. But everything you need is right there in your alsa-utils package. Open up a terminal and let’s get going!

Recording from a microphone:

 arecord -f cd -t wav -c 2 -D plughw:1,0 foobar.wav

This will record you microphone input from your chosen soundcard (in this case card 1, device 0) at 2 channel CD quality, outputting to a wav file for you to play with later. To playback your recording:

aplay foobar.wav

For more information and options, simply type:

man aplay

OK, to play back midi files, you may need to ensure you have timidity installed. Then find a midi file (*.mid) or a kar file (*.kar) and download it. Then it is simply:

 aplaymidi -l

to find an open port, in my case 128:0, then:

 aplaymidi mymidifile.mid

If you want to get the best out of kar files, then install pykaraoke, it will play back the midi and highlight the lyrics through the song :)

Linux - Listen to Microphone on remote PC

Sunday, December 26th, 2010

Due to the festive season there was a need to contact relatives in far flung places. Of course I hadn’t done any preparation, so things didn’t work as I wanted. I plugged a PS2 Eyetoy into my htpc, and installed Skype. Was pleased to see the video worked off the bat, but when contacting my first relly, I could see and hear them, they could see me, but could not hear me. The mic on the Eyetoy wasn’t configured correctly. Due to my work with special needs kids, I put my sign language (Makaton) skills (limited!) to good use, and we had a fun conversation as I taught my rellies sign language using sign language :)

Anyway set about reconfiguring my htpc to get the mic working later that evening. It’s a Ubuntu 10.10 setup, so I removed pulse audio, and got back to a basic alsa setup. (Note: my main PC is set up the same way) I did all the setup via ssh from my main desktop. Needed a way to test my microphone was working:

arecord -l

gave the the number of my device:

card 1: Namtai [EyeToy USB camera Namtai], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0

I then tracked down via google a command that with some modification allowed me to listen to the microphone on the speakers on my main PC (in a another room). I put my smartphone next to the eyetoy, and set it to play an audiobook I had on it. Ran back to my study and ssh’d into the htpc. Once there I had to run the command, which included ssh’ing back to my main PC (bet that would confuse 2 windows PCs if you tried it!) Command all on one line:

 arecord -f dat -D plughw:1,0 | ssh -C bimma@10.10.10.10 aplay -f dat

and blow me, the sound of my audiobook being “listened to” by the microphone came through loud and clear on my main PC speakers

Key things: getting the device right, the arecord -l command gave the information of card 1 and device 0, which translates to plughw:1,0 (the -D option denotes the device for arecord to listen to), and -f dat indicates the format the sound will be recorded in and relayed to the remote PC. Have a good read of man aplay, for more info.

Now to try things out on a different relly tomorrow ;)

Video Tearing Fixed - Xubuntu 9.10 - Nvidia ION

Sunday, February 14th, 2010

I use mplayer as my default video player, and have always had video tearing of some sort on my main PC. Having recently updated my PC to an Asus EB1012 net-top with the Nvidia ION graphics chip I found that enabling the vdpau options with mplayer gave me tear free playback. So I set out to resolve it for other file types (e.g. mp4 / avi)

I run Xubuntu by preference and normally use the Compositor to add transparency and shadows, and the default output plugin for mplayer is xv.

1. Turn off the compositor

2. This should produce tear free playback in totem and vlc

3. Open up nvidia-settings and ensure Sync to Vblank is ticked in  X Server XVideo Settings, and Sync to VBlank and Allow Flipping is ticked in Open GL Settings

4. edit the ~/.mplayer/config file

I grabbed the config file from GeexBox, as this served as a useful template to set up profiles for various file types.

For h264 / vdpau files (mkv) that use the GPU to process

[extension.mkv]
profile-desc=”Profile for Matroska files”
profile=lang
vo=vdpau
vc=ffh264vdpau

For HD Files (mp4)

[extension.mp4]
profile-desc=”Profile for HD mp4 files”
vo=gl
vfm=ffmpeg
lavdopts=lowres=0:fast=1:skiploopfilter=all
autosync=30
cache=32768

For “normal” (avi) files

[extension.avi]
profile-desc=”Profile for deinterlacing avi files”
vo=gl
vf=pp=lb/hb/vb/dr

The default section looks like this:

[default]
vo=vdpau,xv,gl
ontop=yes
double=yes
dr=yes
framedrop=yes
cache=8192
lirc=no
alang=en
slang=en

I have to make sure I check encoded options and rename the extension accordingly, but 95% of the time, this configuration works fine for me

Here is the entire ~/.mplayer/config file:

[deinterlace]
profile-desc=”Profile for picture de-interlacing”
vf-add=pp=fd

[dvd]
profile-desc=”Profile for DVD playback”
profile=deinterlace
dvd-speed=4
cache=8192

[lang]
profile-desc=”Profile for language”
alang=en

[protocol.dvd]
profile-desc=”Profile for dvd:// streams”
profile=dvd
profile=lang

[protocol.dvdnav]
profile-desc=”Profile for dvdnav:// streams”
profile=dvd
profile=lang
nocache=yes

[protocol.cdda]
profile-desc=”Profile for cdda:// streams”
cdda=speed=2

[protocol.tv]
profile-desc=”Profile for tv:// streams”
profile=deinterlace

[extension.mkv]
profile-desc=”Profile for Matroska files”
profile=lang
vo=vdpau
vc=ffh264vdpau

[extension.mp4]
profile-desc=”Profile for HD mp4 files”
vo=gl
vfm=ffmpeg
lavdopts=lowres=0:fast=1:skiploopfilter=all
autosync=30
cache=32768

[extension.avi]
profile-desc=”Profile for deinterlacing avi files”
vo=gl
vf=pp=lb/hb/vb/dr

[default]
vo=vdpau,xv,gl
ontop=yes
double=yes
dr=yes
framedrop=yes
cache=8192
lirc=no
alang=en
slang=en

Mplayer - neat and tidy on the CLI

Wednesday, November 18th, 2009

How long have I been using mplayer? Years. Why has it never bothered me to sort this out before? Don’t know. Ageism, autism, or general grumpiness sent me off in search of a solution, and how easy was it.

Every time I have run mplayer from the cli I got three lines written out before the “action”:

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.

The very simple fix to this is to add an entry to your ~/.mplayer/config file

lirc=no

That’s it, no more extra lines. Remember to comment out “lirc=no” if you want to use a remote control, though :)

GeeXBoX playback at fullscreen on LCD TV

Sunday, August 30th, 2009

This has been an issue for me for quite a while, but I have simply overcome it by using the menu to change the aspect ratio.

It all stems from the perplexing confusion of recording dvb tv for later playback. Watching live TV on the TV, the picture fills the screen. It’s a 16:9 42″ TV (720p). When I record dvb tv using my main pc in its raw .ts state it is 720×576 (16:9). If I play this back using the GeexBox, mplayer squeezes the picture to a display of something like Cinemascope, so a change of aspect ratio to 4:3 usually sorts this out. But it got me thinking, if the original video is 16:9 why doesn’t play as such? The Pc in use to run GeexBox has an nvidia 6200 and is connected to the TV using VGA (DVI/HDMI went “pop” a while ago on the TV!).

First off, I tried all sorts of encoding options, changing the aspect ratio and resolution using mencoder. I got some success if I encoded with aspect=4/3 as an option. but this still didn’t make sense; if I had a 16:9 aspect ratio TV, why the original video didn’t fit.

This is what I did to sort things out. I have a HDD install of GeexBox so can ftp in to edit files. If you use a live cd, then you will need to edit the files before you generate a custom iso.

Boot up the GeexBox

FTP in, and make doubly sure you get into the right place

/mnt/GeexBox-partition/GEEXBOX/etc

Open up tvout in that directory and change the Aspect Ratio line to this:

TVOUT_ASPECT=”16:9″

and save.
By just making this change alone, it squeezed my display horizontally to make a 4:3 image, so more to do

Browse to /mnt/Geexbox-partition/GEEXBOX/etc/mplayer and open up mplayer.conf
In the [default] section, add the following two lines:

monitoraspect=”16:9″

aspect=”16:9″

and save. It may be worth noting that I have vo=vidix,vesa in the default section of mplayer.conf

Log out of the ftp and reboot the GeexBox. The Main Menu and background.avi should be filling the screen.

Now try playing any recorded dvb tv or video that was previously letterboxed, even though it was 16:9 ratio or 720×576 resolution. You should see it playback in full screen.

Caveats: This may not work in your setup, and you may not need to make all the adjustments to get this to work, this is what worked for me.

ts to avi using projectx and mencoder + ffmpeg

Wednesday, July 8th, 2009

I am feeling pretty pleased with myself tonight, having resolved another issue with my encoding of dvb-t streams to avi.

Having sorted out the crashing problem, I was now having a/v sync issues. This is easy enough to deal with by using the +/- keys to adjust a/v sync with mplayer, but annoying for other users.

I have solved the problem by demuxing the stream to its component video and audio parts, using Projectx, Mencoder and ffmpeg.

Projectx is a great java tool that allows you to crop and cut a dvb or mpeg stream and then output either to a ts/ps/m2p a/v stream or to demux the stream. By demuxing you end up with a video m2v file and an audio mp2 file.

It is easy enough to then encode the video using mencoder, then to use ffmpeg to encode the audio, and finally to use mencoder again to mux the video and audio encodes back together, which gives a perfect a/v sync. I put together a script that will encode all m2v/mp2 collections in a directory, so I can leave it running overnight if I have more than one encode to do. Here it is: Mencoder Batch Mux Script

I am using default mencoder libraries and encoders with a two pass approach, but will give h264 a crack later to see if I get better quality for the time and cpu cycles taken. The current script produces eminently watchable video on my 42″ LCD using GeeXBoX so I’m a happy bunny :)

If you get stuck or need help, give me a shout….