Ryan Harrison My blog, portfolio and technology related ramblings

FFmpeg - Convert Video to MP3

FFmpeg can easily be used to extract the audio tracks from virtually all video files and save them to new audio files:

Copy audio track from video file:

This example assumes that the video’s audio track is already an MP3 so can be simply copied over without re-encoding.

ffmpeg - i video.mkv -acodec copy audio.mp3

Convert audio track to MP3 (CBR):

This example assumes that the video’s audio track is something other than MP3 (or whatever target format you want). FFmpeg will re-encode the audio track so you can also specify some additional quality options.

ffmpeg -i video.mkv -b:a 192K -vn audio.mp3

In this command we specify the that the bit rate of the new audio file will be a constant 192kb/s (CBR).

Convert audio track to variable bit rate MP3 (VBR)

This command creates a variable bit rate (VBR) MP3 instead of the CBR file as in the above example. This will reduce the size of the new MP3, but may sacrifice some quality.

ffmpeg -i video.mkv -q:a 0 -map a audio.mp3

Read More

Ubuntu Server Setup Part 1 - Logging In

In this set of posts I’ll be running through setting up a new Ubuntu server - from initial setup and security through to installation of some of the most common packages. You can get your very own server/VPS through companies such as DigitalOcean or OVH - in this case we’ll be using Ubuntu Server 14.04, but the same concepts should apply to previous and future releases as well.

In this post we’ll be covering how to login to your server for the first time through SSH and creating a new user with root privileges.

Logging in as Root

When you create your new server, you should receive an email with directions on how to login to your server. This will normally include the public IP address of your server and a randomly generated password which will allow you to login as the main root user. The root user is the main administrator in a Linux system and so has privileges to do pretty much anything. You therefore generally don’t want to be logged in as root for day-to-day operations where you can potentially cause damage quite easily. We do however need to login as root initially (as at this point we don’t have any other users).

In this post we’ll be using PuTTY as an SSH client, however all other alternatives will work just as well. Inside your initial email you should have the public IP of your server along with the root password. In the PuTTY client window enter the IP address into the Host Name field and press ‘Open’ (assuming SSH is selected and the port is set to the default 22).

PuTTY

You should be greeted with a black window prompting you for the user your wish to login as - in this case ‘root’. Enter the root password when prompted. As this is the first time you are logging in, you will also be prompted to change the default root password (make sure to choose something strong).

PuTTY Login Screen

If you see a new bash shell, then you have successfully logged in to your server and you can move on to setting up your own new user.

PuTTY Bash Shell

Creating a New User

As I said before, working as root is generally considered a bad idea. Instead, we will be creating a new user which we will be logging in as in all future sessions.

$ adduser demo

This command will create a new user called ‘demo’ (change the name to be something more relevant). You will be prompted for a new password (again choose a strong one) along with some general information for the new user (you can skip this be pressing Enter).

Granting Root Privileges

We have just created a new user, but by default they have very limited privileges which prevents them from installing new packages or modifying any system files. Obviously for us this is not ideal, so instead we can grant the new user ‘sudo’ privileges, which allows us to run commands as root without having to actually be logged in as root. To grant these privileges, run the following as root:

$ gpasswd -a demo sudo

You can now run commands as root by simply adding sudo to the beginning - essentially meaning that you never need to login as root anymore. For additional security we can also completely disable root login through ssh (which stops a lot of brute force attacks) which will be covered in a future post.

Now we have a new user setup, we still need to do some more work to better secure our server. In the next post we will set up public key authentication which is a much more secure method than the simple password we have at the moment.

Read More

Bash - Redirecting stdout and stderr

Redirect stdout/stderr to a truncated file:

# StdOut
cmd > out.txt # (stderr printed to console)
# StdErr
cmd 2> err.txt # (stdout printed to console)

Redirect stdout/stderr to a file (appending):

# StdOut
cmd >> out.txt # (stderr printed to console)
# StdErr
cmd 2>> err.txt # (stdout printed to console)

Redirect both stdout and stderr in same command to different files (truncating):

cmd > out.txt 2> err.txt

Redirect both stdout and stderr to same output (truncating):

cmd > out.txt 2>&1

“Redirection statements are evaluated, as always, from left to right. >> file - STDOUT to file (append mode) (short for 1>> file) 2>&1 - STDERR to ‘where stdout goes’ Note that the interpretation ‘redirect STDERR to STDOUT’ is wrong”

Source

Read More

Htop Colours Explained

Htop is a great expansion to the top command in Unix based systems. Rather than just spitting out metrics to the console, htop provides a complete ncurses-based interface which makes it extremely useful when managing processes.

The header of htop contains a number of bars that show CPU usage, memory usage and swap usage. The bars are all colour coded to show different aspects of the usage. This is great when you know the what the colours mean, but htop doesn’t make it too obvious what they mean. The F1 (Help) menu offers a guide to the colours of the usage bars:

htop colour guide

Read More

Python - Simple HTTP Server

Python comes with a really handy built-in HTTP server that you can get up and running in a matter of seconds - and without having to mess around with something like Apache. This is great for serving simple web pages locally through HTTP to get around the limitations of the file:// protocol within the browser.

The command to start the server unsurprisingly differs from Python 2 to Python 3, but either way it’s really easy to remember. Simply open up a terminal, change directory to the folder containing the files you want to serve and either of the following to start the server depending on your Python version:

Python 2:

cd files/my-directory
python -m SimpleHTTPServer

Serving HTTP on 0.0.0.0 port 8000 ...

Python 3:

cd files/my-directory
python -m http.server

Serving HTTP on 0.0.0.0 port 8000 ...

Then navigate to http://localhost:8000 in your browser to see your files (by default you get a directory listing if there is no index.html file).

You can also specify the port you want to use if 8000 doesn’t work for you:

python -m SimpleHTTPServer 8080
python -m http.server 8080

Serving HTTP on 0.0.0.0 port 8080 ...
Read More