# Time distortion videos made with slit-scan technique
[[_TOC_]]

This is an experimental project about converting footage into a slit-scan video. For this type of video transfer i am using the term "time distortion". Because every scanline or pixel in the transformed video originates from a different point in time.

There is a sibling of this technique. Converting the original footage into a still image. Every pixel in the transformed image has its origin in an different point in time. You can find this type of open source tool here: https://gitlab.com/metagrowing/slitscan

There is a lack of precise terminology to distinguish between these two workflows. What they have in common is that they primarily manipulate the timeline. From an aesthetic standpoint, however, these two methods produce different results. A rendering method that yields an appealing result for a still image usually falls short when used to produce a video. And vice versa.

## Time distortion and slit-scan videos

* Record a image sequence.
  * If possible record a video at high frame rate
  * If you use video footage as source disassemble the movie frame by frame to generate a lot of image files. Its good to have some hundreds images containing a single frame each.
  * Or take lots of photos over a long time.
* Then load these image into the TimeDistortion application.
* Select one ore more processing strategies and press the Transform button.
* For every processing strategy you will get a folder filled with image files. These are the distorted images.
* You can combine these images with FFmpeg or Kdenlive into a video.



## Software used for simple time distortion videos

* FFmpeg to split footage to single images.
* Kdenlive or FFmpeg to combine an image sequence into a movie file.
* TL-0.1.0.jar A OpenJDK program to cut small stripes out of images and arrange them to a new image.
* (optional) GNU Wget to capture images from web cams.
* (optional) Fswebcam a tiny and flexible webcam command-line program for capturing
images from a V4L1/V4L2 device.


## Original footage
You need a lot of image files having sortable file names.

All image file names should have an common prefix.

```bash
find split | sort
  split/clip_00001.png
  split/clip_00002.png
  split/clip_00003.png
  ...
  split/clip_01500.png
```

All Images must be identical in width and height.

## How to capture or pre-process the original footage
One way is splitting a video file into lots of single frames.

`moviesplit avideo.mov`

```bash
#!/bin/bash
mkdir split
cd split
ffmpeg -i ../$1 clip_%05d.png
```

Or capture images from a public WEB-cam.

`public-web-cam.sh`

```bash
#!/bin/bash
mkdir split
for i in `seq 3000`
do
filenumber=$(printf '%05d\n' $i)
wget -O split/clip_$filenumber.jpg --tries=2 https://webcam.your-favorit-cam.com/current.jpg
sleep 60
done
```

Or capture images from a V4L1/V4L2 compatible USB camera.

`local-cam.sh`

```bash
#!/bin/bash
mkdir split
VIDEODEVICE=video1
#uvcdynctrl --device $VIDEODEVICE --set "Power Line Frequency" 1
#uvcdynctrl --device $VIDEODEVICE --set  Sharpness 7
#uvcdynctrl --device $VIDEODEVICE --set "Focus, Absolute" 129
#uvcdynctrl --device $VIDEODEVICE --set "Brightness" 0
#sleep 1
for i in `seq 10`
do
filenumber=$(printf "%05d" $i)
fswebcam -d /dev/$VIDEODEVICE -r 3264x2448 split/clip_$filenumber.jpg
sleep 5
done
```

## Download the application and adjust the startup script.

1. Download sources and binaries from https://metagrowing.org
```bash
wget https://metagrowing.org/time-distortion/timedistortion-0.1.0.tar.gz
```

2. Unpack this tar.gz archive.

3. Adjust the startup script `td.sh`

In particular, the values for OUTPATH and MEMSIZE.

```bash
#!/bin/bash

JARFILE=TD-0.1.0.jar
OUTPATH=~/td_exp
STARTSWITH=clip_
ENDSWITH=.png
MEMSIZE="-Xmx24g"

java $MEMSIZE -jar $JARFILE --path $IMGPATH \
                            --startswith $STARTSWITH \
                            --endswith $ENDSWITH \
                            --out $OUTPATH
```

## Time Distortion as post process
1. Split a movie in single frames or capture lots of images. See above.

2. Start the TD user interface. `./TD.sh`

![](./doc/TimeDistortion-UI.png)

3. Use the `Load` button to select an input folder. This is the folder where the image files of the original movie can be found.

4. Select one or more transformers **and** one or more time-mappers.

5. Press the `Transform` button.

6. You can find the generated images in sub-folders of your `--out` configuration.


## Memory management and performance
TD uses image files as input. Formats with lossless compression are best. The frames extracted from 30 seconds of film material can occupy some GB of RAM when loaded.

These strategies are helpful:
* The image files should be stored on an SSD drive.
* The size of the heap of the JVM should be set large enough. But avoid swapping to disk.
* A heap that is too small will cause the garbage collector to require too much computing time. Increase `-Xmx`
* If the heap is not large enough, an OutOfMemory exception will be thrown.  Increase `-Xmx`
