Skip to content

Commit

Permalink
Create video from multiple images (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
correa-brian authored Jun 3, 2023
2 parents b961677 + 573e1bf commit 2a0dc99
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
assets/*
.DS_Store

*.log
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Sometimes you need a quick way to work with media files. For example:

If you find yourself working with media files often, it may be helpful to have a library to automate your workflows.

![GIF of Pluto demo](https://cosmos-media-content.s3.amazonaws.com/public-content/pluto_demo.gif)
<img src="pluto_demo.gif?raw=true">

To use the library, follow the prerequisites below and run from your terminal.

Expand All @@ -48,8 +48,8 @@ To get started, check out the table of contents below and follow these steps:

1. Run the name of the command from your terminal like so:

```bash
sh index.sh <command> <file1> <file2> <option1>
```bash
sh index.sh <command> <file1> <file2> <option1>
```

1. Check the `assets` directory for the created file
Expand All @@ -58,6 +58,7 @@ To get started, check out the table of contents below and follow these steps:

## Table of Contents

- [Convert multiple images to video](#convert-multiple-images-to-video)
- [Convert image to video](#convert-image-to-video)
- [Convert image to WebP](#convert-image-to-webp)
- [Convert PDF to video](#convert-pdf-to-mp4)
Expand All @@ -70,6 +71,16 @@ To get started, check out the table of contents below and follow these steps:
- [Extract frames from a video](#extract-frames-from-a-video)
- [Modify video speed](#modify-video-speed)

### Convert Multiple Images to Video

This command can encode multiple image files into an MP4 video file. Pass in the desired transition (you can find all the available options in the [FFmpeg documentation](https://trac.ffmpeg.org/wiki/Xfade)), duration, and files you'd like to join.
Usage:
```bash
sh index.sh multiple-images-to-video hblur 12 assets/image1.png assets/image2.png assets/image3.png assets/image4.png
```
### Convert Image to Video
This command can encode an image file into an MP4 video file. Pass in the desired frame rate and duration. Frame rate will default to 24 frames per second and duration will default to 10 seconds.
Expand Down
61 changes: 61 additions & 0 deletions library/multiple-images-to-video.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

# Check if the script is called with at least 4 arguments
if [ "$#" -lt 4 ]; then
echo "Error: Insufficient number of arguments for filenames. Need at least 2 files."
echo "Usage: ./script.sh transition duration <filename1> <filename2>..."
exit 1
fi

# Store the filenames
arguments=("$@")

# Extract filenames
transition="${arguments[0]}"
duration="${arguments[1]}"
file_args="${@:3}"

IFS=' ' read -ra file_paths <<< "$file_args"

command="ffmpeg -y -hide_banner"
for f in "${file_paths[@]}"
do
command="$command -loop 1 -i $f -t $duration"
done

count=$(awk -F' ' '{print NF}' <<< "$file_args")
offset=$(echo "scale=0; $duration/$count" | bc)
new_offset=$offset

filter_complex="[0:v][1:v]xfade=transition=${transition}:duration=0.5:offset=${offset}"

if [ "${count}" -gt 2 ]; then
initial_tag="t0"
filter_complex="$filter_complex[t0];"

index=2
new_offset=$((new_offset + offset))

rem_files="${@:5}"
IFS=' ' read -ra rem_file_paths <<< "$rem_files"

for rf in "${rem_file_paths[@]}"
do
filter_complex="$filter_complex[${initial_tag}][${index}:v]xfade=transition=${transition}:duration=0.5:offset=${new_offset}"

if [ "${index}" -eq "$((count-1))" ]; then
filter_complex="$filter_complex[out]"
else
filter_complex="$filter_complex[t$index];"
initial_tag="t$index"
fi

new_offset=$((new_offset + offset))
index=$((index + 1))
done
else
filter_complex="$filter_complex[out]"
fi

final_command="$command -filter_complex $filter_complex -r 30 -c:v libx264 -map [out] -pix_fmt yuv420p -t $duration -preset superfast assets/slide_show.mp4"
$final_command
Binary file added pluto_demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2a0dc99

Please sign in to comment.