Skip to content

How to Convert h264 video to MP4

Claude Pageau edited this page Feb 13, 2018 · 42 revisions

Join MP4 Files or Convert h264 video to MP4

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.

How To Edit convid.sh Settings

Use nano to edit the video.conf settings for convid.sh script variables to suit. For more detail See convid.sh GitHub Code

cd ~/pi-timolo
nano video.conf

Use nano to edit video.conf variables for convid.sh per comments then ctrl-x y to save and exit nano

You can also choose to rename or delete h264 files after processing by setting the mp4_del_h264 boolean variable to true or false per code comments. 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 = 30    # 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 recommended that you keep the motionVideoTimer setting to a short duration Eg 5 to 30 seconds. This will reduce browser download time and also save disk space. Normally when 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 return to motion detection faster.

motionNumOn setting can create filenames with a date/time creation stamp as part of the video name.

If you want a date time stamp annotated directly on the video stream set config.py showDateOnImage = True. The font size can be controlled with the showTextFontSize variable. Default is at top middle with black text on a white background. To change colors you will need to directly edit the pi-timolo.py script in the takeVideo function.

showDateOnImage = True     # Set to False for No display of date/time on image default= True
showTextFontSize = 20      # Size of image Font in pixel height

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 

Using Video Plugins

There are several video plugins that make it easier to implement a specialized video task.

  • strmvid On Motion take 640x480 video at 10fps. Designed for efficient upload to rclone storage name. Add rclone-video.sh to sync to remote service.
  • secvid plugin is motion triggered and will take HD videos that are more suited for local records. T
  • dashcam will take a continuous series of HD videos. This can be handy for vehicle or wildlife projects
  • slowmo On Motion Take 640x480 video at 90fps that will produce a slow motion video at 30fps

For more details see Wiki How to Use Plugins

webserver 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

Convert 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 bulk convert h264 files. Be sure that permissions are correct. If you follow the run on boot instructions the pi-timolo will run in background as pi user.

cd ~/pi-timolo
./convid.sh convert

How to Automate joining of MP4 Videos

You can run convid.sh join in a crontab if you wish to bulk join mp4 videos per the following

sudo crontab -e

Add the following line to the bottom of the crontab file to execute convid.sh join periodically. This can be changed to run on whatever schedule you like. It is recommended that you run once or twice a day.

To run convid.sh join 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

Use convid.sh join command to automate bulk joining per instructions above

Configure rclone to automate upload of mp4 videos

You can configure an rclone script to upload mp4 files. See rclone script variables to do this.
For Details See How to Setup Rclone
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