Skip to content

How to Convert h264 video to MP4

Claude Pageau edited this page Jan 13, 2017 · 42 revisions

Editing How join MP4 or Convert h264 video to MP4

###convid.sh Description convid.sh will automate conversion of h264 files to mp4 format per the takeVideo function subprocess call. It uses the MP4Box utility that is installed as part of gpac when the latest pi-timolo-install.sh script is run. Default is to process h264 files in the /home/pi/pi-timolo/motion folder. It will convert all available h264 files and wait for the most recent file since it may still be in progress. The h264 files can optionally (default) be deleted or renamed after conversion per the del_h264 variable. Deleting the smaller source videos will save disk space.

convid.sh can join multiple mp4 videos into one bigger datetime named Output file and optionally delete source files. The max number of joins can be controlled with the max_joins variable. Joining is handy for preserving archives videos, when pi-timolo.py is set to recycle filenames. This can be run in a crontab once a day or on a specific time schedule depending on your needs. Deleting the source mp4's will prevent joined output file duplications.

###How to Run convid.sh

You Must Specify a Valid Parameter per below. Help will be displayed if no parameter is specified.

filepath - convert h264 file per filepath  NOTE - Original filename will be preserved   
all      - converts all h264 files in folder per variable source_h264   
join     - Joins all mp4 files into a dated mp4 per specified max_joins variable   

Examples

./convid motion/mo-cam1-1234.h264
./convid convert"
./convid join"

You can also run from a crontab

sudo crontab -e

Then add a line per sample below to execute convid.sh join once a day at 10pm as pi user

0 22 * * * su pi -c "/home/pi/pi-timolo/convid.sh join > /dev/null"

It is recommended you test the script before adding a crontab entry.

###convid.sh Variable Settings Use nano to edit the convid.sh script variables to suit per github code here convid.sh

cd ~/pi-timolo
nano convid.sh

Use nano to edit convid.sh variables below as required then ctrl-x y to save and exit nano

# H264 Conversion Variable Settings
#----------------------------------
source_h264=/home/pi/pi-timolo/motion/*h264  # Path and file type of source files to convert
del_h264=true     # delete=true rename=false

# MP4 Join Variables Settings
#----------------------------
max_joins=12   # Number of videos to join before starting New Join
source_mp4="/home/pi/pi-timolo/motion" 
dest_mp4=/home/pi/pi-timolo/videos
video_prefix=convid  # Prefix of output join file
command_to_run='/usr/bin/MP4Box -add '  # Command for converting video format

You can also choose to rename or delete h264 files after processing by setting the del_h264 boolean variable to true or false per code comments. It is recommended you leave default delete option in order to save space. The mp4 files can be easily viewed using webserver.sh on the RPI and chrome browser (tested) on a local network computer. Other browsers may allow directly viewing mp4 files as well.

The convid.sh join variables set the maximum number of joins per larger output video plus source, destination, output file prefix, etc. You should not touch the command_to_run variable

###How to Turn on Video Mode in pi-timolo To enable pi-timolo motion video mode edit /home/pi/pi-timolo/config.py and change the following variable(s).

cd ~/pi-timolo
nano config.py

Then edit/review the following variables. ctrl-x y to save changes and exit nano editor

motionVideoOn = True     # if True then video clip is taken rather than image
motionVideoTimer = 10    # Seconds of video recorded if Motion Detected default=10
motionNumOn = False      # True=On (filenames by sequenced Number) otherwise date/time used for filenames

It is highly recommended that you keep the motionVideoTimer setting a short duration Eg 5 to 20 seconds. This will reduce browser download time and also save disk space. Normally when there motion is detected, more than one video will be generated. Motion detection is disabled while video is being recorded so setting a short duration enables motion detection faster. motionNumOn setting will create filenames with a date/time creation stamp as part of the video name.

To reduce video file sizes you can also reduce the video resolution by editing the following config.py settings (this is web cam resolution) You may need to adjust larger for better resolution.

imageWidth = 640          # Full Size Image Width in px  default=1024
imageHeight = 480         # Full Size Image Height in px default=768 

###webserver.py setting to sort Videos by filename use nano to edit webserver.py and change the variable below

show_by_datetime = False

This will sort by file name and put the files in the correct order

###Set convid.sh to automate conversion of h264 files to MP4 format

IMPORTANT pi-timolo.py ver 3.0 or later defaults to run convid.sh in a subprocess as each video is recorded. The h264 file will be passed to convid.sh as a parameter and convid will convert it to MP4 format and sync (touch -t) to sync the file dates then optionally delete the h264 file (default).

You can run convid.sh manually to convert and h264 files that may be missed. Be sure that permissions are correct. If you follow the run on boot instructions the pi-timolo will run in background as pi user.

You can run this script in a crontab if you wish but should not be necessary if the pi-timolo.py conversion is working OK

sudo crontab -e

add the following line to the bottom of the crontab file to execute convid.sh once every minute. This can be changed if you wish eg */2 is every two minutes Etc..

To run as pi user

0 22 * * * su pi -c "/home/pi/pi-timolo/convid.sh join >/dev/null"
Optional start sync.sh and makevideo.sh
*/5 * * * * su pi -c "/home/pi/pi-timolo/sync.sh > /dev/null"
0 22 * * * su pi -c "/home/pi/pi-timolo/makevideo.sh > /dev/null"

TIP - To join two compatible videos (must be same resolution and framerate) use command similar to below

MP4Box -cat file1.mp4 -cat file2.mp4 -new outfile.mp4

You will need to write a script to concatenate a series of files

###Configure sync.sh to automate upload of mp4 videos You can configure sync.sh to upload mp4 files. See sync.sh script variables to do this.
For Details See How to Configure sync.sh to Automate uploads
Note: This will require a reasonably fast internet connection. To optimize you can reduce video resolution and recording time per recommendations above

Clone this wiki locally