Sunday 15 September 2019

Raspberry Pi Apache Web Server Installation

Introduction

This page describes how I setup the web server Apache, specifically Apache2, along with PHP and MariaDB.

Note I use MariaDB in preference to the original MySQL database.

hostname -I
sudo apt-get install apache2 apache2-doc apache2-utils
sudo apt-get install php libapache2-mod-php php-pear -y
sudo apt-get install mariadb-server mariadb-client php-mysql
sudo service apache2 restart

Apache is run via the binary apache2
To start / stop Apache use:

/etc/init.d/apache2
or
apache2ctl

but not /usr/bin/apache2 directly as it will not work on Raspbian Linux as it is Debian based.

sudo apache2ctl stop   
sudo apache2ctl start
sudo apache2ctl restart



Important Files Location

Put your website into:

/var/www/html/

It will be necessary to rename the default Apache index.html file via

mv index.html oldindex.html

You can then copy your website into /var/www/html/

  • Apache 2 Configuration files

          /etc/apache2/apache2.conf
          apache2.conf is the main Apache configuration file.
          This file is used by Debian instead of the more common httpd.conf

If you have PHP installed then you probably want Apache to serve index.php as the default page instead of index.html


To do this :

sudo nano /etc/apache2/mods-enabled/dir.conf

and put index.php before index.html so that it looks like :

<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Note that some Apache configurations put DirectoryIndex in either httpd.conf or apache2.conf

Debian, on which Raspbian is based, uses apache2.conf and dir.conf for some of the configurations.




Further Help


INSTALL APACHE, PHP 7.0 AND MYSQL/MARIADB ON A RASPBERRY PI (LAMP) WITH PHPMYADMIN

How to install a web server on the Raspberry Pi (Apache + PHP + MySQL)

How to setup a Raspberry Pi web server with Raspbian Stretch Lite, Nginx, MariaDB and PHP as the LEMP stack




Raspbian Raspberry Pi Static IP Address

Introduction


This page will describe how to set a static IP address for your Raspberry Pi using Raspbian.
See separate post on how to do the same for Lubuntu / Ubuntu 20.04



Steps


Begin by finding your current IP address :

hostname -I

You will receive back one or more IP address, such as ;

192.168.1.70 
192.168.1.69

From this you learn that your computer is on the 192.168.1.0 network.

Then run :

sudo apt-get install nmap
sudo nmap -sP 192.168.1.0/24

adjusting 192.168.1.0 according to your network.

From this I learnt that my router (dsldevice.lan) has the IP address 192.168.1.254

The DNS server, which is often the same iP address as your router can be found via :

cat /etc/resolv.conf

which gave:

nameserver 192.168.1.254

Now go into your router's admin pages and find the DHCP range.

The next step, while in the router's admin pages, is to limit the DHCP Network Range to 192.168.1.50 - 192.168.1.99. This provides 50 available dynamic IP addresses, which is sufficient for most households.

What this means is that 192.168.1.100 - 192.168.1.253 or 154 IP addresses are available to be set as static IP addresses. 

Again while in the router's admin pages, looking at the IP addresses connected to my router I found that only 192.168.1.201-192.168.202, and of course 192.168.1.254 for the router itself, had static IP addresses.

All other connected devices had dynamic DHCP addresses within the 192.168.1.50 - 192.168.1.99 range.

See the bottom of this page for screen captures from my router.

Running on my Raspberry Pi :

ifconfig

showed that the address for eth0 was 192.168.1.70 and wlan0 was 192.168.1.69

I therefore chose to setup my main Raspberry Pi using a static IP address for its Ethernet (eth0) connection, but to leave its wireless wlan0 to be dynamically set via DHCP.


sudo nano /etc/dhcpcd.conf


I added the following text to the end of the /etc/dhcpcd.conf file.

interface eth0
static ip_address=192.168.1.205/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.254

sudo ip link set eth0 down
sudo ip link set eth0 up
ifconfig

Running ifconfig should show the change in IP address.

If the static IP address does not show up try running ifconfig several times, it sometimes takes a few seconds to show.






Thursday 12 September 2019

Creating a Raspberry Pi Samba Server

Introduction


This page provides the bare minimum information needed to get a Windows Samba server up and running on your Raspberry Pi.

Once the Samba server is running you will be able to play videos stored on the Samba server using clients such as iPad, Tablet, mobile phones and Windows PCs etc.


Installation


sudo apt update
sudo apt upgrade
sudo apt-get install samba samba-common-bin
sudo mkdir -p /home/shares/public
sudo chmod 777 -R /home/shares/public
sudo nano /etc/samba/smb.conf


sudo nano /etc/samba.conf (/etc/samba/smb.conf in XFCE)


At the end of the file add the following:
[Public]
comment = Raspberry Pi 4GB Samba Storage
path = /home/shares/public 
browseable = yes
only guest = no
create mask = 0777
directory mask = 0777
public = yes
guest ok = yes


sudo smbpasswd -a pi       (  sudo smbpasswd -a ubuntu in XFCE for user ubuntu )

The above creates a new samba user and sets the password.
To simply change the samba password to a new password use:

sudo smbpasswd pi

Adjust the following mount command to reflect the /dev/location of the files being shared (e.g. /dev/sda2).

