Monday, April 14, 2014

Expanding an Existing VirtualBox Disk

Issue

You have spent ages installing an operating system onto a VirtualBox virtual disk, but now find that after updates and software installations that most of the disk space has already been used. 

What to do?

Reinstall on a new larger VirtualBox virtual disk?
No!

Fortunately existing VirtualBox virtual disks can be resized with VBoxManage and then GParted can be used to resize the existing partition to fill up the extra space created by VBoxManage from within the virtual machine.

This page describes a simple procedure to use to increase the storage capacity of an existing VirtualBox virtual disk.

Procedure

This example procedure demonstrates how to increase the capacity of an existing virtual disk used in a Windows Virtual Machine.

Ideally you want your existing VirtualBox virtual disk to be in vdi format, but even it if it is in another format (vmdk in this example) its capacity can still be expanded by simply cloning the disk from vmdk to vdi format. A vmdk disk simply means an additional step (Step 1) not needed for a vdi disk.

Step 1
Skip this step if your virtual disk is already in vdi format.

VBoxManage clonehd Input.vmdk Output.vdi --format vdi

Step 2
VBoxManage modifyhd Output.vdi --resize 40960

Note --resize expects the new disk size in MegaBytes, here we show 40960 MB which is equivalent to 40 GB.

Step 3
Remove the original virtual disk from the Virtual Machine and replace it with the newly created (expanded) vdi virtual disk.

Step 4
Download the GParted.iso and then use it as bootable CD in the Virtual Machine.

Step 5
Boot up the Virtual Machine, which will then run GParted, resize the existing partition to fill the now larger disk. Then close down the Virtual Machine.

Step 6
Remove the GParted.iso from the Virtual Machine so that it will boot up normally.

Boot the Virtual Machine. Assuming it is a Windows Virtual Machine then Windows will give an error message.

Don't do anything!

Let Windows automatically update the virtual disk to the new size.

Let Windows make the necessary corrections.

Once Windows boots up you should have an expanded virtual disk that fully utilises the extra space you created in Step 2.

Finished!

Well Done!

======================================================

Further Reading

How-to-Geek : How To Enlarge a Virtual Machine’s Disk in VirtualBox or VMware

How to resize a VirtualBox vmdk file

Resize your /dev/sda1 disk of your Vagrant / VirtualBox VM

How to increase hard disk size after installing a guest OS?

======================================================


Converting Videos for the iPhone 5s

Overview

This page describes how to use the free software HandBrake to convert video files into a format suitable for playing on the iPhone 5s and iPod Touch.

HandBrake comes in two versions:
  1. Graphical user interface version (HandBrake), suitable for converting a small number of video files manually
  2. Command line version (HandBrakeCLI) that is best suited for converting multiple files automatically via a Bash shell script.
The operating system used in these examples is a version of Linux Ubuntu (Mint KDE Nadia). HandBrake runs on Mac and Windows as well as Linux. For multiple video conversions a simple Bash shell script is provided that uses HandBrakeCLI.

Two Bash shell scrips are provided:

(a) General script for converting any videos into .mp4
(b) Specific script that only converts .mp4 files.

Introduction

Long before I purchased an Apple iPhone 5s, I knew that my favourite video container (MKV) was not directly supported on Apple hardware. While it is possible to install Apps that will play MKV video files I wanted to be able to use the iPhone's native media software. It was obvious that I would need to find a reliable and easy method to convert multiple video files into the correct video format for an iPhone.

A quick look at the Apple 5s technical specifications shows that the following video formats are supported by the iPhone 5s:

(i) H.264 video up to 1080p, 60 frames per second, High Profile level 4.2 with AAC-LC audio up to 160 Kbps, 48kHz, stereo audio in .m4v, .mp4 and .mov file formats;

(ii) MPEG-4 video up to 2.5 Mbps, 640x480 pixels, 30 frames per second, Simple Profile with AAC-LC audio up to 160 Kbps per channel, 48kHz, stereo audio in .m4v, .mp4 and .mov file formats;

(iii) Motion JPEG (M-JPEG) up to 35 Mbps, 1280x720 pixels, 30 frames per second, audio in ulaw, PCM stereo audio in .avi file format

