Spaces:
Running
Running
| # Input video file | |
| input_file="input_video.mp4" | |
| # Output video file | |
| output_file="output_video.mp4" | |
| # Target aspect ratio (e.g., 9:16) | |
| target_aspect_ratio="9:16" | |
| # Get the dimensions of the input video | |
| video_info=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 "$input_file") | |
| width=$(echo "$video_info" | cut -d ',' -f 1) | |
| height=$(echo "$video_info" | cut -d ',' -f 2) | |
| # Calculate the target width based on the target aspect ratio and height | |
| IFS=':' read -ra target_ratio <<< "$target_aspect_ratio" | |
| target_height=$((height)) | |
| target_width=$((height * target_ratio[0] / target_ratio[1])) | |
| # Make sure the dimensions are even | |
| if [ $((target_width % 2)) -ne 0 ]; then | |
| target_width=$((target_width + 1)) | |
| fi | |
| if [ $((target_height % 2)) -ne 0 ]; then | |
| target_height=$((target_height + 1)) | |
| fi | |
| # Calculate the cropping parameters to center the video | |
| crop_x=$(( (width - target_width) / 2 )) | |
| crop_y=0 # Center vertically | |
| # Use FFmpeg to crop and resize the video | |
| ./ffmpeg -i "$input_file" -vf "crop=$target_width:$target_height:$crop_x:$crop_y" -c:a copy -s "${target_width}x${target_height}" -y "$output_file" | |
| echo "Video cropped, resized, and saved as $output_file" | |