sudo mount -t auto /dev/sda2 /home/shares/public

sudo /etc/init.d/smbd restart


Samba Client


To access Samba shares on your Raspberry Pi and other computers you will need to begin by installing :

sudo apt-get install smb4k smbclient


More to follow....


ext4 Partition Label


It may help you to arrange your partitions if they have meaningful names.

To change the name of an existing ext4 partition use e2label :

sudo e2label /dev/sda2 Files


Specifying Exact Drive / Partition to use as Samba Drive

blkid

Exam the output of blkid to find the UUID (unique identifier) of your chosen drive for Samba.

Here the chosen drive is /dev/sdb

/dev/sdb: LABEL="Samba4TB" UUID="979610b6-2a3f-432e-bc11-5465bcb5253c" TYPE="ext4"

To automatically use /dev/sdb without it becoming sdc or sdd etc if more drives are added at boot time, copy the text in red but remove the "

UUID=979610b6-2a3f-432e-bc11-5465bcb5253c

Now make sure your chosen drive is mounted.
Then run to find the mounting information for it in red.

cat /proc/mounts

/dev/sdb /media/ubuntu/Samba4TB ext4 rw,nosuid,nodev,relatime 0 0

Change (if necessary the desired location of the mount point to be consistent with Samba configuration file, e.g. 

/dev/sdb /home/shares/public ext4 rw,nosuid,nodev,relatime 0 0

sudo nano /etc/fstab

Add from the previous two commands the following to the bottom of the /etc/fstab file:

# Storage
UUID=979610b6-2a3f-432e-bc11-5465bcb5253c /home/shares/public ext4 rw,nosuid,nodev,relatime 0 0

Now umount your the Samba drive, e.g.

umount /dev/sdb

Then to reread the edited /etc/fstab and to update /etc/mtab or /proc/mounts that shows mounted drives run:

sudo mount -a



Example External USB Share

Using a USB drive as a share requires that the user mounting the drive be specified so that all guest and other Samba users requests go through that user.
Without

force user = pi

it simply isn't possible to access files via Samba on an external USB drive.

The share can be setup under Linux (not primarily Samba) so that nobody can Write but everyone can Access / View, so essentially everyone has read and execute permissions only.

[Samba-4TB-Hard-Drive]
comment = Samba 4TB USB WD Hard Drive
path = /media/pi/Samba4TB
Public = yes
read only = yes
browseable = yes
only guest = no
create mask = 0700
directory mask = 0700
Guest ok = yes
force user = pi

The Linux user group in this example was also pi

For more details Google for force user and external USB drive.

There is an AskUbuntu post 'Cannot sharing my second harddrive with Samba'

and

Sharing External Drive using Samba in Ubuntu 18.04


Wednesday 11 September 2019

Run Raspberry Pi 4 root directory / from a USB 3 SSD

Update

Since first writing this blog, a much better blog has emerged that covers in detail:

1. Continuing to use SD card for boot via /boot
2. Uses an SSD for everything not /boot e.g. root /

Raspberry Pi 4 USB Boot Config Guide for SSD / Flash Drives

I strongly recommend the James A. Chambers blog.

My original blog is shown below, which will shortly be updated with the above instructions, where relevant.

Until then I would use

Raspberry Pi 4 USB Boot Config Guide for SSD / Flash Drives



Introduction


Running the root directory / from a USB 3 SSD significantly increases the speed of your Raspberry Pi 4.
You will achieve desktop web performance in Chromium.
It is amazing how much faster the Pi 4 is with the root directory / on USB 3 SSD.

The speed increase will be particularly noticeable when you have several Chromium tabs open.

There are two steps:
  1. Edit the file /boot/cmdline.txt file (microSD)
  2. Run sudo rsync -avx / /media/pi/ExternalSSD
    replacing ExternalSSD with the SSD mount point in /media
Editing the /boot/cmdline.txt file tells the microSD to pass control to the USB 3 SSD by specifying that the root directory / is on the USB 3 SSD (i.e. not on the microSD).

You will still need the microSD for initial boot up but after that the Raspberry Pi 4 runs directly from the root directory / on the USB 3 SSD.

Finally you do the following steps at your own risk.
These instructions have been prepared in good faith and are provided without warranty. Things can and do go wrong.
I suggest making an offline copy of your microSD and using that for this procedure. 
In case of any problems you can always access your Raspberry Pi with the original microSD.

Caveat Emptor !




0. Preparation

Before you make any changes to /boot/cmdline.txt it should look similar to:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=4831f5fb-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

Begin by finding the UUIDs for ALL available disks on system:

ls -lha /dev/disk/by-uuid
lrwxrwxrwx 1 root root  10 Jan 19 17:43 3122c401-b3c6-4d27-8e0d-6708a7613aed -> ../../sdb2
lrwxrwxrwx 1 root root  10 Jan 19 17:43 3FFE-CDCA -> ../../sdb1

lrwxrwxrwx 1 root root   9 Jan 19 17:38 979610b6-2a3f-432e-bc11-5465bcb5253c -> ../../sda

cat /etc/fstab
PARTUUID=4831f5fb-01  /boot           vfat    defaults          0       2
PARTUUID=4831f5fb-02  /               ext4    defaults,noatime  0       1

df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        29G   20G  7.9G  72% /
/dev/mmcblk0p1  253M   52M  201M  21% /boot
/dev/sda        3.6T  3.4T   14G 100% /media/pi/Samba4TB
/dev/sdb1       253M   40M  213M  16% /media/pi/temp1
/dev/sdb2        29G   18G   11G  62% /media/pi/temp2


sudo blkid
/dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="3FFE-CDCA" TYPE="vfat" PARTUUID="4831f5fb-01"

/dev/mmcblk0p2: LABEL="rootfs" UUID="3122c401-b3c6-4d27-8e0d-6708a7613aed" TYPE="ext4" PARTUUID="4831f5fb-02"

/dev/sda: LABEL="Samba4TB" UUID="979610b6-2a3f-432e-bc11-5465bcb5253c" TYPE="ext4"

/dev/mmcblk0: PTUUID="4831f5fb" PTTYPE="dos"

/dev/sdb1: LABEL_FATBOOT="boot" LABEL="boot" UUID="3FFE-CDCA" TYPE="vfat" PARTUUID="4831f5fb-01"

/dev/sdb2: LABEL="rootfs" UUID="3122c401-b3c6-4d27-8e0d-6708a7613aed" TYPE="ext4" PARTUUID="4831f5fb-02"






1. Edit the /boot/cmdline.txt file


Begin by making a copy in case anything goes wrong:

sudo cp /boot/cmdline.txt /boot/cmdline.bak

sudo nano /boot/cmdline.txt

Change the text from:

root=PARTUUID=a4c81466-02
to
root=/dev/sda1

Note you will have a different PARTUUID number from a4c81466-02

So you should change  cmdline.txt  from something like:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=a4c81466-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

to :

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/sda1 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

The text to delete is in blue and the text to add is in red but you may also have to type the file system if not using ext4

Optionally, you may also want to delete quiet splash in order to view Linux's startup messages.




2. sudo rsync -avx / /media/pi/ExternalSSD


You will need an available partition, preferably sda1, on your USB 3 SSD.

I recommend the first partition and that this USB 3 SSD always be plugged in before any other USB storage device.

The USB SSD partition to be used as the root directory / would typically be /dev/sda1

If you really want to use the PARTUUID in place of /dev/sda1 then run

sudo blkid

Example output (not specific to this tutorial) :
pi@Rasp4-4GB:/ $ sudo blkid
/dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="016B-16E4" TYPE="vfat" PARTUUID="56b1c1da-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="6ead9b69-967c-4c44-ac59-11075ed8e9a7" TYPE="ext4" PARTUUID="56b1c1da-02"
/dev/sdb1: LABEL_FATBOOT="boot" LABEL="boot" UUID="3FFE-CDCA" TYPE="vfat" PARTUUID="a4c81466-01"
/dev/sda1: LABEL="RaspberryPiBoot" UUID="128f5236-c026-44e5-abf8-69397c1e969f" TYPE="ext4" PARTUUID="002be3a8-01"
/dev/sda2: UUID="c0d9a904-2bda-4c38-87e0-5072cfc6ac4f" TYPE="ext4" PARTUUID="002be3a8-02"
/dev/sdb2: LABEL="rootfs" UUID="3122c401-b3c6-4d27-8e0d-6708a7613aed" TYPE="ext4" PARTUUID="a4c81466-02"
/dev/mmcblk0: PTUUID="56b1c1da" PTTYPE="dos"
/dev/sdd1: UUID="ad488156-5256-4e02-9382-cb2bf9c6bfed" TYPE="ext4" PARTUUID="f0cb35b6-01"
/dev/sdd2: UUID="9a4c2ce6-be96-4c66-8bb6-4ec2fda9f58c" TYPE="ext4" PARTUUID="f0cb35b6-02"
pi@Rasp4-4GB:/ $ 

In addition to PARTUUID, blkid will also show the UUID.

If you use root=/dev/sda1 you will be able to use any suitably prepared USB SSD and not just the paritcular SSD identified by PARTUUID.

I further recommend that the USB SSD partition be formatted using ext4 .

A good tool for creating the USB SSD partition and formatting under Linux / Raspberry Pi is GParted.

GParted can be installed via:

sudo apt-get install gparted

The next step is to run :

sudo rsync -avx / /media/pi/ExternalSSD

Replace ExternalSSD with the exact USB SSD mount point name shown when mounting in /media/pi/....

Enjoy !
:-)