For space and quality reasons I chose the H.264 video codec, AAC audio codec and mp4 container specification. The key is that not all .mp4 files will play on an iPhone and so must be converted into the iPhone compatible mp4 format via HandBrake / HandBrakeCLI .

HandBrake / HandBrakeCLI provides a preset(--preset="iPhone & iPod touch") that converts videos into a suitable specification that will play on iPhone and iPod Touch devices. So it was simply question of using HandBrake's iPhone and iPod Touch preset.

======================================================

(a) Bash Script for Converting any videos into iPhone .mp4 format

#!/bin/bash
clear
clear
echo  "Start of $0"
echo
## This is to stop Bash breaking on Spaces and force it to break at the End of each Line.
IFS=$'\n'

count=0
# Change this input directory to suit you if you are not already in the source media directory.

# cd "/WinF/Inputs"

for i in *.*
do
echo "Starting $i"
echo
#j=$(basename $i .*)

j=${i%.*}

echo -e "$i WITHOUT original EXTENSION is \n$j"
echo


echo -e "New filename without old Extension is :   \n$j"
echo

# Here we output the files into the directory /WinF/HandBrake-Output obviously change this to to suit you.

# Choose a HandBrakeCLI preset from 
# HandBrakeCLI --preset-list

#HandBrakeCLI -i "$i" --preset="iPad" -o /WinF/HandBrake-Outputs/$j.mp4

HandBrakeCLI -i "$i" --preset="iPhone & iPod touch" -o /WinF/HandBrake-Outputs/$j.mp4

# Following example from 

# ffmpeg -i "$i" -ab 256k "${i%m4a}mp3"
let count=$count+1
echo
echo "====================================================================================="
echo
echo "$i done"

echo "Files processed=$count"
sleep 10

echo
echo "====================================================================================="
echo

done
======================================================

(b) Bash Script for Converting .mp4 files into iPhone .mp4 format


#!/bin/bash
clear
clear
echo  "Start of $0"
echo
## This is to stop Bash breaking on Spaces and force it to break at the End of each Line.
IFS=$'\n'

count=0

# Change this input directory to suit you.
cd "/WinF/Inputs"

for i in *.mp4
do
echo "Starting $i"
echo
j=$(basename $i .mp4)

echo -e "$i WITHOUT MP4 EXTENSION is \n$j"
echo

echo -e "New filename with MP4 extension removed is :   \n$j"
echo

# Here we output the files into the directory /WinF/HandBrake-Output obviously change this to to suit you.

#HandBrakeCLI -i "$i" --preset="iPad" -o /WinF/HandBrake-Outputs/$j.mp4

HandBrakeCLI -i "$i" --preset="iPhone & iPod touch" -o /WinF/HandBrake-Outputs/$j.mp4


# Following example from 
# ffmpeg -i "$i" -ab 256k "${i%m4a}mp3"

echo
echo "====================================================================================="
echo

echo "$i done"

echo "Files processed=$count"

let count=$count+1

sleep 10

echo
echo "====================================================================================="
echo

done
======================================================

Further Reading 



HandBrake Downloads

HandBrakeCLI

HandBrake Guide

======================================================


Saturday, March 22, 2014

Converting FLAC to AAC for Apple iPhone .m4a

Converting FLAC to AAC for Apple iPhone

Overview
This page describes how to convert from FLAC to AAC via avconv. The conversion tool avconv is a fork of FFmpeg.

The operating system used is a version of Linux Ubuntu (Mint KDE Nadia). For multiple FLAC to AAC conversions a simple Bash shell script is provided.

During tests savings of almost 70% were achieved when converting 166 files from FLAC to AAC, equivalent to a saving of 3.4 GB (from 4.9 GB to 1.5 GB).

The key avconv FLAC to AAC conversion syntax is 

avconv -i  inputfile.flac -acodec aac -strict experimental -ab 320k -ar 48000 -y outputfile.m4a


