When working with videos that have been rotated (either in post-production or during capture), it’s important to ensure that they are displayed correctly. This is where the `-display_rotation` option comes in handy. It allows you to specify how many degrees a video should be rotated when played back, which can help prevent distorted images and other issues.
For example, let’s say we have a video file called “myvideo.mp4” that was captured with the camera rotated 90 degrees clockwise (so it looks like it’s in portrait mode). To display this video correctly on our screen, which is oriented horizontally, we can use FFmpeg to apply a rotation of -90 degrees:
# The first line calls the FFmpeg program and specifies the input file "myvideo.mp4" and the output file "output.mp4".
ffmpeg -i myvideo.mp4 -filter_complex "transpose=1" output.mp4
# The "-i" flag indicates the input file, while the "-filter_complex" flag specifies the filter to be applied to the input file. In this case, the "transpose=1" filter rotates the video by -90 degrees.
# The "output.mp4" at the end of the command specifies the name of the output file.
# Note: The "transpose=1" filter is specifically designed for rotating videos by 90 degrees clockwise. To rotate by 90 degrees counterclockwise, the filter would be "transpose=2".
In this example, the `-filter_complex` option is used to add a filter that rotates the video by 90 degrees (using the `transpose` filter). The resulting output file will be in landscape mode and can be played back without any issues.
However, if we want to display the original orientation of the video on our screen (which would require flipping it horizontally), we can use FFmpeg’s `-display_rotation` option instead:
#!/bin/bash
# This script uses FFmpeg to transpose a video file and output it in landscape mode.
# First, we specify the input file using the `-i` option, followed by the name of the file.
# Then, we use the `-filter_complex` option to apply a filter to the video.
# In this case, we use the `transpose` filter with a value of `1` to rotate the video 90 degrees clockwise.
# Finally, we use the `-c copy` option to copy the video and audio streams without re-encoding, resulting in a faster process.
# The output file is specified after the `-c copy` option.
ffmpeg -i myvideo.mp4 -filter_complex "transpose=1" -c copy output.mp4
# However, this script does not take into account the original orientation of the video.
# If we want to display the video in its original orientation, we can use FFmpeg's `-display_rotation` option instead.
# To use the `-display_rotation` option, we need to specify the input file again using the `-i` option.
# Then, we use the `-vf` option to apply the `hflip` filter, which will horizontally flip the video.
# Finally, we use the `-c copy` option to copy the video and audio streams without re-encoding, and specify the output file.
ffmpeg -i myvideo.mp4 -vf "hflip" -c copy output.mp4
In this example, the `-c copy` option is used to ensure that the video stream is not reencoded (which can result in a loss of quality). Instead, it’s simply copied over from the input file to the output file. The resulting output file will have the correct orientation for display on our screen, but without any additional processing or filtering applied.
Overall, FFmpeg provides several options and filters that can be used to manipulate video metadata (such as rotation) in a variety of ways. By understanding how these tools work and when they should be used, you can ensure that your videos are displayed correctly on any screen or device.