Further Help


How to Run Raspberry Pi 4 or 3 Off an SSD or Flash Drive

Raspberry Pi 4 With an SSD: Dramatic Speed Improvements, Higher Price

YouTube : How to boot Raspberry PI4 from USB

Tuesday 27 August 2019

Raspberry Pi 4 ffmpeg screen recording using x11grab

Introduction

This page provides simple instructions on how to screen record videos on your Raspberry Pi 4 using nothing more than ffmpeg .

This ffmpeg approach is favoured over using the more common graphical tools for screen recording, as ffmpeg provides greater user control. Not all screen recorders work well with the Raspberry Pi 4.

One interesting finding is that the h264_omx video encoder seems to work a lot faster when the input and output are matroska container files mkv .

The h264_omx video encoder is noticeably slower when the container output file is an mp4 . It is therefore suggested to copy the final h.264 encoded video from a mkv to mp4 container as the final step and to do all video transcoding between mkv container files.

With this ffmpeg based screen capture method the user can create a series of screen recordings that can be combined to create a YouTube video etc using a video editor such as Shotcut.


ffmpeg


echo $DISPLAY
:1

ffmpeg -f x11grab  -s 1824x984 -i :1.0 -r 30 -c:v mpeg2video -b:v 6M -f matroska -y mytest-6MB-r30.mkv 




