Skip to content

Instantly share code, notes, and snippets.

@mmarkus13
Forked from ViktorNova/rotate-video.sh
Created February 7, 2022 14:48
Show Gist options
  • Select an option

  • Save mmarkus13/6bf41e2772a293fa5819bad7a1ef6e5e to your computer and use it in GitHub Desktop.

Select an option

Save mmarkus13/6bf41e2772a293fa5819bad7a1ef6e5e to your computer and use it in GitHub Desktop.
Rotate a video with FFmpeg (100% lossless, and quick)
$INPUTVIDEO='input.mp4'
$OUTPUTVIDEO='output.mp4'
ffmpeg -i $INPUTVIDEO -metadata:s:v rotate="-90" -codec copy $OUTPUTVIDEO
@mmarkus13

Copy link
Copy Markdown
Author

A little overkill; but I've made a 'shortcut' for the above; whereas you only have to call the script and specify the file in an argument.

Example usage:
rotate90 myVideo.MP4

  • The shortcut can be called from anywhere
  • Saves output to the current working folder
    (using the example above: from myVideo.MP4 -> myVideo+90.MP4)

In order to be able to use the above 'shortcut':

Copy, paste and run the following within your terminal (once):

### [COPY & PASTE all the contents below into Linux (Debian) cmd] ###


# update repositories & packages

sudo apt-get update -y
sudo apt-get upgrade -y


# install ffmeg

sudo apt install ffmpeg -y


# create a shortcut script for video rotation

SDir=/home/$USER/scripts/
mkdir -p "$SDir"

EOF				
cat << "EOF" >  ${SDir}rotate90
INPUTVIDEO="$1"
OUTPUTVIDEO="${INPUTVIDEO%.*}+90.MP4"

ffmpeg -i "$INPUTVIDEO" -metadata:s:v rotate="90" -codec copy "$OUTPUTVIDEO"
EOF

chmod 750 -R "$SDir"


# update ~/.bashrc (to be able to call the script from anywhere without having to use "./" as prefix or need full PATH)

echo -e 'export PATH=$PATH:/home/'"$USER/scripts" >> ~/.bashrc

To change the rotation angle edit the script /home/$USER/rotate90 4th line variable rotate="90"'s value to the desired one.
( ex.: -270; -90; 180; ... )

@mmarkus13

Copy link
Copy Markdown
Author

rotate90ALL:

for file in ./*.MP4

do
    rotate90 "$file"
done

@mmarkus13

Copy link
Copy Markdown
Author

I was too lazy to copy the already rotated files to a new folder;

  • so I've modified the above script to ignore these files (and automatically skip asking me if I want to overwrite each already rotated file...).

while true; do echo no; done | for file in $(ls | grep -i ".MP4" | grep -v "+90"); do rotate90 "$file"; done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment