Last active
May 31, 2023 21:34
-
-
Save yuki-inaho/05d79429241fc1227c2ea2decc4a3d73 to your computer and use it in GitHub Desktop.
The script to split an image horizontally into equal parts with ffmpeg
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
| #!/bin/bash | |
| input_file="$1" | |
| N="$2" | |
| output_dir="${3:-.}" | |
| # Get image width | |
| width=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=p=0 "$input_file") | |
| # Calculate width of each segment | |
| segment_width=$((width / N)) | |
| # Get base name of input file without extension | |
| base_name=$(basename "$input_file") | |
| base_name="${base_name%.*}" | |
| # Create output directory if it doesn't exist | |
| mkdir -p "$output_dir" | |
| # Split and save segments | |
| for ((i = 0; i < N; i++)); do | |
| start_position=$((i * segment_width)) | |
| output_file="${output_dir}/${base_name}_$((i + 1)).jpg" | |
| ffmpeg -i "$input_file" -vf "crop=${segment_width}:ih:${start_position}:0" -q:v 1 "$output_file" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment