Created
April 8, 2025 10:01
-
-
Save JohannesNE/29e7b872e3d052ec3f52514944ee333a to your computer and use it in GitHub Desktop.
Julia script to make a short looping gif playing a longer video.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using GLMakie | |
using VideoIO | |
GLMakie.activate!(inline=true) # Show plots in VSCode | |
# Open the video file | |
video = VideoIO.openvideo("sprite_fright_60x60.mp4") | |
frames = collect(video) | |
close(video) | |
Video properties | |
framerate = 10 | |
duration = 6.3 # Duration in seconds | |
total_frames = round(Int, framerate * duration) | |
n_rows = 10 | |
n_cols = 10 | |
# Setup canvas | |
fig = Figure(size = (600, 600), figure_padding = 0) | |
ax = Axis(fig[1,1], limits = (0,600,0,600), aspect = 1, backgroundcolor = :black) | |
# Can't figure out why I have to run this again to get an actual 600x600 px figure. | |
fig = Figure(size = (600, 600), figure_padding = 0) | |
ax = Axis(fig[1,1], limits = (0,600,0,600), aspect = 1, backgroundcolor = :black) | |
hidedecorations!(ax) | |
hidespines!(ax) | |
ax.yreversed = true | |
function makeframe(t, part) | |
frame_idx = floor(Int, total_frames*part + t*total_frames + 1) | |
# zero indexed row and col. | |
row = part ÷ n_rows | |
even_row = row % 2 == 0 | |
if (even_row) | |
col = part % n_cols | |
else | |
col = n_cols - (part % n_cols) - 1 | |
end | |
first_col = col == 0 | |
last_col = col == n_cols - 1 | |
img = image!(ax, rotl90(frames[frame_idx])) | |
y_start = 60 * row | |
x_start = 60 * col | |
if ((first_col && !even_row) || (last_col && even_row)) | |
# Move down | |
translate!(img, x_start, y_start + t*60) | |
elseif (even_row) | |
translate!(img, x_start + t*60, y_start) | |
else | |
translate!(img, x_start - t*60, y_start) | |
end | |
end | |
# Create and save the animation | |
record(fig, "output.gif", 0:total_frames; framerate=framerate) do i | |
empty!(ax) | |
for part = 98:-1:0 | |
makeframe(i/total_frames,part) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment