Skip to content

Commit 2a0dc99

Browse files
authored
Create video from multiple images (#10)
2 parents b961677 + 573e1bf commit 2a0dc99

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
assets/*
22
.DS_Store
3+
4+
*.log

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Sometimes you need a quick way to work with media files. For example:
2727

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

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

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

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

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

51-
```bash
52-
sh index.sh <command> <file1> <file2> <option1>
51+
```bash
52+
sh index.sh <command> <file1> <file2> <option1>
5353
```
5454

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

5959
## Table of Contents
6060

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

74+
### Convert Multiple Images to Video
75+
76+
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.
77+
78+
Usage:
79+
80+
```bash
81+
sh index.sh multiple-images-to-video hblur 12 assets/image1.png assets/image2.png assets/image3.png assets/image4.png
82+
```
83+
7384
### Convert Image to Video
7485
7586
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.

library/multiple-images-to-video.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/bash
2+
3+
# Check if the script is called with at least 4 arguments
4+
if [ "$#" -lt 4 ]; then
5+
echo "Error: Insufficient number of arguments for filenames. Need at least 2 files."
6+
echo "Usage: ./script.sh transition duration <filename1> <filename2>..."
7+
exit 1
8+
fi
9+
10+
# Store the filenames
11+
arguments=("$@")
12+
13+
# Extract filenames
14+
transition="${arguments[0]}"
15+
duration="${arguments[1]}"
16+
file_args="${@:3}"
17+
18+
IFS=' ' read -ra file_paths <<< "$file_args"
19+
20+
command="ffmpeg -y -hide_banner"
21+
for f in "${file_paths[@]}"
22+
do
23+
command="$command -loop 1 -i $f -t $duration"
24+
done
25+
26+
count=$(awk -F' ' '{print NF}' <<< "$file_args")
27+
offset=$(echo "scale=0; $duration/$count" | bc)
28+
new_offset=$offset
29+
30+
filter_complex="[0:v][1:v]xfade=transition=${transition}:duration=0.5:offset=${offset}"
31+
32+
if [ "${count}" -gt 2 ]; then
33+
initial_tag="t0"
34+
filter_complex="$filter_complex[t0];"
35+
36+
index=2
37+
new_offset=$((new_offset + offset))
38+
39+
rem_files="${@:5}"
40+
IFS=' ' read -ra rem_file_paths <<< "$rem_files"
41+
42+
for rf in "${rem_file_paths[@]}"
43+
do
44+
filter_complex="$filter_complex[${initial_tag}][${index}:v]xfade=transition=${transition}:duration=0.5:offset=${new_offset}"
45+
46+
if [ "${index}" -eq "$((count-1))" ]; then
47+
filter_complex="$filter_complex[out]"
48+
else
49+
filter_complex="$filter_complex[t$index];"
50+
initial_tag="t$index"
51+
fi
52+
53+
new_offset=$((new_offset + offset))
54+
index=$((index + 1))
55+
done
56+
else
57+
filter_complex="$filter_complex[out]"
58+
fi
59+
60+
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"
61+
$final_command

pluto_demo.gif

12.4 MB
Loading

0 commit comments

Comments
 (0)