Method

Key points to remember are:

  1. Use the MKV matroska container
  2. Use the mpeg2video video codec for original screen recording
  3. Convert to mp4 and h.264 (if desired) as later steps

Note these instructions consider video only, they would need to be expanded to included audio. See bottom of page for further details.

ffmpeg -f x11grab  -s 1920x1080 -i :0.0 -r 30 -c:v mpeg2video -b:v 6M -f matroska -y mytest-6MB-r30.mkv

This example uses a 6 megabit video bit rate and a frame rate of 30 frames per second (fps). See ffmpeg documentation. The command also records the full screen.  

Now convert to h.264 using the GPU hardware encoder h264_omx using a 3 megabit video bit rate. Note the movflags and faststart are flags are optional but enable the file to start up quickly without the need for the entire file to download first.

ffmpeg -i mytest-6MB-r30.mkv -c:v h264_omx -b:v 3M -movflags +faststart -f matroska -y mytest-3MB-r30-h264_omx.mkv

Alternatively, consider 2 pass encoding which produces higher quality video but takes longer to encode:

ffmpeg -i mytest-6MB-r30.mkv -c:v h264_omx -b:v 3M -pass 1 -y -f matroska /dev/null && ffmpeg -i mytest-6MB-r30.mkv -movflags +faststart -c:v h264_omx -b:v 3M -pass 2 -f matroska mytest-3MB-r30-h264_omx.mkv 

Finally copy the video encoded to the mp4 container if desired:

ffmpeg -i mytest-3MB-r30-h264_omx.mkv -c:v copy -f mp4 -y mytest-3MB-r30-copy.mp4


That is it!



Further Help

For further advice, including recording audio at the same time as video please see the below links.

ffmpeg Capture Desktop wiki


How to get near-perfect screen recording quality?

How to make an MPEG2 video file with the highest quality possible using FFMPEG?


Screen recording (in X11) with ffmpeg

An Overview of H.264 Advanced Video Coding

Video Encoding Settings for H.264 Excellence (good discussion)

Wednesday 20 March 2019

Introduction

After thinking about buying a Raspberry Pi for many years, I finally got around to buying a 3B+from thepihut.com

This page records my experiences and tips.
If anyone else finds them useful then Great!
If not, thanks for reading and please send me your tips.

Installation


nmap -sn 192.168.0.0/24

hostnane -I

http://qdosmsq.dunbar-it.co.uk/blog/2013/06/noobs-for-raspberry-pi/

Format an SD card which is 8GB or larger as FAT.
Download and extract the files from the NOOBS zip file.
Copy the extracted files onto the SD card that you just formatted, so that this file is at the root directory of the SD card.
[ Copy contents of extracted zip file, e.g. NOOBS_v2_8_1., to SD card]

https://www.raspberrypi.org/documentation/installation/noobs.md

Must Haves

To get the best from a Raspberry Pi I strongly recommend :

 Sandisk 32gb Micro Sd Sdhc Ultra Memory Card Uhs 1 80mb Class 10 Inc



How To Change The Command Line Font Size

https://www.raspberrypi-spy.co.uk/2014/04/how-to-change-the-command-line-font-size/
http://www.stevencombs.com/raspberrypi/2016/05/18/change-terminal-font-size-raspi.html

sudo dpkg-reconfigure console-setup
https://unix.stackexchange.com/questions/77049/how-do-i-change-the-screen-font-size-when-using-a-virtual-console

sudo dpkg-reconfigure console-setup
#select "UTF-8"
#select "Guess optimal character set"
#select "Terminus"
#select 16*32 (my selection for a 7 inch monitor on raspberry pi)


sudo nano /etc/default/console-setup
 
You should edit the file /etc/default/console-setup and 
change the FONTSIZE variable. 
Once you've made your changes you must reconfigure your 
terminal by running:

sudo service console-setup restart 
OR
sudo /etc/init.d/console-setup restart
 

https://askubuntu.com/questions/173220/how-do-i-change-the-font-or-the-font-size-in-the-tty-console/173221#173221

https://www.raspberrypi-spy.co.uk/2014/04/how-to-change-the-command-line-font-size/



