Friday, 1 November 2013

Converting images (Jpeg, png, etc) to PDF on linux

Pop up a terminal.
$ sudo apt-get install imagemagick
$ convert img1 [img2...] output.pdf

If your files are already in alphabetical order, bash can automatically expand:

$ convert *.jpg output.pdf
If you want to limit the target file size, play around with -quality VALUE:
$ convert -quality 30 *.jpg output.pdf
VoilĂ !

Wednesday, 29 February 2012

Concatenating video files

A simple way to put two video files together:

mencoder -oac copy -ovc copy -o output.mkv input1.avi input2.avi

Of course, you can change the extension of output to your preferred container, like m4a. The input can also be anything that mencoder can read, depending on your codecs. To give more details, "-oac copy" means that the output audio will be copied, so not re-encoded. I guess you can easily figure out what "-ovc copy" means then. You may also want to add the -idx option to allow seeking:

mencoder -idx -oac copy -ovc copy -o output.mkv input1.avi input2.avi

Note that you can have an arbitrary number of input files, just list them one after the other. On Ubuntu, mencoder can be installed with sudo apt-get install mencoder, but you already knew that...

Sunday, 29 January 2012

Converting Flac to Mp3 (or other formats)

Sometimes you need to have .mp3 files for compatibility reasons (mp3 player?). Here's an easy way to convert Flac (or any other formats readable by ffmpeg, m4a, wav, whatever else) to Mp3 on Linux (or windows using Cygwin or ffmpeg for windows... which is hard to find). First, install lame codecs and ffmpeg
ubuntu: sudo apt-get install lame ffmpeg libavcodec-extra-53 
Then cd to the directory with flac files and use find (or change ffmpeg for avconv):
find -name "*.flac" -exec ffmpeg -i {} -acodec libmp3lame -ab 128k {}.mp3 \;
Done! You now have the exact same filenames with .mp3 encoding (also adds .mp3 at the end but keeps the .flac as well).
Enjoy.
Notes:
  1. Obviously, change *.flac to whatever extension your input files are in
  2. You can also change the output codec by replacing libmp3lame to whatever codec you'd like
  3. Heck, just use man find ;)
  4. If you don't like the .flac.mp3 thing, you can rename easily (I like it since it tells me it came from a lossless source, which is likely sitting somewhere on my hard drive):
    rename 's/\.flac//' *.mp3

Alternative without ffmpeg

If you don't have ffmpeg and prefer using lame and flac directly, first install the codecs if you dont have them already.
Ubuntu: sudo apt-get install flac lame 
Arch: sudo pacman -Syu flac lame
Then, convert away!
find . -name '*.flac' -exec sh -c 'flac -cd "{}" | lame - "{}".mp3' \;
You can also do this in the current folder only with a for-loop:
for f in *.flac; do flac -cd "${f}" | lame - "${f}.mp3"; done;
Set the target bitrate depending on the quality/size you prefer with the -b option of lame:
lame -b 320 - "${f}.mp3"

Tuesday, 22 November 2011

Template inheritance (Jinja2 Python Pyramid)

When creating a website, it's not uncommon to have a base layout for some or all of your pages. In programming, it is a very bad practice to copy that layout for every pages and simply change the content. What happens if you want to modify your base layout? You'd have to perform the same modification to every single pages. In order to solve this, most templating engines support inheritance (or you can mimic it using some other feature). My personal favorite for python is Jinja2. With this templating engine, you can use the following syntax.
{%block NAME%}
content...
{%endblock%}
This defines a "block" with the default content "content...". You can then extend the base file and override the blocks that you want:
{%extends "mytemplate.jinja2"%}
{%block NAME%}
Overridden content
{%endblock}
The content of this template would then be "Overridden content". Now some of you might see the issue here. When using Jinja2 for example, you have to pass a dictionary to the engine so that it can substitutes the variable with the ones in the dictionary. Your page may inherit from your main layout but every variable still needs to be passed from the code. This can become quite messy. This is where the python Decorators can come handy. You first define your main layout function, which will fetch and return all the appropriate data in a dictionary and make a decorator out of it:
def homeLayout(orig):
   def f(request):
      d = orig(req)
      return dict({'menu':getMenu(), 'maincss':static_url('myproj:static/main.css')}), **d)
   return f
This bit of code basically defines a decorator @homeLayout which will take the dictionary returned from the webmethod and add all the variables necessary for the home layout (by merging the two dicts). Now any webmethod decorated with @homeLayout will have the data required for the inherited layout.

Monday, 21 November 2011

Decorators in Python (@ annotations)

In Java, there is a feature called annotations which allows the developer to add metadata to source code (members variables, methods, classes, etc). This metadata is written using the @annotation syntax and can then be read using reflection. Python has a similar syntax that can be used. However, it's not called annotations and it is not really the same thing. In Java for example, this only adds a piece of metadata to the annotated code, whereas in Python we call this syntax Decorators. They are named appropriately because it essentially is the decorator pattern. Here are a few examples of built-in decorators in Python:
@staticmethod
@classmethod
@property
The really cool thing about python's decorator is to create your own custom ones. Basically, when you decorate a function or a class, this is what happens behind the scene:
@myDecorator
def myfunction():
   return "Hello"
The decorator is read by the parser and this is what it's translated to:
myfunction = myDecorator(myfunction)
Therefore, you can do whatever you'd like with this annotated function! The "@" really is only syntactic sugar, it doesn't add anything more than calling the method directly and assigning to the decorated name. However, the code becomes clearer and more readable by using the @decorator syntax. A decorator can be any callable, it will simply be called with the decorated name (class, method, etc). An example here would be to add "world" to the returned string:
def myDecorator(decorated):
   def f():
      return decorated() + " world"
   return f
There you go, myfunction now returns "Hello world".

Update:
You can also pass arguments to the decorator, i.e.:
@mydecorator(arg1, arg2)
The best way to understand this is to know what happens behind the scene:
myfunction = mydecorator(arg1, arg2)(myfunction)
Pretty straightforward isn't it? The decorator is called with the arguments, the return value is then called with the function (or class or whatever) to be decorated. You would implement a decorator that takes arguments as such:
def myDecorator(a1, a2):
   def wrapper(f):
      def decorated():
         return f() + a1 + a2
      return decorated
   return wrapper
Essentially, decorating the "myfunction()" above (the one that returns "Hello" with this decorator as such:
@myDecorator(" World", " Weee!")
def myfunction():
   return "Hello"
would now return "Hello World Weee!". The decorator function takes arguments and returns a function that will be called with the "thing" to be decorated. Because of closure, the arguments can be applied inside the second call and return the decorated function. You can also do it with a class (may be easier to understand):
class myDecorator(object):
   def __init__(self, a1, a2):
      self.a1 = a1
      self.a2 = a2
   def __call__(self, f):
      def decorated():
         return f() + self.a1 + self.a2
      return decorated
remember how it's done under the hood!
myfunction = myDecorator(a1,a2)(myfunction)

Friday, 4 November 2011

Ubuntu: Daemon is inhibited

In the last few weeks, I've found myself wondering why nothing in the file manager and Disk Utility would work (i.e. for mounting drives). The error I was getting wasn't so helpful "Daemon is inhibited". A quick Google search revealed nothing very useful (other than killing all udisks which isn't really reasonable). In the end, the solution is way simpler: Close GParted.

Simply put, GParted runs as root and will call:
udisks --inhibit
Since Disk Utility does not run as root, when it tries to mount a partition for example, udisks will return an "Inhibited" error. You could run Disk Utility as root as well, however, mounting partitions will mount as root, which most likely isn't what you're looking for.