Skip to content

Instantly share code, notes, and snippets.

@Cyberes
Last active December 15, 2022 08:11
Show Gist options
  • Save Cyberes/75e515fd425e1defc634be77f11572de to your computer and use it in GitHub Desktop.
Save Cyberes/75e515fd425e1defc634be77f11572de to your computer and use it in GitHub Desktop.
# A simple script to create a slideshow from images in a directory. Put all your images in `input/`
# Delete the old images.txt
rm images.txt
# I used this to convert all my png files to images
# mkdir old
# for f in input/*.png; do
# echo "$f"
# mogrify -format jpg "$f"
# mv "$f" old
# done
# Put all the image files into a text file that will be fed into ffmpeg
for f in input/*.jpg; do
printf '%s\n' "file '$f'" >> images.txt
done
# Sort it
sort -R images.txt > temp
mv temp images.txt
BPM=1.65 # use a BPM finder tool online to find the BPM
MODIFIER=1 # this is a modifier to multiply the BPM
# Calculate duration of video from number of images and framerate
input_fps=$(echo "$BPM * $MODIFIER" | bc -l) # BPM of song multiplied by a modifier to speed it up
total_images=$(cat images.txt | wc -l)
duration=$(printf %.2f $(echo "$total_images / $input_fps" | bc -l)) # get the duration in minutes of the video
duration_ffmpeg=$(printf '%02d:%02d:%02d' $(echo "$duration/3600" | bc) $(echo "$duration%3600/60" | bc) $(python3 -c "print(int($duration%60))")) # format the calculated duration into hours:minutes:seconds for ffmpeg
echo "Duration of video in hours:minutes:seconds -> $duration_ffmpeg"
# -stream_loop -1 -i 'i_miss_the_rage_loop.mp3' # this is to loop the audio. better to concact it instead to make it smoother
ffmpeg -loglevel error -stats -y -f concat -safe 0 -r $input_fps -i "images.txt" -start_number 1 -c:v libx264 -c:a aac -codec:a copy -vf "scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,format=yuv420p" -r 30 -movflags +faststart -t $duration_ffmpeg out.mp4
# ffmpeg -y -i out.mp4 -filter_complex [0]pad=w=20+iw:h=20+ih:x=10:y=10:color=red final.mp4 && rm out.mp4 # add a border
# This will extract a random frame from all videos in the current folder.
import os
import cv2
import random
for file in os.listdir('./webm'):
if file.endswith(".webm"):
vidcap = cv2.VideoCapture('./webm/' + file)
totalFrames = vidcap.get(cv2.CAP_PROP_FRAME_COUNT)
randomFrameNumber=random.randint(0, totalFrames)
vidcap.set(cv2.CAP_PROP_POS_FRAMES,randomFrameNumber)
success, image = vidcap.read()
if success:
cv2.imwrite('./input/' + file + ".jpg", image)
print(file + ".jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment