From charlesreid1

Line 88: Line 88:
<pre>
<pre>
$ mkdir my_timelapse/
$ mkdir my_timelapse/
$ /bin/ls -1 | grep jpg > files
$ awk 'NR % 2 {print} !(NR % 2) && /pattern/ {print}' files | xargs -I% mv % my_timelapse/.
$ awk 'NR % 2 {print} !(NR % 2) && /pattern/ {print}' files | xargs -I% mv % my_timelapse/.
$ # optional: remove all other images
$ # optional: remove all other images

Revision as of 01:02, 31 July 2016

Overview

Procedure

The procedure for post-processing videos looks like this:

  • Obtain and wrangle a large number of sequentially numbered jpeg files (xargs)
  • Turn mass of jpeg files into video (ffmpeg)
  • Downsample (xargs/unix)
  • Figure out what effects to apply - single jpeg file (lightroom)
  • Apply desired effects en-masse - all jpeg files (lightroom)
  • Make more videos (ffmpeg)

Tools

ffmpeg: One of the most useful tools for post-processing video is ffmpeg. There are some notes on ffmpeg on the wiki already: Ffmpeg. Most of the material here will be a variation on that.

xargs: Another useful tool for post-processing video is xargs. Notes on xargs on the wiki already: Xargs. This is a unix command-line utility that forks a single process or single command to multiple inputs. This is extremely useful to streamlining image processing, which can be done in parallel. (It is also useful for mass-renaming files.)

ImageMagick: the command-line "convert" tool is the interface for Image Magick, and this is an extremely handy tool if you need to do any mass-manipulation of images from the command line - for example, shrinking, cropping, stretching, filtering, masking, sharpening, blurring. You name it, you can probably do it with ImageMagick. When combined with convert and xargs, you become unstoppable.

There are also some interesting side topics, such as image averaging using the Python_Imaging_Library. This allows for smoother, more stretched-out timelapse videos.

Wrangling Large Numbers of Files

When I finished with my first timelapse, I was left with 16,636 jpg files over an approximately 9 hour period. That's a pretty unwieldy number of files, and at 200 KB a pop, it's also a lot of disk space. That means stitching together movies, applying effects, &c will take a long time.

Normally, I would turn all the photos into a "baseline" movie - one that shows everything, and makes it easy to narrow in on the interesting parts. However, with 16,000 files, this would be a long, big movie. I decided to throw out half the photos (can get more later).

Getting the filename list

With 16,000 files, if you try and run ls *.jpg, ls will raise an error (too many arguments). Instead, just list everything (no arguments), and search for jpg files:

/bin/ls -1 | grep jpg > files

Now if you examine files you'll see the interval between photos is very erratic - it was programmed to be 2 seconds, but it is all over the place - sometimes 3, sometimes 2.

I wanted to throw out every other photo, and I couldn't do it based on the digits or the numbering. The intervals were uneven, and there were multiple hours of photos. So, I decided to just cut out every other photo.

Chopping up the filename list

To remove every other file, I needed to feed the output of the above command, ls -1 | grep jpg, to a program that would print out every other line - good task for a one-liner in sed or awk.

Here's an awk one-liner to print only odd lines (only the even lines in awk, which numbers lines starting at 0):

awk 'NR % 2 {print} !(NR % 2) && /pattern/ {print}'

Here's that script in action:

$ cat multiline
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10

$ cat multiline | awk 'NR % 2 {print} !(NR % 2) && /pattern/ {print}'
line1
line3
line5
line7
line9

Now that can be applied to an external file,

$ awk 'NR % 2 {print} !(NR % 2) && /pattern/ {print}' files

This prints out a list of files to keep. The easiest way to do this is probably to make a directory to hold the downsampled photos, and (if desired) delete the remaining photos. To move the list of photos obtained in the prior step, you can use xargs to feed the filenames to the "mv" command:

xargs -I% mv % my_timelapse/.

To stitch it all together, save your odd-numbered files in a directory, and remove all the other remaining photos:

$ mkdir my_timelapse/
$ /bin/ls -1 | grep jpg > files
$ awk 'NR % 2 {print} !(NR % 2) && /pattern/ {print}' files | xargs -I% mv % my_timelapse/.
$ # optional: remove all other images
$ # rm *.jpg

That awk command starts with the list of all the files (the "files" file), then parses it down to every other one, then passes that reduced list on to xargs.

Before:

$ ls -1 | wc -l
    16636

After:

$ ls -1 | wc -l
    8320

and counting the number of downsampled photos:

$ ls -1 my_timelapse | wc -l
    8318

Applying Effects

Single photo

En masse

Creating Video from Image Files

These notes are based on the notes at the Ffmpeg page.

Renaming Files

One of the quirks of ffmpeg is that, if you are converting a large number of files, they have to be sequentially numbered, starting with 0 OR 1.

Test Run

The best way to do this is to put all the photos you want to add to your timelapse into one big folder, in the order you want them. Then do a /bin/ls -1 to list them all in order. Then use a bash for loop to name each file in a sequential fashion: 0001.jpg, 0002.jpg, etc.

#!/bin/sh

i=1
for file in `/bin/ls -1 *.jpg`; do
    filestrip=$(echo "$file" | sed 's/\.jpg//')
    newfile=$(printf "${filestrip}_%04d.jpg" "$i")
    echo "mv ${file} ${newfile}"
    i=$((i+1))
done

The output of this file, run on a dummy directory with 8 dummy files (note that it preserves the original filename, without the extension)

$ ./doit.sh
mv f1.jpg f1_0001.jpg
mv f2.jpg f2_0002.jpg
mv f3.jpg f3_0003.jpg
mv f4.jpg f4_0004.jpg
mv f5.jpg f5_0005.jpg
mv f6.jpg f6_0006.jpg
mv f7.jpg f7_0007.jpg
mv f8.jpg f8_0008.jpg

Real Deal

When it was time to run the real script, I copied the above script into the directory containing the downsampled pile of 8,000 photos, and made one modification: the line that actually EXECUTES, not just PRINTS, the mv command.

#!/bin/sh

i=1
for file in `/bin/ls -1 *.jpg`; do
    filestrip=$(echo "$file" | sed 's/\.jpg//')
    newfile=$(printf "${filestrip}_%04d.jpg" "$i")
    echo "mv ${file} ${newfile}"
    mv ${file} ${newfile}
    i=$((i+1))
done

Ran it. Took almost a minute, but it renamed all the files like a charm.

File:RenamingSuccess.png

Flags