Introduction
Having finally purchased an Apple iPhone 5s, it quickly became clear that my favoured lossless audio format of FLAC (or Apple's lossless ALAC) was going to have much larger files sizes than desired.

Although the Apple iPhone 5s 16 GB storage (of which about 12.9 GB is available for user storage) would easily accommodate my 166 favourite songs in either lossless format, it seemed a waste of space given the limited amount of storage, and besides I wanted to store some videos on the Apple 5s as well!

A quick look at the Apple 5s technical specifications confirmed that the Advanced Audio Coding (AAC) codec was the correct lossy audio format to use. 

AAC is the successor to the popular but outdated MP3 format. AAC generally achieves better sound quality than MP3 at similar bit rates. 


Put simply forget MP3, it has already been widely replaced by AAC.

Against the general advice on the internet I went for the highest possible bit rate of 320 Kbps, fully knowing that I could have achieved smaller file sizes with lower bit rates with minimal or modest sound quality loss. I wanted the best sound that AAC would permit if I couldn't use either FLAC or ALAC.

I did consider using a Variable Bit Rate (VBR) but given the experimental nature of the AAC codec natively used in avconv I decided after several tests to play safe and use a Constant Bit Rate (CBR). In doing so I noted that the file sizes with CBR were larger than in VBR (around 50% larger in my simple tests). 

The Bash shell script shown later on this page contains a commented out (#) VBR conversion line that the user can use by uncommenting it (i.e. removing the starting #) and commenting out (i.e. placing # at the start) the CBR conversion line.

If you must use VBR try 

avconv -i  inputfile.flac -acodec aac -strict experimental -aq 31 -ar 48000 -y outputfile.m4a

and adjust the constant quality -aq 31 from the value 31 shown here until you obtain an output that satisfies your personal sound quality / file size tradeoff.


Solution
The key avconv FLAC to AAC conversion syntax is

avconv -i  inputfile.flac -acodec aac -strict experimental -ab 320k -ar 48000 -y outputfile.m4a

replacing inputfile.flac and outputfile.m4a as needed.

The key points to note are:

-acodec aac tells avconv to use the aac audio codec.
Note that multiple alternative codecs exist for converting to AAC, however most of them can't by default be released with avconv. The aac codec used by avconv doesn't have a very good reputation and is still experimental. Previously the libfaac AAC codec was available but it is no longer available by default for licensing reasons.

-strict experimental is needed since the native avconv AAC codec aac is still experimental.

-ab 320k sets a constant bit rate of 320 Kbps, the maximum value permitted under the Apple 5s technical specifications. This can equivalently be written as -b:a 320k

-ar 48000 sets a sample rate of 48,000 Hz and is recommended since some FLAC files can have higher sampling rates not supported by aac.

-y is used to automatically overwrite an existing file with the same name.


Bash Shell Script

Feel free to use this Bash shell script and distribute it to all your friends. I warmly welcome receiving improvements to the script. It is designed to 'get the job done' not be anywhere near perfect.

At the very least you will need to replace the bold red text as it refers to directories that will not exist on your computer.
#!/bin/bash
clear
clear
echo  "Start of $0"
echo
## This is to stop Bash breaking on Spaces and force it to break at the End of each Line.
IFS=$'\n'

count=0

cd  "/WinE/FLAC Master - 166/"

echo "Files processed = $count"

for i in `ls *.flac`
do
echo "Starting $i"
echo
j=$(basename $i .flac)

#Alternatively....
#j="${i%.flac}"
# or to also add the.m4a container extension at the same time...but don't forget to remove .m4a from end of avconv line around line 41.
# j="${i%.flac}.m4a"

echo -e "$i WITHOUT flac EXTENSION is \n$j"
echo

# See http://stackoverflow.com/questions/8467424/echo-new-line-in-bash-prints-literal-n for advice on adding newlines with echo -e

echo -e "New filename with FLAC extension removed is :   \n$j"
echo

echo "Files processed = $count"

let count=$count+1

# Here we output the files into my home directory (~) that has sub-directories of iPhone and AAC obviously change these to suit you.
avconv -i  $i -acodec aac -strict experimental -b:a 320k -ar 48000 -y ~/iPhone/AAC/$j.m4a

# Alternative VBR syntax
#avconv -i  inputfile.flac -acodec aac -strict experimental -aq 31 -ar 48000 -y outputfile.m4a
#avconv -i  $i  -acodec aac -strict experimental -aq 31 -ar 48000 -y ~/iPhone/AAC/$j.m4a

echo "$i done"
echo
echo "====================================================================================="
echo
done


Further Reading

To follow.