Code Monkey home page Code Monkey logo

php-timelapse-creator's Introduction

Timelapse Editor

This project is basically a PHP script that takes a sequence of photos, sorts them by their EXIF date, and creates a timelapse video from them. The project was built in macOS, but should work just as well on all platforms that can install and run PHP and ffmpeg.

Requirements

  • PHP 8 - Install with Homebrew on macOS (brew install php). PHP 7.x also works great.
  • PHP Composer - Install with Homebrew on macOS (brew install composer)
  • ffmpeg (with x264/x265 support) - Install with Homebrew on macOS (brew install ffmpeg)
  • (optional) Imagemagick - Install with Homebrew on macOS (brew install imagemagick)
  • (optional) imagick extension for PHP - Install using pecl (pecl install imagick)
  • Quite a lot of available disk space...

The script is built to prefer using the imagick extension for the image editing (= MUCH better quality), but it will work-ish with PHP's built-in GD as well. The script will check for the imagick extension, and will fallback to using GD. Also, note that the imagick extension requires Imagemagick to be installed as well in order to work.

Disk space

A general rule of thumb is to make sure to have available disk space that is 5 times the size of the folder containing the source images before running this script. It is possible to configure the script to use multiple folder locations, residing on different disks.

Installation and usage

  • Clone or download the source files, then run composer install to install the necessary libraries
  • Put timelapse images into the 'images-src' folder
  • (optional) Copy the file 'settings.env.example' to 'settings.env' and do your changes (look below for info)
  • Run the script in the Terminal (php create_timelapse.php).

It is probably wise to test with a small number of images untill you get the result you want. Also,

The script will do the following:

0. Initialising

At first, the script will create the folders it needs, and empty the temporary folders before doing the actual work. By temporary folders, we mean the folder for the processed images (default: 'tmp-processed') and the image sequence folder (default: 'tmp-sequence'). They will also be emptied during after creating the image sequence and after creating the video, to conserve disk space.

1. Process the images

The script will first scan through all source photos and read the EXIF info contained in them. If it isn't a duplicate, the photo will put the date and time it was taken (from EXIF) superimposed in the lower right corner, while the logo.png will be placed in the top left corner, with a 40% transparency. PS! There are lots of other editing possibilities here, but this is what was needed in this project.

The resulting image will be placed in the folder for processed images (default: 'tmp-processed'), with it's EXIF date as filename (YYYY-MM-DD-HHMMSS).

There is also the option to skip EXIF and use the picture filename or also its modification time to make the sequence.

If duplicates are encountered during processing, they are skipped from processing (but not deleted from the source folder).

And yes, thousands of 1080p images will take up quite a lot of space. It is wise to have the temporary folders residing on a fast disk with lots of free space available.

2. Create the image sequence

After this, the script will scan the processed image folder, and create a photo sequence by copying the files to the 'tmp-sequence' folder. Their filenames will be in the pattern 'seq-########.jpg', the oldest will be 'seq-00000001.jpg' and so on and so on. This will make it possible for ffmpeg to create the video with the correct frame sequence.

3. Create the video

By default, the script will create a HEVC file with a Constant Rate Factor of 30 (≈ 5,7 Mbit/sec bitrate). The filename will also contain the date of the last photo in the sequence.

The resulting video will be placed in the video folder, with a name containing the date of the last image taken.

Settings

All configuration can be done in the file settings.env. All default settings are available in the file settings.env.example, so a good routine is to copy settings.env.example to settings.env before running the script. You may also create a settings.env file containing only the settings you want to change. Most of the default settings will work fine as they are, even without creating a settings.env file at all. The most likely candidates for changes are IMAGE_FONT and possibly the IMAGE_ROTATE settings.

Overview

  • FILEDATE_SRC - method used to get the image timestamp. Default: "exif". You may also use "filename" if the filename itself is a timestamp, or "filemod" to extract the timestamp from the file's modification time.
  • FILEDATE_FORMAT - (Only used if FILEDATE_SRC is 'filename') The date format used in the filename. Default: "Y-m-d_His"
  • IMAGE_FONT - Path to the font file to use for the superimposed text. The default value will work only on macOS. You may also use integer values 1-5 to use the GD built-in fonts, as documented on the image library website. A pro tip is to test this with a few images before you do the full 100000 image run. Default: "/System/Library/Fonts/Arial.ttf"
  • IMAGE_DRIVER - Which driver to use whith image processing, default "imagick". Will use "gd" automatically if imagick is not available
  • IMAGE_FONTSIZE_DATE - Font size to use on the superimposed date. Default: 26
  • IMAGE_FONTSIZE_TIME - Font size for the superimposed time. Default: 40
  • IMAGE_DATE_FORMAT - Date format to use, based on the DateTime::format specification. Default: "d.m.Y", eg. "01.11.2019".
  • IMAGE_TIME_FORMAT - Time format to use, based on the same specs. Default: "H:i:s", eg. "19:42:08"
  • IMAGE_NO_LOGO - Makes it possible to disable the use of a superimposed logo in the top left corner. Set to 1 to disable. Default: 0
  • IMAGE_LOGO - The logo file (PNG preferred) to superimpose. Default: 'logo.png'
  • IMAGE_LOGO_HEIGHT - The final size of the superimposed logo is set by scaling the image to a given height, keeping the aspect ratio. Default: 200
  • IMAGE_LOGO_OPACITY - Transparency setting for the superimposed logo, in percent. 0 = invisible, 100 = opaque. Default: 40
  • IMAGE_JPEG_QUALITY - JPEG Quality for the processed images. Keep high to avoid getting a bad video result. Default: 90
  • IMAGE_ROTATE - For some reason, images originating from iPhone (iOS 12+) ends up upside-down when imported by Imagemagick. This setting will let the script rotate the image when editing it. Set to 0 to disable the rotation. Default: 1
  • IMAGE_ROTATE_SKIP - If the first X images doesn't need rotating (for instance iPhone-images from iOS < 12), set this value to X. Default: 0
  • VIDEO_FORMAT - Lets you specify the video encoding to use when making the video. You may use "x264" to enjoy much faster encoding (and much larger files), or "hevc" to use the more efficient HEVC/x265 format. There is also the choice of "hevc_vt", which uses Apple's VideoToolbox. This will make M1 Macs do a vary fast HEVC encoding. Default: "hevc"
  • VIDEO_FRAMERATE - The framerate (frames per second) of the resulting video. Default: 25
  • CRF_HEVC - Constant Rate Factor for HEVC encoding. Higher value = smaller file. The default value results in a bitrate ≈ 5-6 Mbit/s. Default: 30
  • CRF_x264 - Constant Rate Factor to use for x264 encoding. The default value has about the same video quality as a HEVC CRF of 30, but results in a much larger file (bitrate ≈ 14 Mbit/s). Default: 25
  • BR_VIDEOTOOLBOX - VideoToolbox does not support CRF. So if you use "hevc_vt", bitrate must be given in kilobits. Default: "6000K"
  • PATH_SRC - Path to the source folder, where the original images are stored, without trailing "/". Default: "images-src"
  • PATH_IMG - Where to put images after superimposing the timestamp (and logo). Default: "images"
  • PATH_SEQ - Where the numbered images are stored. This is where ffmpeg finds the image sequence it needs to produce the video. Default: "images-sequence"
  • PATH_VIDEO - Where the finished video ends up. Default: "video"
  • KEEP_TMP_PROCESSED/KEEP_TMP_SEQUENCE - Normally, the script will delete the temporary folders during and after processing to conserve disk space. If set to 1, these two settings will skip the deletion. The temporary folders will be emptied the next time the script is run, though. Default: 0

php-timelapse-creator's People

Contributors

eirikwulff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

php-timelapse-creator's Issues

Thank you for this

Hey, just wanted to say thank you for this. It matched exactly what I needed for a sky camera project I'm creating. Without having to depend on some external overpriced website.
I was just thinking recently to ask about updating some of the dependencies and I see you just did.
cheers,
David

Can I use it without composer?

Hi, thanks for your work! I would like to know if there is a way to use the script without installing composer.
I need to use this script in shared hosting and unfortunately I can't use Terminal or install extensions.
Thanks!

Not Creating Video

