Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

maxoakland

macrumors 65816
Original poster
Oct 6, 2021
1,015
1,449
I want to automate the process of checking if a video is horizontal (wider than it is tall) and if so, rotate it. Is that possible with AppleScript?

It's part of a larger AppleScript so I'd rather not use Automator or Shortcuts, but I would be willing if I have to.

I'm already using a shell script with FFMPEG so if it's possible to check this and then rotate it in the shell script, that would also be good
 
what do you prefer? 😀 exiftool -Orientation -S your-video.mp4 outputs the orientation in the shell, you can use that for the rotation and reencoding in ffmpeg.

Any decent video player will allow you to rotate after opening/while playing - maybe you like to provide some more info of what you try to acchieve?
 
  • Like
Reactions: maxoakland
Should be no problem using AppleScript to execute shell commands. One issue would be how to determine which way the video should be rotated (right/left).
 
  • Like
Reactions: maxoakland
Should be no problem using AppleScript to execute shell commands. One issue would be how to determine which way the video should be rotated (right/left).
I ended up figuring it out. For anyone who might want to do it in the future:

AppleScript:
log "Checking width and height…"

do shell script "

# Ensure ffmpeg and ffprobe are in the PATH;
export PATH=/usr/local/bin:$PATH;

# Navigate to the working directory;
cd 'Path/to/folder/';

# Define the video file path;
video_file='/Path/to/video';

# Use ffprobe to extract video width and height;
dimensions=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0:s=x $video_file);

# Parse the dimensions to get width and height;
width=$(echo $dimensions | cut -d'x' -f1);
height=$(echo $dimensions | cut -d'x' -f2);


# Debugging: Display width and height;
echo 'Width: $width';
echo 'Height: $height';

# Check if width and height are valid numbers;
if ! [[ $width =~ ^[0-9]+$ ]] || ! [[ $height =~ ^[0-9]+$ ]]; then

 echo 'Error: Could not retrieve video dimensions.';
exit 1;
fi;

# Compare width and height;
if [ $width -gt $height ]; then

# Do wider than tall rotation here;
echo 'Processing wider than tall video...';
ffmpeg -y -display_rotation:v:0 -90.0 -I $video_file -i audio.wav -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 -shortest './Temp/new_video_temp_name.mov';

elif [ '$width' -lt '$height' ]; then

#Process taller than wide video with no rotation
echo 'Processing taller than wide video...';
ffmpeg -y -display_rotation:v:0 0.0 -i $video_file -i audio.wav -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 -shortest './Temp/new_video_temp_name.mov'

else

# Handle square video;
echo 'Processing square video...';
ffmpeg -y -display_rotation:v:0 0.0 -i $video_file -i audio.wav -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 -shortest './Temp/new_video_temp_name.mov'

fi;
"

Note that if you copy & paste this for your own use, you'll have to change these things to get it to work:

• Change 'Path/to/folder/' to the path to the actual folder you want to work in. Use single quotes
• Put a folder called Temp and a folder called Finished in that folder
• Change /Path/to/video to the real path to the folder you want to work in. Use single quotes
 
Last edited:
If a newer OS, Shortcuts can do it as well (disclosure: have not coded up something like this myself).

Ignore the sequence in first image: just wanted to put the three relevant Actions on the screen.

Bonus with this: can also use on i*OS devices.

i1.png

i2.png
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.