6
votes

I need to detect whether videos are recorded in portrait or landscape mode, then transpose those into the correct orientation, in a scripted fashion.

if [ "$v_orient" ==  "landscape" ]
  then
    ffmpeg -i file.mp4 -vf "transpose=1" file.ogv
  else 
    ffmpeg -i file.mp4 file.ogv
fi

I've looked in ffmpeg online documentation and googled around,

I've attempted exiftool

exiftool -Rotation -Rotate file.mp4

However, this outputs Rotate: 90 for both landscape & portrait videos I have.


How can I detect the video orientation in bash?

2

2 Answers

8
votes

ffprobe is part of the ffmpeg package and it will report on video metadata.

ffprobe somevideo.mp4

grep can be used to select just the rotation information. On a rotated video, I see this output:

$ ffprobe somevideo.mp4 2>&1 | grep rotate
      rotate          : 90

On unrotated videos that I tried, the above command produced no output.

This command depends on video file's metadata. If you have a source that does not produce a reliable rotate tag, you may need to look for other tags.

Old cameras that do not have accelerometers (gravity sensors) cannot tell if they are held in portrait or landscape orientation and hence cannot produce any useful tag.

1
votes

I found out that ffprobe itselfs offers a way to fetch single meta data values, so we can easily fetch the value of rotation directly without having to use grep on the output:

rotate=$(ffprobe -v error -select_streams v:0 -show_entries stream_tags=rotate -of csv=s=x:p=0 "${sourceFile}")
if [ "$rotate" != "" ]; then 
    echo "We need to rotate: $rotate"
fi

In portrait mode, this select call will give you just the numer of degres:

$ ffprobe ~/Videos/test.mp4 -v error -select_streams v:0 -show_entries stream_tags=rotate -of csv=s=x:p=0
90