I am not sure but its not creating the video file. I am not sure if i am making any mistake. Can you pls guide me on this? Thx

   Welcome to the Timelapse Editor 0. Initialising... >> Using the default GD driver to process images (oh, no...) Using font file 'd:\wamp\www\yousuf\vfm\timelapse\arial_th\ArialTh.ttf' 1. Processing files... images-src/Canon_40D.jpg -> tmp-processed/2008-07-31-103811.jpg images-src/Canon_40D_photoshop_import.jpg -> tmp-processed/2008-07-31-100549.jpg images-src/Canon_DIGITAL_IXUS_400.jpg -> tmp-processed/2008-07-31-171501.jpg images-src/Canon_PowerShot_S40.jpg -> tmp-processed/2003-12-14-120144.jpg images-src/Fujifilm_FinePix6900ZOOM.jpg -> tmp-processed/2008-07-31-171756.jpg images-src/Fujifilm_FinePix_E500.jpg -> tmp-processed/2008-07-31-164910.jpg images-src/Kodak_CX7530.jpg -> tmp-processed/2008-07-31-103926.jpg images-src/Konica_Minolta_DiMAGE_Z3.jpg -> tmp-processed/2008-07-31-164537.jpg images-src/Nikon_COOLPIX_P1.jpg -> tmp-processed/2008-07-31-174303.jpg images-src/Nikon_D70.jpg -> tmp-processed/2008-07-31-100344.jpg images-src/Olympus_C8080WZ.jpg -> tmp-processed/2008-07-31-130347.jpg images-src/PaintTool_sample.jpg -> tmp-processed/2023-09-25-113243.jpg images-src/Panasonic_DMC-FZ30.jpg -> tmp-processed/2008-07-31-163924.jpg images-src/Pentax_K10D.jpg -> tmp-processed/2008-07-31-155649.jpg images-src/Reconyx_HC500_Hyperfire.jpg -> tmp-processed/2023-09-25-113244.jpg images-src/Ricoh_Caplio_RR330.jpg -> tmp-processed/2008-07-31-173621.jpg images-src/Samsung_Digimax_i50_MP3.jpg -> tmp-processed/2008-07-31-190337.jpg images-src/Sony_HDR-HC3.jpg -> tmp-processed/2008-07-31-172021.jpg images-src/WWL_(Polaroid)_ION230.jpg -> tmp-processed/2008-07-31-173206.jpg images-src/corrupted.jpg -> tmp-processed/2015-09-08-110217.jpg images-src/long_description.jpg -> tmp-processed/2008-07-31-105000.jpg Processed 21 files, saved 21, skipped 0 duplicates 2. Creating the image sequence tmp-processed/2003-12-14-120144.jpg -> tmp-sequence/seq-00000001.jpg tmp-processed/2008-07-31-100344.jpg -> tmp-sequence/seq-00000002.jpg tmp-processed/2008-07-31-100549.jpg -> tmp-sequence/seq-00000003.jpg tmp-processed/2008-07-31-103811.jpg -> tmp-sequence/seq-00000004.jpg tmp-processed/2008-07-31-103926.jpg -> tmp-sequence/seq-00000005.jpg tmp-processed/2008-07-31-105000.jpg -> tmp-sequence/seq-00000006.jpg tmp-processed/2008-07-31-130347.jpg -> tmp-sequence/seq-00000007.jpg tmp-processed/2008-07-31-155649.jpg -> tmp-sequence/seq-00000008.jpg tmp-processed/2008-07-31-163924.jpg -> tmp-sequence/seq-00000009.jpg tmp-processed/2008-07-31-164537.jpg -> tmp-sequence/seq-00000010.jpg tmp-processed/2008-07-31-164910.jpg -> tmp-sequence/seq-00000011.jpg tmp-processed/2008-07-31-171501.jpg -> tmp-sequence/seq-00000012.jpg tmp-processed/2008-07-31-171756.jpg -> tmp-sequence/seq-00000013.jpg tmp-processed/2008-07-31-172021.jpg -> tmp-sequence/seq-00000014.jpg tmp-processed/2008-07-31-173206.jpg -> tmp-sequence/seq-00000015.jpg tmp-processed/2008-07-31-173621.jpg -> tmp-sequence/seq-00000016.jpg tmp-processed/2008-07-31-174303.jpg -> tmp-sequence/seq-00000017.jpg tmp-processed/2008-07-31-190337.jpg -> tmp-sequence/seq-00000018.jpg tmp-processed/2015-09-08-110217.jpg -> tmp-sequence/seq-00000019.jpg tmp-processed/2023-09-25-113243.jpg -> tmp-sequence/seq-00000020.jpg tmp-processed/2023-09-25-113244.jpg -> tmp-sequence/seq-00000021.jpg 3. Creating HEVC video... === T H E E N D === 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.