HowTos


Windows with ffmpeg — recursively convert all *.mov movies to .mp4. with fixed resolution for android 1

We needed to convert a bunch of mov files to mp4 and while doing that we wanted to shrink them down so that they would fit the screen of an older android device.
We did that both to save space on the internal memory and to make the device perform as efficient as possible as it would not have to shrink the video on the fly.

We downloaded the windows binary for ffmpeg from https://ffmpeg.org/ and copied it to the folder we wanted to execute the command from using Windows Explorer.
After that, while holding the Shift key we right clicked in the Windows Explorer empty area to popup the menu. From the menu we selected Open command window here, that opened a Command Prompt that was already navigated in the folder we placed the binary.
To convert the movies we executed the following:

for /R %f in ("*.mov") do (ffmpeg.exe -i "%~f" -s 864x486 -acodec copy "%~pf%~nf.mp4")

What the above command did was, direct command prompt to find recursively all the files that their name ends in .mov (this is the part that looks like this for /R %f in ("*.mov")) and then execute for each a command, in our case was to convert the file to mp4, resize the video while preserving the audio as is and produce a new file that has the same name but different file extension so that new files will have the mp4 extension instead of mov.


HOWTO: Make Terminator Terminal Act Like Guake Terminal in Fedora 23 1

We tried to toggle the visibility of the terminator window using the default keybinding which is (Shift+Ctrl+Alt+A) and failed. Changing the configuration in the ‘Terminator Preferences’ under Keybindings to a new key-bind also did not do any good. We could not get the hide_window keybinding to work and so we could not toggle the window visibility with the keyboard.

We propose this alternative solution that requires two additional packages: xdotool and wmctrl.

In Fedora you can install them using sudo dnf install xdotool wmctrl and in Ubuntu using sudo apt-get install xdotool wmctrl

After the installation is complete, you need to paste the following code in a file and make it an executable.

e.g From a terminal issue nano ~/toggle_visibility.sh, then paste the code and hit CTRL+X to exit. When prompted if you want to save press ‘Y’ and enter.

#!/bin/bash

#The purpose of this script is to allow the user to toggle the visibility of (almost) any window.
#Please note it will work on the first match, so if there are multiple instances of an application it would be a random window of them the one to be affected.
#Usually it will control the window with the smallest PID.

#Checking that all dependencies are met, since we cannot proceed without them.
declare -a DEPENDENCIES=("xdotool" "wmctrl");
declare -a MANAGERS=("dnf" "apt-get");

for DEPENDENCY in ${DEPENDENCIES[@]}; do
    echo -n "Checking if $DEPENDENCY is available";
    if hash $DEPENDENCY 2>/dev/null; then
        echo "- OK, Found";
    else
        echo "- ERROR, Not Found in $PATH";
        for MANAGER in ${MANAGERS[@]}; do
            if hash $MANAGER 2>/dev/null; then
                echo -n "$DEPENDENCY is missing, would you like to try and install it via $MANAGER now? [Y/N] (default is Y): ";
                read ANSWER;
                if [[ "$ANSWER" == "Y" || "$ANSWER" == "y" || "$ANSWER" == "" ]]; then
                    sudo "$MANAGER" install "$DEPENDENCY";
                else
                    echo "Terminating";
                    exit -1;
                fi
            fi
        done
    fi
done

APPLICATION="$1";

#Checking if the application name provided by the user exists
if ! hash $APPLICATION 2>/dev/null; then
    echo -e "$APPLICATION does not seem to be a valid executable\nTerminating";
    exit -2;
fi

#Checking if the application is running. We are using pgrep as various application are python scripts and we will not be able to find them using pidof. pgrep will look through the currently running processes and list the process IDs of all the processes that are called $APPLICATION.
PID=$(pgrep --exact $APPLICATION | head -n 1);

#If the application is not running, we will try to launch it.
if [ -z $PID ]; then
  echo "$APPLICATION not running, launching it..";
    $APPLICATION;
else
    #Since the application has a live instance, we can proceed with the rest of the code.
    #We will get the PID of the application that is currently focused, if it is not the application we passed as parameter we will change the focus to that. In the other case, we will minimize the application.
  echo -n "$APPLICATION instance found - ";
    FOCUSED=$(xdotool getactivewindow getwindowpid);
    if [[ $PID == $FOCUSED ]]; then
    echo "It was focused so we are minimizing it";
        #We minimize the active window which we know in this case that it is the application we passed as parameter.
        xdotool getactivewindow windowminimize;
    else
    echo "We are setting the focus on it";
        #We set the focus to the application we passed as parameter. If it is minimized it will be raised as well.
        wmctrl -x -R $APPLICATION;
    fi