Software to Install

  • omxplayer   
  • ufw
  • gufw
  • hfsprogs 
  • hfsutils 
  • hfsplus
  • ntfs-3g
  • exfat-fuse 
  • exfat-utils
  • usbmount (configure with sudo nano 

Use omxplayer in place of VLC, as omxplayer has dedicated Raspberry Pi 3B+ hardware support thereby making video decoding MUCH faster.
For omxplayer keys see https://www.raspberrypi.org/forums/viewtopic.php?t=13328

https://www.raspberrypi-spy.co.uk/2013/06/playing-videos-on-the-raspberry-pi-command-line

omxplayer ufw gufw hfsprogs hfsutils hfsplus ffmpeg ntfs-3g exfat-fuse exfat-utils ntfsdoc mpv mplayer ttf-mscorefonts-installer fonts-liberation fonts-liberation2 fonts-croscore fonts-arkpandora gufw mkvtoolnix mediainfo mc vlc fbi fim



--win "0 0 640 480"
https://raspberrypi.stackexchange.com/questions/24360/omxplayer-video-position-and-dimension

Setup a firewall

Use ufw or gufw firewall

sudo apt-get install ufw
sudo apt-get install gufw
sudo ufw enable
sudo ufw allow 22
sudo ufw deny 22


https://help.ubuntu.com/lts/serverguide/firewall.html


Playing Audio files (e.g. AAC m4a)
omxplayer filename.m4a

 + or -  = Up / Down Volume.

left or right arrow keys = backwards / forwards.


ffmpeg

https://ffmpeg.org/ffmpeg.html

ffmpeg -encoders | grep aac
          aac

ffmpeg -encoders | grep h264
ffmpeg -decoders | grep aac

ffmpeg -decoders | grep h264
          h264
          h264_mmal
          h264_vdpau

ffmpeg -encoders | grep h264_omx
          libx264
          libx264rgb
          h264_omx
          h264_vaapi

For decoding videos in FFMPEG use h264_mmal
For encoding videos in FFMPEG use h264_omx

ffprobe file.mp4 | less


DVD = 720 x 480

sudo apt-get update
sudo apt-get search ntfs
sudo ufw status

-c:v h264_omx


-c:v h264_mmal

-c:a copy
-c:a aac


ffmpeg -c:v


In this example :

ffmpeg -c:v h264_mmal -i inputfile.mp4 -c:v h264_omx -c:a copy -b:v 1500k outputfile.mp4

-c:v h264_mmal is used for decoding
-c:v h264_omx is used for encoding



https://trac.ffmpeg.org/wiki/Encode/AAC
https://trac.ffmpeg.org/wiki/Encode/H.264

This command encodes a video with good quality, using slower preset to achieve better compression:  

ffmpeg -i input.avi -c:v libx264 -preset slow -crf 22 -c:a copy output.mkv

ffmpeg -i input -c:v libx264 -preset slow -crf 28 -c:a aac -b:a 128k output.mp4

Replace libx264 with h264_omx to achieve faster h.264 encoding, but at the expense of some quality (software encoders such as libx264 are generally of higher quality than more simplistic yet faster hardware encoders such as h264_omx ).

ffmpeg -i Mike06.mkv -movflags faststart -ss 0 -t 60 -c:v libx264 -crf 23 -profile:v baseline -level:v 3.1 -maxrate 3500k -bufsize 3500k -c:a aac -b:a 256k -ac 2 -ar 48000 -y h264test.mp4

ffmpeg -i Mike06.mkv -movflags faststart -ss 0 -t 60 -c:v h264_omx -b:v 1750k -maxrate 3500k -bufsize 3500k -c:a aac -b:a 256k -ac 2 -ar 48000 -y omxtest.mp4

libx264 = 9.7 MB
h264_omx=14 MB

Baseline temperature = 48 C

Lowering the -crf from 23 to 20 (i.e. improving the quality) improves the video quality and results in a file size of 13.6 MB. The FFmpeg code runs at speed=0.589x and an overall bit rate of 1905kbits/s. Temperature peaked at 60.1 C.
Visually this is my preferred FFmpeg encoding, although it is much slower than the equivalent h264_omx encode.


For the h264_omx encode the figures were speed=1.21x and an overall bit rate of 1951kbits/s.
Temperature peaked at 52.6 C.

Running the same tests for the full 60 mins of the input video we have

libx264 -crf 20
max temp 63.4 speed=0.54x bitrate=1870kbps fps=13
filesize=810MB

h264_omx
max temp=52.6 speed=1.43x bitrate=2012kbps fps=36
filesize=872MB

Running the h264_omx encoder instead of the libx264 produces noticeably faster encoding times and a much cooler CPU. In the above example, which consisted of a teacher in front of a white board, obtained from a DVD, the above two ffmpeg examples produced broadly similar results - and certainly good enough for teaching purposes.


fbi

Frame Buffer
sudo apt-get install fbi

fbi -a -t 5 -u --readahead *

fbi --edit filename
then use r (90 clockwise), l (90 anti-clockwise).
File automatically saves with changes

Also consider from fim
fim xyz.jpg

fim is supposed be fbi improved.
https://www.raspberrypi-spy.co.uk/2014/09/how-to-display-images-on-raspbian-command-line/

ffmpeg Examples and Tips


The following examples use the Raspberry Pi inbuilt hardware h.264 video encoder and the Advanced Audio Codec (AAC) encoder.


Simple Version

ffmpeg -i input.mkv -c:v h264_omx -b:v 1750k -c:a aac -b:a 192k output.mp4

Change -b:v 1750k to suit your required video quality and similarly -b:a 192k for audio quality, larger values are better quality (in bits). 


Full Version

ffmpeg -i input.mkv -movflags faststart -c:v h264_omx -b:v 1750k -maxrate 3500k -bufsize 3500k -c:a aac -b:a 192k -ac2 -stats -y output.mp4


ffmpeg -i input.mkv -movflags faststart -c:v libx264 -crf 16 -preset ultrafast -tune fastdecode -c:a aac -stats -y output.mp4

ffmpeg -i input.mkv -movflags faststart -ss 00 -t 30 -c:v libx264 -crf 16 -preset ultrafast -tune fastdecode -c:a aac -stats -y output.mp4


ffmpeg -i audio.aac -acodec mp3 -ac 2 -ab 160 audio.mp3
https://raspberrypi.stackexchange.com/questions/14268/converting-aac-to-mp3?rq=1

https://www.reddit.com/r/raspberry_pi/comments/5677qw/hardware_accelerated_x264_encoding_with_ffmpeg/

https://www.raspberrypi.org/forums/viewtopic.php?t=182960

https://maniaclander.blogspot.co.uk/2017/08/ffmpeg-with-pi-hardware-acceleration.html

https://raspberrypi.stackexchange.com/questions/3936/what-speed-can-i-expect-from-the-hardware-h264-encoding

https://mediaexperience.com/raspberry-pi-xbmc-with-raspbmc/


uname -a
sudo mount -t auto /dev/sda1 /media/pi/sda1
while true; do date; df -hm; sleep 10; clear;  done
sudo alsa force-reload
sudo fdisk -l grep sd
cat /etc/mtab
cat /etc/fstab
mc (switch between windows with tab key)

5 commands to check memory usage on Linux
free
top
htop

while true; do free; sleep 5; done

~/fonts.conf


Temperature

Warning icons
https://www.raspberrypi.org/documentation/configuration/warning-icons.md

/opt/vc/bin/vcgencmd measure_temp

https://www.raspberrypi.org/forums/viewtopic.php?t=34994

while true; do /opt/vc/bin/vcgencmd measure_temp; sleep 5; done


Mount Mac HFS+
sudo apt-get install hfsprogs hfsutils hfsplus
sudo mount -t auto /dev/sdc /home/pi/sdc1
Note on my system
sudo fdisk -l
simply shows sdc not sdc1 but mounting still worked.


apt-get install ntfs-3g

and

apt-get install exfat-fuse exfat-utils


Network scan

ip address

nmap -sn 192.168.1.0/24






Tuesday 19 March 2019

Raspberry Pi 3b+ Video Encoding with ffmpeg libx264 and h264_omx

Raspberry Pi 3b+ Video Encoding with ffmpeg libx264 and h264_omx


Note that this is a live page that will be updated regularly. 

Please give your corrections and suggestions at the bottom of the page. 

Constructive comments only !

Introduction

This page describes how to use ffmpeg to encode videos using the h.264 codec on the Raspberry Pi 3b+.

There are two ffmpeg h.264 encoders available for the Raspberry Pi:
  1. Software (CPU) based h.264 encoder libx264 
  2. Hardware (GPU) based h.264 encoder h264_omx
libx264 provides high quality output and offers most of the x264 options. The downside is that as it is software based (i.e. it uses the CPU) it encodes at about 0.53x, i.e. a one hour video takes nearly two hours to encode,

h264_omx uses the GPU (video card) rather than the CPU and so is very fast. It encodes at about 1.2x, i.e. a one hour video takes less than one hour to encode. The output from h264_omx is inferior to that of libx264. Also the only encoding option available with h264_omx  is to set the video bit rate (e.g. -b:v 3000k).   

So you have a choice between a high quality, slow h.264 encoder with lots of x264 options, i.e. libx264, or a fast h.264 encoder with really just one option (setting the video bit rate) that produces inferior single pass output, i.e. h264_omx


Recommendation

The best combination of encoding speed and video quality was obtained by using h264_omx with 2 pass encoding. The h264_omx video quality results were significantly better than using with single pass h264_omx encoding.

Example 2 pass h264_omx encoding:
ffmpeg -i inputfile -c:v h264_omx -b:v 3000k -pass 1 -an -y -f mp4 /dev/null && ffmpeg -i inputfile -movflags +faststart -c:v h264_omx -b:v 3000k -pass 2 -c:a aac -b:a 256k -ac 2 -ar 48000 -y outputfilename.mp4 

Adjust the video bit rate -b:v 3000k and audio bit rate -b:a 256k to taste.

2 pass encoding with h264_omx was found to be significantly faster than 1 pass encoding with libx264
As long as you have the time and patience, and your CPU doesn't overheat, then libx264 does produce superior results to h264_omx

If you require the absolutely highest possible video encoding quality in a reasonable time frame, then it is recommended to use Handbrake or ffmpeg running on Linux with an recent i7 processor.
For most user cases though, 2 pass encoding with h264_omx will produce good quality video as long as you don't need to resize the video.


Assumptions

It is assumed that you are:
  1. Using Raspberry Pi 3b+ (not tested with earlier Pis)
  2. Raspbian (Raspbian Stretch with desktop)
  3. You have installed ffmpeg :
    sudo apt-get install ffmpeg 
  4. You have installed VLC for video testing purposes:
    sudo apt-get install vlc
  5. You have a basic familiarity with ffmpeg and the Linux bash shell.
Note that as of late 2018 VLC comes with hardware h.264 decoding enabled, thereby making it simple to watch h.264 videos on the Raspberry Pi.


Single Pass Examples

These two examples encode the first minute of the input test video Mike06.mkv (an mpeg 2 DVD file simply ripped to MKV without any codec change using MakeMKV on an i7 Linux Mint PC).

Remove -ss 00:00:00 -to 00:01:00 to encode the full video.

h264_omx
ffmpeg -i Mike06.mkv -ss 00:00:00 -to 00:01:00 -c:v h264_omx -b:v 1500k -c:a aac -b:a 256k -ac 2 -ar 48000 -y mike06-h264_omx-1500.mp4 

Adjust -b:v 1500k to change the video bitrate (quality).

libx264
ffmpeg -i Mike06.mkv -ss 00:00:00 -to 00:01:00 -c:v libx264 -crf 20 -preset medium -profile:v high -level:v 4.0 -c:a aac -b:a 256k -ac 2 -ar 48000 -y mike06-libx264-crf20.mp4 

In the above 1 minute test video a 12.8 MB output file corresponded to:
-c:v libx264 -crf 20 
and 
-c:v h264_omx -b:v 1500k 

The libx264 encoder produced better video quality than h264_omx even though the output file sizes were similar. The libx264 encoding time was however more than double that of h264_omx

In another test that had a very high quality h.264 encoded input file (1920x1080) with an original bit rate around 16 MB the libx264 encoder was encoding at around 7% of original speed with  -crf 20 meaning that a 1 hour encode would take 14+ hours ! 
This is too long to be useful and was aborted well before completion.
Not Recommended.

Using 2 pass encoding with -b:v 16000k and h264_omx produced very watchable high quality video quality. 
Strongly Recommended.

The best combination of encoding speed and video quality was obtained by using h264_omx with 2 pass encoding.
The h264_omx video quality results were significantly better than using with single pass h264_omx encoding.

Example 2 pass h264_omx encoding:

ffmpeg -i inputfile -c:v h264_omx -b:v 3000k -pass 1 -an -y -f mp4 /dev/null && ffmpeg -i inputfile -movflags +faststart -c:v h264_omx -b:v 3000k -pass 2 -c:a aac -b:a 256k -ac 2 -ar 48000 -y outputfilename.mp4 

Adjust the video bit rate -b:v 3000k and audio bit rate -b:a 256k to taste.


Tips

Apart from varying the video bitrate here isn't much else you can change for the h264_omx encoder.
You can NOT use h264_omx with -crf
You can ONLY use h264_omx with -b:v
You can also change the video dimensions but at the expense of speed, however I have not tested this option myself. 

For libx264, slower presets than the default -preset medium can be used, however the encoding times become VERY long and the CPU gets extremely hot.
Not Recommended.

Adding -movflags +faststart works for both encoders and enables videos to be instantly streamed without the need to first download the entire video.
Recommended.

While the video profile and level for libx264 can be set up to
-profile:v high -level:v 4.0
the h264_omx encoder automatically sets High@L4 

So while you can control the video profile and level for libx264, these options are not available for the h264_omx encoder.

A reasonable range for the -crf for the libx264 seems to be 20 to 23.  
You can go down to -crf 16 but at the expense of significantly longer encoding times than those for -crf 20  

The libx264 encoding produced the better video quality results of the two encoders using single pass encoding.

A large increase in encoding speed (from 1.2x to 2.5x) can be obtained for the h264_omx encoder by using the MP3 encoder libmp3lame in place of the Advance Audio Codec encoder aac

For example:
ffmpeg -i Mike06.mkv -ss 00:00:00 -to 00:01:00 -c:v h264_omx -b:v 1500k -c:a libmp3lame -b:a 256k -ac 2 -ar 48000 -y mike06-h264_omx-1500-mp3.mp4

The problem with using MP3, is that for mp4 containers (which contain both video and audio) Apple devices expect the audio in an mp4 container to be encoded with AAC not MP3
Consequently mp4 files encoded with mp3 audio will not have any sound on Apple devices. 
Note this does not apply to the more general user case of using  MP3 in audio only files (e.g. m4a).

Using the MP3 encoder libmp3lame in combination with the libx264 encoder didn't improve upon the encoding speed using aac :
ffmpeg -i Mike06.mkv -ss 00:00:00 -to 00:01:00 -c:v libx264 -crf 20 -preset medium -profile:v high -level:v 4.0 -c:a libmp3lame -b:a 256k -ac 2 -ar 48000 -y mike06-libx264-crf20-mp3.mp4 

In fact the encoding with the libx264 and libmp3lame combination was actually slower than using aac ! 
Your results may vary. 
Try it !
 

Example 2 Pass Encoding Outputs from h264_omx


The original input files Mike06.mkv-Mike10.mkv are all mpeg 2 files with a video bitrate of 2MB - 3MB.
These files consist of a language tutor teaching in front of a white board.
Other than the tutor's Hawaiian shirts there isn't much detail in the video.
There isn't much movement and the original video quality is just 'fair'.

ffprobe -i Mike06.mkv
Stream #0:0(eng): Video: mpeg2video (Main), yuv420p(tv, progressive), 720x576 [SAR 64:45 DAR 16:9], 25 fps, 25 tbr, 1k tbn, 50 tbc
Stream #0:1(eng): Audio: mp2, 48000 Hz, stereo, s16p, 224 kb/s (default)

The same ffprobe results are also obtained for Mike07.mkv-Mike10.mkv

In the below ffmpeg outputs, note how the h264_omx first pass encoding takes around 4x speed and the second  h264_omx pass encoding around 1.5x speed, given an overall encoding time of around 55 minutes for a 60 minute video (i.e.60 / 4 = 15 mins + 60/1.5 = 40 =55 minutes) or 0.92x speed.
This compares with around 0.5x speed for a single pass libx264 for a modest overall improvement in video quality. 

Mike06.mkv
frame=90864 fps= 95 q=-0.0 Lsize=  927507kB time=01:00:34.52 bitrate=2090.5kbits/s speed= 3.8x 
frame=90864 fps= 35 q=-0.0 Lsize= 1043451kB time=01:00:34.56 bitrate=2351.9kbits/s speed= 1.4x

Mike07.mkv
frame=114092 fps= 98 q=-0.0 Lsize= 1178330kB time=01:16:03.64 bitrate=2115.2kbits/s speed=3.93x
frame=114092 fps= 37 q=-0.0 Lsize= 1325673kB time=01:16:03.69 bitrate=2379.6kbits/s speed= 1.5x

Mike08.mkv
frame=97683 fps=107 q=-0.0 Lsize=  997178kB time=01:05:07.28 bitrate=2090.7kbits/s speed=4.29x
frame=97683 fps= 39 q=-0.0 Lsize= 1122635kB time=01:05:07.32 bitrate=2353.7kbits/s speed=1.55x

Mike09.mkv
frame=86881 fps=106 q=-0.0 Lsize= 1170912kB time=00:57:55.20 bitrate=2760.2kbits/s speed=4.26x 
frame=86881 fps= 33 q=-0.0 Lsize= 1279450kB time=00:57:55.26 bitrate=3016.0kbits/s speed=1.31x 

Mike10.mkv
frame=132565 fps=106 q=-0.0 Lsize= 1738525kB time=01:28:22.56 bitrate=2685.9kbits/s speed=4.25x
frame=132565 fps= 39 q=-0.0 Lsize= 1909932kB time=01:28:22.61 bitrate=2950.7kbits/s speed=1.57x

Very High Quality Input file

The very high quality h.264 encoded input file (1920x1080) with an original bit rate of around 16 MB (briefly described above), has the following attributes:

ffprobe -i VS2017.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'VS2017.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 2017-12-03T09:29:42.000000Z
    encoder         : HandBrake 1.0.7 2017040900
  Duration: 00:42:30.28, start: 0.000000, bitrate: 16300 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 15961 kb/s, 29.94 fps, 29.97 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2017-12-03T09:29:42.000000Z
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 328 kb/s (default)
    Metadata:
      creation_time   : 2017-12-03T09:29:42.000000Z
      handler_name    : Stereo

This very high quality input file is taken from the 2017 Victoria Secret Shanghai show.
Given the high level of detail in the clothes, skin tones, makeup and rapid onstage movement, the Victoria Secret shows provide an excellent video encoding test.

The 2 pass h264_omx encoder produced very good results when encoding this very high quality Victoria Secret 2017 input file.

By contrast, the libx264 encoder was encoding at around 7% of original speed with a quality of  -crf 20 meaning that a 1 hour encode would take 14+ hours ! 
This is too long to be useful and was aborted well before completion.
Not Recommended.

Using 2 pass encoding with -b:v 16000k and h264_omx produced very watchable high quality video quality. 
Strongly Recommended.


More Content to Follow


ffmpeg -i input.mp4 -c:v h264_omx -b:v 16000k -c:a aac -b:a 256k -ac 2 -ar 48000 -y output.mp4


ffmpeg -i input -c:v h264_omx -b:v 16000k -pass 1 -an -y -f mp4 /dev/null && ffmpeg -i input -movflags +faststart -c:v h264_omx -b:v 16000k -pass 2 -c:a aac -b:a 256k -ac 2 -ar 48000 -y output.mp4

ffmpeg -i input -c:v libx264 -crf 20 -preset medium -profile:v high -level:v 4.0 -c:a aac -b:a 256k -ac 2 -ar 48000 -y output.mp4 


ffmpeg -i VS2018-CRF16-veryslow-fastdecode-web-high4-HB.mp4 -c:v h264_omx -b:v 16000k -pass 1 -an -y -f mp4 /dev/null && ffmpeg -i VS2018-CRF16-veryslow-fastdecode-web-high4-HB.mp4 -movflags +faststart -c:v h264_omx -b:v 16000k -pass 2 -c:a aac -b:a 256k -ac 2 -ar 48000 -y VS2018-ICE-16MB.mp4



ffmpeg -i Wilt\ \(1989\).mkv -movflags +faststart -c:v libx264 -crf 20 -c:a aac -b:a 256k -ac 2 -ar 48000 -y Wilt-1pass-libx264-CRF20.mp4

Starting second pass: moving the moov atom to the beginning of the file1.07x   
frame=133046 fps= 27 q=-1.0 Lsize=  876248kB time=01:28:41.72 bitrate=1348.9kbits/s speed=1.07x



Links