Code for Concinnity

beautiful and elegant solutions


Iterating Bash arrays with spaces

The problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/sh

$ a=(hello world foo bar)
$ for i in #{a[*]}; do echo $i; done

# Expected output:
#   hello
#   world
#   foo
#   bar

# so far so good:
#   hello
#   world
#   foo
#   bar

$ a=("hello world" "foo bar")
$ for i in #{a[*]}; do echo $i; done

# Expected output:
#   hello world
#   foo bar

# omg:
#   hello
#   world
#   foo
#   bar

The problem is caused by the affect that Bash uses space as array element separator internally.

Failed attempt

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh

$ a=("hello world" "foo bar")
$ for i in "#{a[*]}"; do echo $i; done

# Expected output:
#   hello world
#   foo bar

# zomg!
#   hello world foo bar

The solution

The solution lies in the magical $@ expansion. When the $@ expansion is put in a quote, the shell automatically expands each element properly quoted:

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh

a=("hello world" "foo bar")
for i in "#{a

# Expected output:
#   hello world
#   foo bar

# yay!
#   hello world
#   foo bar
Published by kizzx2, on April 10th, 2010 at 1:27 am. Filled under: Useful tips Tags: , , , No Comments

Muxing audio and video with ffmpeg

ffmpeg is a great codec converter, but it’s wide array of options is really daunting. I just figured out it can also be used to mux video and audio together into a file, here’s how to do it

1
2
3
4
5
6
# Simple example: mux an audio with a video file without audio track
$ ffmpeg -i audio.mp3 -i video.avi -acodec copy -vcodec copy output.avi

# Daily usage example: mux an audio with a video file _with_ an existing audio track.
# This will replace the AVI file's audio track with the MP3
$ ffmpeg -i audio.mp3 -i video-with-audio.avi -acodec copy -vcodec copy output.avi -map 0.0 -map 1.0

The key to the second command is the -map parameter. Typically, the output file would contain two streams: one audio and one video. The numbers 0.0 and 1.0 refers to the first input file and the second input file respectively.

The -map parameter is used to spell out the streams. What the above said was “use input stream 0.0 for your first output stream (which is an audio stream) and input stream 1.0 for your second output stream (which is the video stream)”.

You can see a list of stream numbers by

1
$ ffmpeg -i audio.mp3 -i video-with-audio.avi

You can add additional streams to the output file with the -newaudio and -newvideo parameters. Two audio streams make sense when you’re making a DVD rip with two sound tracks. I didn’t try it myself but it’s nice to know :P

Cheers :)

Published by kizzx2, on April 8th, 2010 at 6:10 pm. Filled under: Useful tips Tags: , , No Comments

Vrapper: open source vim plugin for eclipse

Found out Vrapper recently. The fact that it is open source deserves a lot of merits. For years eclipse users only had the commercial option ViPlugin which didn’t work quite well for something that charges money, considering the NetBeans folks have the excellent jVi which is also open source.

Considering eclipse is free and open source, Vrapper only seems more fitting than ViPlugin. I’ve tried it for a week and it seems most essential functions are there, and I’ve been using vi for a couple of years so I think my set of “essential functions” shouldn’t be too narrow :p

Published by kizzx2, on April 8th, 2010 at 5:49 pm. Filled under: Useful tips Tags: , , , , , No Comments

Installing HuffYUV on Windows 7 x64

This will probably also apply to other INF based installation for that matter. A simple command line saved me hours of trouble:

rundll32 C:\Windows\SysWOW64\setupapi.dll,InstallHinfSection DefaultInstall 0 C:\Path\To\huffyuv.inf

Run the above with admin privilege and bam, HuffYUV correctly appears in my codec list.

P.S. If you’re looking for a lossless codec, maybe take a look at Lagarith. It claims to be an improvement to HuffYUV and supports multi-processor encoding. It also comes with a dedicated installer so there’s no need to tinker with the command line just to install the codec.

Published by kizzx2, on April 5th, 2010 at 6:18 pm. Filled under: Useful tips Tags: , , , , 4 Comments

cmd-recycle: Delete files from Windows command line

Download links

I wrote another tool to do just this a while ago, but that one stopped working since I migrated to Windows 7 x64. Microsoft said the SHFileOpearation interface (which the old tool used) had been replaced by the IFileOperation interface. So I figured I would dig up my rusty C# again to update it.

Damn, I can’t believe it took a couple of hours, but the result worked amazingly. This one also supports wildcards:

1
recycle file1 file2 supports-wildcards\*.tmp

I’ve gone the extra mile and set up a proper repository this time around on GitHub. Probably some Windows gurus will mock me left and right with some obscure one-liners or something, lol.

Published by kizzx2, on January 6th, 2010 at 3:30 am. Filled under: Useful tips Tags: , , No Comments

Starting Windows’ Network and Sharing Center from command prompt

It has always looked not-so-cool to have to type “Sharing” at the start menu or worse yet, navigate through the Control Panel just to start it. Today I finally dug out how to do it from TechNet:

1
control /name Microsoft.NetworkAndSharingCenter

Looks like Microsoft decided to go down the verbose path. Maybe I’ll just stick with the old ways :p

Published by kizzx2, on December 29th, 2009 at 9:21 pm. Filled under: Useful tips Tags: , , , No Comments

Getting the currently running host name in Capistrano

I don’t know if it is so obvious that no people talked about it, or that people actually don’t need to use it. I find that for any non-trivial tasks, it is necessary. There is a thread on Google Groups that talked about it but it didn’t spell out the answer directly. So here it is:

1
current_host = capture("echo $CAPISTRANO:HOST$").strip

Kind of anti-climatic lol.

Published by kizzx2, on November 29th, 2009 at 3:31 pm. Filled under: Useful tips Tags: , No Comments

gvim — E303: Unable to open swap file for “[No Name]“, recovery impossible

vim is my editor of choice and I install it in Windows. All is working quite good except one little annoyance — when I open the editor fresh and started editing by going into insert mode, it will give this error message:

1
E303: Unable to open swap file for "[No Name]", recovery impossible

This is usually harmless, as soon as I save the file everything would be great, it’s just that 1 – 2 seconds of pause that doesn’t make me feel good, so today I tried to pin down the problem and here’s how it went:

1
2
3
4
5
6
7
:help E303
(So it's related to the swap file. Let's see where our swap file fails to be created.)
:set directory?
directory=.;c:\tmp;c:\temp
(Oh, those are the default %TEMP% which I moved to another drive! But anyway, the first priority should be the current directory. So let's see where we're now)
:pwd
C:\Windows\system32

Solution

OK it’s pretty obvious now. It probably needs a little bit of manual fixing. Let’s put this in our .vimrc

1
2
3
4
5
" Thanks for an anonymous guest to provide this generic, better solution
set directory=.,$TEMP

" This is line I used at first. The above line is better than this

set directory=.,d:\temp

Great, problem solved!

Published by kizzx2, on November 6th, 2009 at 3:58 am. Filled under: Useful tips Tags: , , , 6 Comments