fi

exit 0

Afterwards, you need to make the script an executable so you should issue chmod +x ~/toggle_visibility.sh to do that.

Then, execute ~/toggle_visibility.sh in your terminal once. We need to do that in order to install any missing dependencies for the tool.

Finally, you need to create a custom shortcut that will call the script using the key combination you like at any point.

For Fedora,

  1. Issue the following in a terminal gnome-control-panel to start the gnome control panel.
  2. In the newly appeared window, click on the ‘keyboard’ icon that is in the category ‘Hardware’.
  3. After that, click on the tab ‘Shortcuts’
  4. and on the left list, click on custom shortcuts.
  5. You will see a button with the + sign, click that.
  6. In the dialog box that will appear enter the following:
    – In the name field enter anything you like. e.g ‘Toggle Terminator Visibility’
    – In the command field enter ‘/home/<USER>/toggle_visibility.sh terminator’ where user enter your own username.
    – Click apply.
  7. You will see a new row with two columns with the name you just set in the first column. Click on the second column, where it should say ‘Disabled’ and the press the key combination you want for toggling terminator e.g F12

For Ubuntu, go to System Settings and follow the same procedure after step 2.

You are ready to go 🙂

Just try the key combination you just provided and terminator will appear in front of you. Pressing it once more it will hide it.


[JavaScript] Get values from URL parameter

The following code will get the query parameters of a URL and assign them to an object called QueryMap.

// This function is anonymous, is executed immediately and the return value is assigned to QueryMap.
var QueryMap = function () {
	var query_map = {};
	//The search property sets or returns the querystring part of a URL, including the question mark (?).
	//So we remove the question mark by removing the first characted of the string.
	var query = window.location.search.substring(1);
	var variables = query.split('&');
	for (var i = 0; i < variables.length; i++) {
		var position = variables[i].indexOf('=');
		var key = variables[i].substring(0, position);
		var value = variables[i].substring(position + 1);
		// If it is the first entry with this name
		if (typeof query_map[key] === "undefined") {
			query_map[key] = decodeURIComponent(value);
			// If it is the second entry with this name we change the value to an array of values
		}
		// If it is the second entry with this name we change the value to an array of values
		else if (typeof query_map[key] === "string") {
			var array = [query_map[key], decodeURIComponent(value)];
			query_map[key] = array;
		}
		// If it is the third or later entry with this name we just add to the array
		else {
			query_map[key].push(decodeURIComponent(value));
		}
	}
	return query_map;
}();

This code will handle cases where the equals sing (=) is part of the value of a variable.

Examples:

If the URL is http://example.com?a=1
Then QueryMap.a will contain value “1”.

If the URL is http://example.com?a=1&b=2
Then QueryMap.a will contain value “1” and QueryMap.b will contain value “2”.

If the URL is http://example.com?a=1=0001&b=2
Then QueryMap.a will contain value “1=0001” and QueryMap.b will contain value “2”.

If the URL is http://example.com?a=1=0001&b=2&b=0010
Then QueryMap.a will contain value “1=0001” and QueryMap.b will contain an array with the values “2” and “0010”.


[GitLab.com] Get a list with the names of all repositories in your account

GitLab.com offers a public API that allows us to get information related to our accounts. One of the API calls available is the account projects call (http://gitlab.com/api/v3/projects).

This call will return a JSON object describing the projects available to your account.

To get a list of the names of the projects available to you, you can use the following:

TOKEN="PASTE_YOUR_PRIVATE_TOKEN_HERE"; PREFIX="ssh_url_to_repo"; curl --header "PRIVATE-TOKEN: $TOKEN" http://gitlab.com/api/v3/projects | grep -o "\"$PREFIX\":[^ ,]\+" | xargs -L1 basename | awk -F '.' '{print $1}'

The above code will bring the JSON object, filter out everything except for the “ssh_url_to_repo” member of each project and then it will print it out on screen.

 

To get the above code working: the GitLab API requires that you use a token that is related to your account instead of using your credentials to make the call to the API.

To get your private token, visit this page http://gitlab.com/profile/account , the private token is the random sequence of characters in the white box:

[GitLab.com] Private TokenYou need to copy that value in the place of the variable TOKEN in the above script.

 

In case you have a lot of projects (more than 10), the default call will only produce the results for the first 10 repositories only.

To list all available repositories you have two options:

  1.  Set the per_page query parameter to a value big enough to fetch all your projects information if they are less than 100. e.g http://gitlab.com/api/v3/projects?per_page=100
  2. Follow the link headers from the initial response to make all the next calls.