467

I have been trying to figure out how to rotate videos with FFmpeg. I am working with iPhone videos taken in portrait mode. I know how to determine the current degrees of rotation using MediaInfo (excellent library, btw) but I'm stuck on FFmpeg now.

From what I've read, what you need to use is a vfilter option. According to what I see, it should look like this:

ffmpeg -vfilters "rotate=90" -i input.mp4 output.mp4

However, I can't get this to work. First, -vfilters doesn't exist anymore, it's now just -vf. Second, I get this error:

No such filter: 'rotate'
Error opening filters!

As far as I know, I have an all-options-on build of FFmpeg. Running ffmpeg -filters shows this:

Filters:
anull            Pass the source unchanged to the output.
aspect           Set the frame aspect ratio.
crop             Crop the input video to x:y:width:height.
fifo             Buffer input images and send them when they are requested.
format           Convert the input video to one of the specified pixel formats.
hflip            Horizontally flip the input video.
noformat         Force libavfilter not to use any of the specified pixel formats
 for the input to the next filter.
null             Pass the source unchanged to the output.
pad              Pad input image to width:height[:x:y[:color]] (default x and y:
 0, default color: black).
pixdesctest      Test pixel format definitions.
pixelaspect      Set the pixel aspect ratio.
scale            Scale the input video to width:height size and/or convert the i
mage format.
slicify          Pass the images of input video on to next video filter as multi
ple slices.
unsharp          Sharpen or blur the input video.
vflip            Flip the input video vertically.
buffer           Buffer video frames, and make them accessible to the filterchai
n.
color            Provide an uniformly colored input, syntax is: [color[:size[:ra
te]]]
nullsrc          Null video source, never return images.
nullsink         Do absolutely nothing with the input video.

Having the options for vflip and hflip are great and all, but they just won't get me where I need to go. I need to the ability to rotate videos 90 degrees at the very least. 270 degrees would be an excellent option to have as well. Where have the rotate options gone?

Michael Currie
  • 11,166
  • 7
  • 39
  • 54
jocull
  • 17,542
  • 18
  • 95
  • 139
  • Updated my answer. It looks like that filter was just added last month to source. – rwilliams Nov 22 '10 at 21:34
  • 85
    For anyone searching for a 180 degree rotation: `-vf "vflip,hflip"` – OrangeDog Jun 15 '12 at 11:31
  • 4
    I am wondering, if this can be achieved without decoding and then re-encoding the video -- the way jpegtran can losslessly rotate JPEG images... – Mikhail T. Dec 31 '13 at 03:31
  • I believe that a lot of devices support some kind of embedded rotation value. They will rotate (losslessly) automatically when decoding, but the support isn't universal. That was the problem I was trying to avoid when asking this question originally. – jocull Jan 02 '14 at 14:46
  • 2
    See here for rotation flag: http://stackoverflow.com/questions/15335073/can-i-set-rotation-field-for-a-video-stream-with-ffmpeg – Davor Josipovic Jan 18 '14 at 14:59
  • 2
    Also see the detailed answer at [How to flip a video 180° (vertical/upside down) with FFmpeg?](http://superuser.com/a/578329/110524) **Don't forget to reset any existing rotation metadata** (see link). – llogan Sep 21 '14 at 18:24
  • 1
    I just uploaded a 180° rotated video with "transpose=1,transpose=1" to vimeo. Even though I can play back the rotated video, it was rejected by vimeo. @OrangeDog's approach `-vf "vflip,hflip"` works like a charm. – Besi Feb 19 '16 at 10:30
  • This question is way off-topic for SO now, and there's a nearly identical one [on Superuser](https://superuser.com/questions/578321/how-to-rotate-a-video-180-with-ffmpeg). – Dan Dascalescu Dec 01 '17 at 03:42
  • Also see this: https://video.stackexchange.com/a/23245/23954 my video was rotated 180° and I could not understand why. – soger Oct 13 '20 at 12:58

14 Answers14

775

Rotate 90 clockwise:

ffmpeg -i in.mov -vf "transpose=1" out.mov

For the transpose parameter you can pass:

0 = 90CounterCLockwise and Vertical Flip (default)
1 = 90Clockwise
2 = 90CounterClockwise
3 = 90Clockwise and Vertical Flip

Use -vf "transpose=2,transpose=2" for 180 degrees.

Make sure you use a recent ffmpeg version from here (a static build will work fine).

Note that this will re-encode the audio and video parts. You can usually copy the audio without touching it, by using -c:a copy. To change the video quality, set the bitrate (for example with -b:v 1M) or have a look at the H.264 encoding guide if you want VBR options.

A solution is also to use this convenience script.

slhck
  • 30,965
  • 24
  • 125
  • 174
Alexy
  • 7,774
  • 1
  • 12
  • 2
  • 9
    The video will still have the orientation information so now the video will be miss corrected on the iphone. – srcspider Apr 01 '13 at 10:57
  • 2
    So in order to rotate 180 degrees I would have to convert twice with "transpose=1"? I don't want the mirroring effect from using hflip. – Usagi Jul 02 '13 at 20:28
  • 28
    When I use this command, I get a **low quality video output**, unless -- as I've just discovered -- I use this parameter as well: `-vcodec libx264`. But it would be great if I didn't need to look up which encoding to use as ffmpeg should already know it. **Any suggestions?** – Sadi Oct 03 '13 at 10:30
  • 8
    Along the same lines as Sadi, is there a way to "copy" the quality of the original video? – Alec Jacobson Oct 14 '13 at 11:49
  • 3
    I would use the `-a:c copy` flag (goes after the `-vf` flag) to copy the audio stream. My phone's camera takes video using aac, which ffmpeg doesn't support by default on my distro. – Alex Reinking Nov 30 '13 at 19:15
  • 62
    For 180 you can use `-vf "transpose=2,transpose=2"` – Alex Pliutau Dec 23 '13 at 12:43
  • 1
    @plutov.by Who transpose=2 and not transpose=1 twice? – Nicky Smits Mar 08 '14 at 17:37
  • 2
    @AlexReinking I think you meant `-c:a copy`. – Mathias Bynens Aug 15 '14 at 10:04
  • Maybe I did... ffmpeg has some pretty fluid command-line flags. I posted that comment quite a while ago. – Alex Reinking Aug 15 '14 at 15:15
  • This worked for Windows 7 64 bit machine, ffmpeg-20141221-git-41ee459-win64-shared -- fyi, ffmpeg.exe wanted to take nearly 100% of the resources, so i adjusted the affinity on ffmpeg.exe in the processes tab of windows task manager to only use 2 cores instead of 8 (right-click the process) – BitsAndBytes Dec 31 '14 at 18:26
  • @acron where we put it ? – MonsterMMORPG Feb 17 '15 at 14:49
  • @MonsterMMORPG `ffmpeg -i in.mov -qscale 0 -vf "transpose=1" out.mov` BUT read the man pages, as some codecs treat this differently. For example, when I recently worked with Theora, I had to use `-qscale 28` as it ignored `0` and wanted a 1 - 31 value. – Antony Woods Feb 17 '15 at 15:19
  • 24
    Current docs note that "Numerical values are deprecated, and should be dropped in favor of symbolic constants." http://ffmpeg.org/ffmpeg-filters.html#transpose Thus `cclock_flip`, `clock`, `cclock` or `clock_flip` instead of `0`, `1`, `2` or `3`. – l --marc l Jan 21 '16 at 00:18
  • Great Alexy, it worked for me too. I tested it in Windows 8.1 – Angie Quijano Jan 29 '16 at 22:19
  • 1
    @Sadi [transcode using same codec and params as input](http://stackoverflow.com/q/34442156/476716) – OrangeDog Feb 19 '16 at 10:34
  • If you have an updated version of ffmpeg, it autorotates the video see http://superuser.com/questions/578321/how-to-rotate-a-video-180-with-ffmpeg – stuzzo May 24 '16 at 17:22
  • "transpose=2,transpose=2" - does this mean the rotation is done twice?? Meaning it will take double the time? – McVitas Sep 18 '16 at 17:55
  • How can we achieve this in objective c code. We cant use "ffmpeg -i in.mov -vf "transpose=1" out.mov". – Kalu Khan Luhar Feb 24 '17 at 12:04
  • @KaluKhanLuhar Sure you can! You can use NSTask (or the Swift equivalent, Process). Note that this may not work in a sandboxed app. – SilverWolf Sep 02 '18 at 17:06
  • The degrees are countwise, not anticountwise, like mathematicians do – somenxavier May 10 '20 at 13:17
  • Why my video size increase after transposing? – Sisir Dec 19 '20 at 19:38
172

If you don't want to re-encode your video AND your player can handle rotation metadata you can just change the rotation in the metadata using ffmpeg:

ffmpeg -i input.m4v -map_metadata 0 -metadata:s:v rotate="90" -codec copy output.m4v
Hendy Irawan
  • 17,676
  • 10
  • 94
  • 105
Rodrigo Polo
  • 3,257
  • 2
  • 21
  • 29
  • 15
    how about check the rotate metadata first with `ffmpeg -i input.m4v 2>&1 | grep rotate` – lidaobing Feb 13 '16 at 05:10
  • this worked for me. I want to upload Clash Royale to YouTube. It rotated the video 90 degrees without affecting quality. – Sun Apr 05 '16 at 20:41
  • 73
    This is the best answer by far. But there is a small improvement to do. To avoid loosing the remaining meta-data (such as date, camera) on the video do `ffmpeg -i input.m4v -map_metadata 0 -metadata:s:v rotate="90" -codec copy output.m4v`. This way all global metadata on the input file will be copied as global metadata to output file and only the rotation meta-data is changed. – migle Aug 22 '16 at 10:26
  • 15
    If you have a portrait-mode video and just want to "UN-rotate" it to have a standard 1920x1080, then you probably want `rotate=0`. – mivk Nov 15 '16 at 19:29
  • Thank you, @mivk! – anon Mar 12 '17 at 05:20
  • 1
    Tried `-metadata:s:v rotate="180"`, didn't work. Is that supposed to work? – Ondra Žižka Jun 15 '17 at 23:24
  • 8
    IMHO best solution because no reencoding is necessary and most video player support metadata rotation. Also Cloud services like Google Photos. However, remember that ffmpeg does not necessarily copy all metadata from the original file! Therefore, I would recommend to explicitly specify to copy all other metadata from original file: `ffmpeg -i input.mp4 -codec copy -map_metadata 0 -metadata:s:v:0 rotate=0 output.mp4` – Andreas Apr 14 '18 at 10:10
  • Not a _proper_ solution since not all video players will honor the rotation. E.g. Snapchat (via the Camera Roll.) – b1nary.atr0phy Jan 30 '20 at 02:07
  • 1
    As it was stated in the answer: "If you don't want to re-encode your video AND your player can handle rotation metadata" – Rodrigo Polo Jan 31 '20 at 08:57
  • This works great for Google Photos! However it should be noted ffmpeg won't copy all metadata and will leave out some metadata that might be useful (e.g. camera model). You can copy metadata after the fact using exiftool (https://video.stackexchange.com/a/28599/34178), e.g. `exiftool -TagsFromFile input.mov "-all:all>all:all" output.mov` (note that exiftool is smart enough not to overwrite the newly added rotation metadata) – bmaupin Mar 11 '21 at 19:12
85

Have you tried transpose yet? Like (from the other answer)

 ffmpeg -i input -vf transpose=2 output

If you are using an old version, you have to update ffmpeg if you want to use the transpose feature, as it was added in October 2011.

The FFmpeg download page offers static builds that you can directly execute without having to compile them.

bmaupin
  • 11,046
  • 4
  • 71
  • 74
rwilliams
  • 20,165
  • 4
  • 46
  • 53
  • 1
    No, I have not. I didn't know it existed. I'll give that a shot. – jocull Nov 21 '10 at 01:04
  • The transpose filter does not seem to exist in any of my FFmpeg builds. How am I supposed to add it? – jocull Nov 22 '10 at 17:30
  • In the version of the docs as of 2011-05-15 the correct link is now http://www.ffmpeg.org/ffmpeg-doc.html#SEC93 – Peter Hansen May 15 '11 at 23:59
  • the answer from @Alexy seems more relevant – meduz Apr 30 '12 at 13:23
  • 8
    It feels like i got punished for offering a useful answer 1.5 years before the accepted answer. – rwilliams May 27 '13 at 09:47
  • How would this work with **more than 1 filter**? `-vf transpose=2 crop=20:20`? Because `-vf transpose=2 -vf crop=20:20` ignores the rotation (transpose). **Edit:** `-vf transpose=2 crop=20:20` did not work. – Jonas Borggren Jan 30 '15 at 13:12
  • 1
    @JonasB Separate the filters with commas. See http://stackoverflow.com/questions/6195872/applying-multiple-filters-at-once-with-ffmpeg – rwilliams Jan 30 '15 at 19:16
  • @rwilliams While this is a great answer and was 1.5 years before Alexy's answer, it has the disadvantage that it doesn't include as much information on what transpose does or how to use it. It's great that you linked to it, but the disadvantage to linking to the pertinent info is that (as in this case) the link no longer points to the proper documentation. If nothing else I'll submit an edit to fix the link. – bmaupin Mar 11 '21 at 17:20
20

To rotate the picture clockwise you can use the rotate filter, indicating a positive angle in radians. With 90 degrees equating with PI/2, you can do it like so:

ffmpeg -i in.mp4 -vf "rotate=PI/2" out.mp4

for counter-clockwise the angle must be negative

ffmpeg -i in.mp4 -vf "rotate=-PI/2" out.mp4

The transpose filter will work equally well for 90 degrees, but for other angles this is a faster or only choice.

Bijou Trouvaille
  • 6,775
  • 4
  • 34
  • 41
  • 2
    this is great. I found that it's possible to get finer-grained resolution of radians, because `*` behaves as multiplication: `ffmpeg -i in.avi -vf "rotate=-8*PI/40" -q 1 out.avi` (slightly less rotation than -PI/4 = -10*PI/40) – eqzx Apr 22 '16 at 15:41
19

I came across this page while searching for the same answer. It is now six months since this was originally asked and the builds have been updated many times since then. However, I wanted to add an answer for anyone else that comes across here looking for this information.

I am using Debian Squeeze and FFmpeg version from those repositories.

The MAN page for ffmpeg states the following use

ffmpeg -i inputfile.mpg -vf "transpose=1" outputfile.mpg

The key being that you are not to use a degree variable, but a predefined setting variable from the MAN page.

0=90CounterCLockwise and Vertical Flip  (default) 
1=90Clockwise 
2=90CounterClockwise 
3=90Clockwise and Vertical Flip
Sk8erPeter
  • 6,392
  • 9
  • 44
  • 67
Zonjai
  • 191
  • 1
  • 2
12
ffmpeg -vfilters "rotate=90" -i input.mp4 output.mp4 

won't work, even with latest source...

must change the order:

ffmpeg -i input.mp4 -vf vflip output.mp4

works fine

nano
  • 129
  • 1
  • 2
  • that is becuase you're applying the filter to the wrong file... try ffmpeg -i input.mp4 -vf "rotate=90" output.mp4, then it'll work – patrick Sep 04 '15 at 20:30
  • 3
    "Rotate video by an arbitrary angle expressed in __radians__." Documentation: http://ffmpeg.org/ffmpeg-filters.html#rotate So, radians `rotate=PI/2` or `rotate=90*PI/180` is needed – l --marc l Jan 21 '16 at 00:27
  • This is fantastic – Trect Nov 26 '19 at 15:50
8

If you're getting a "Codec is experimental but experimental codecs are not enabled" error use this :

ffmpeg -i inputFile -vf "transpose=1" -c:a copy outputFile

Happened with me for some .mov file with aac audio.

sabujp
  • 791
  • 1
  • 9
  • 11
  • This was the only example that worked for me, but the quality was terrible, and very blocky. Also, it downsized a 1080x1920 video to 352x640. I guess I'm missing a switch or two. Any suggestions? – LOlliffe Dec 03 '13 at 08:45
  • 1
    @LOlliffe add `-sameq` – Andrew Schleifer Dec 04 '13 at 16:49
  • 2
    @AndrewSchleifer Thanks. I tried that, but ffmpeg threw back at me `Option 'sameq' was removed. If you are looking for an option to preserve the quality (which is not what -sameq was for), use -qscale 0 or an equivalent quality factor option. Failed to set value '1' for option 'sameq': Invalid argument` -qscale also gives an error. `Please use -q:a or -q:v, -qscale is ambiguous`, but still works. – LOlliffe Dec 06 '13 at 22:32
3

This script that will output the files with the directory structure under "fixedFiles". At the moment is fixed to MOV files and will execute a number of transformations depending on the original "rotation" of the video. Works with iOS captured videos on a Mac running Mavericks, but should be easily exportable. Relies on having installed both exiftool and ffmpeg.

#!/bin/bash

# rotation of 90 degrees. Will have to concatenate.
#ffmpeg -i <originalfile> -metadata:s:v:0 rotate=0 -vf "transpose=1" <destinationfile>
#/VLC -I dummy -vvv <originalfile> --sout='#transcode{width=1280,vcodec=mp4v,vb=16384,vfilter={canvas{width=1280,height=1280}:rotate{angle=-90}}}:std{access=file,mux=mp4,dst=<outputfile>}\' vlc://quit

#Allowing blanks in file names
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

#Bit Rate
BR=16384

#where to store fixed files
FIXED_FILES_DIR="fixedFiles"
#rm -rf $FIXED_FILES_DIR
mkdir $FIXED_FILES_DIR

# VLC
VLC_START="/Applications/VLC.app/Contents/MacOS/VLC -I dummy -vvv"
VLC_END="vlc://quit"


#############################################
# Processing of MOV in the wrong orientation
for f in `find . -regex '\./.*\.MOV'` 
do
  ROTATION=`exiftool "$f" |grep Rotation|cut -c 35-38`
  SHORT_DIMENSION=`exiftool "$f" |grep "Image Size"|cut -c 39-43|sed 's/x//'`
  BITRATE_INT=`exiftool "$f" |grep "Avg Bitrate"|cut -c 35-38|sed 's/\..*//'`
  echo Short dimension [$SHORT_DIMENSION] $BITRATE_INT

  if test "$ROTATION" != ""; then
    DEST=$(dirname ${f})
    echo "Processing $f with rotation $ROTATION in directory $DEST"
    mkdir -p $FIXED_FILES_DIR/"$DEST"

    if test "$ROTATION" == "0"; then
      cp "$f" "$FIXED_FILES_DIR/$f"

    elif test "$ROTATION" == "180"; then
#      $(eval $VLC_START \"$f\" "--sout="\'"#transcode{vfilter={rotate{angle=-"$ROTATION"}},vcodec=mp4v,vb=$BR}:std{access=file,mux=mp4,dst=\""$FIXED_FILES_DIR/$f"\"}'" $VLC_END )
      $(eval ffmpeg -i \"$f\" -vf hflip,vflip -r 30 -metadata:s:v:0 rotate=0 -b:v "$BITRATE_INT"M -vcodec libx264 -acodec copy \"$FIXED_FILES_DIR/$f\")

    elif test "$ROTATION" == "270"; then
      $(eval ffmpeg -i \"$f\" -vf "scale=$SHORT_DIMENSION:-1,transpose=2,pad=$SHORT_DIMENSION:$SHORT_DIMENSION:\(ow-iw\)/2:0" -r 30 -s "$SHORT_DIMENSION"x"$SHORT_DIMENSION" -metadata:s:v:0 rotate=0 -b:v "$BITRATE_INT"M -vcodec libx264 -acodec copy \"$FIXED_FILES_DIR/$f\" )

    else
#      $(eval $VLC_START \"$f\" "--sout="\'"#transcode{scale=1,width=$SHORT_DIMENSION,vcodec=mp4v,vb=$BR,vfilter={canvas{width=$SHORT_DIMENSION,height=$SHORT_DIMENSION}:rotate{angle=-"$ROTATION"}}}:std{access=file,mux=mp4,dst=\""$FIXED_FILES_DIR/$f"\"}'" $VLC_END )
      echo ffmpeg -i \"$f\" -vf "scale=$SHORT_DIMENSION:-1,transpose=1,pad=$SHORT_DIMENSION:$SHORT_DIMENSION:\(ow-iw\)/2:0" -r 30 -s "$SHORT_DIMENSION"x"$SHORT_DIMENSION" -metadata:s:v:0 rotate=0 -b:v "$BITRATE_INT"M -vcodec libx264 -acodec copy \"$FIXED_FILES_DIR/$f\" 
      $(eval ffmpeg -i \"$f\" -vf "scale=$SHORT_DIMENSION:-1,transpose=1,pad=$SHORT_DIMENSION:$SHORT_DIMENSION:\(ow-iw\)/2:0" -r 30 -s "$SHORT_DIMENSION"x"$SHORT_DIMENSION" -metadata:s:v:0 rotate=0 -b:v "$BITRATE_INT"M -vcodec libx264 -acodec copy \"$FIXED_FILES_DIR/$f\" )

    fi

  fi

echo 
echo ==================================================================
sleep 1
done

#############################################
# Processing of AVI files for my Panasonic TV
# Use ffmpegX + QuickBatch. Bitrate at 16384. Camera res 640x424
for f in `find . -regex '\./.*\.AVI'` 
do
  DEST=$(dirname ${f})
  DEST_FILE=`echo "$f" | sed 's/.AVI/.MOV/'`
  mkdir -p $FIXED_FILES_DIR/"$DEST"
  echo "Processing $f in directory $DEST"
  $(eval ffmpeg -i \"$f\" -r 20 -acodec libvo_aacenc -b:a 128k -vcodec mpeg4 -b:v 8M -flags +aic+mv4 \"$FIXED_FILES_DIR/$DEST_FILE\" )
echo 
echo ==================================================================

done

IFS=$SAVEIFS
  • 2
    Ouch... This may work, but it is perfectly hideous. First of all, the general rule of shell-programming: you never need more than one of grep, cut, awk, or sed in a pipe. Any grep|cut|sed is doable with either sed or awk alone. Then, more specifically, exiftool can be asked to output just the tag(s) you want -- so instead of filtering out the Rotation, just "exiftool -Rotation". Third, you don't need so many "evals" and your "if test..." should be replaced with case $ROTATION in -- for both readability and efficiency. Good luck! – Mikhail T. Dec 31 '13 at 03:21
  • No dice: `[Parsed_pad_2 @ 0x7f8b15c3a580] Input area -420:0:1500:1080 not within the padded area 0:0:1080:1080 or zero-sized`\n ` `[Parsed_pad_2 @ 0x7f8b15c3a580] Failed to configure input pad on Parsed_pad_2`\n `Error reinitializing filters!`\n `Failed to inject frame into filter network: Invalid argument`\n `Error while processing the decoded data for stream #0:0`\n `Conversion failed!` – Slipp D. Thompson Sep 05 '19 at 04:16
3

Since ffmpeg transpose command is very slow, use the command below to rotate a video by 90 degrees clockwise.

Fast command (Without encoding)-

ffmpeg -i input.mp4 -c copy -metadata:s:v:0 rotate=270 output.mp4

For full video encoding (Slow command, does encoding)

ffmpeg -i inputFile -vf "transpose=1" -c:a copy outputFile
Sarthak Singhal
  • 818
  • 8
  • 13
  • Note that the fast command is only adding video metadata, which may already be present in many sources. The issue will be whether or not the player rendering the video respects the metadata flag or not :) – jocull Oct 13 '20 at 16:03
2

Alexy's answer almost worked for me except that I was getting this error:

timebase 1/90000 not supported by MPEG 4 standard, the maximum admitted value for the timebase denominator is 65535

I just had to add a parameter (-r 65535/2733) to the command and it worked. The full command was thus:

ffmpeg -i in.mp4 -vf "transpose=1" -r 65535/2733 out.mp4
smoyth
  • 589
  • 4
  • 13
  • If you're looking for a "NTSC-film" framerate, then `24000/1001` would be more accurate. – Reino Aug 01 '19 at 10:15
2

An additional solution with a different approach from the last mentioned solutions, is to check if your camera driver support the v4l2 camera controls (which is very common).
In the terminal just type:

v4l2-ctl -L

If your camera driver supports the v4l2 camera controls, you should get something like this (the list below depends on the controls that your camera driver supports):

               contrast (int)    : min=0 max=255 step=1 default=0 value=0 flags=slider
             saturation (int)    : min=0 max=255 step=1 default=64 value=64 flags=slider
                    hue (int)    : min=0 max=359 step=1 default=0 value=0 flags=slider
white_balance_automatic (bool)   : default=1 value=1 flags=update
            red_balance (int)    : min=0 max=4095 step=1 default=0 value=128 flags=inactive, slider
           blue_balance (int)    : min=0 max=4095 step=1 default=0 value=128 flags=inactive, slider
               exposure (int)    : min=0 max=65535 step=1 default=0 value=885 flags=inactive, volatile
         gain_automatic (bool)   : default=1 value=1 flags=update
                   gain (int)    : min=0 max=1023 step=1 default=0 value=32 flags=inactive, volatile
        horizontal_flip (bool)   : default=0 value=0
          vertical_flip (bool)   : default=0 value=0

And if you are lucky it supports horizontal_flip and vertical_flip.
Then all you need to do is to set the horizontal_flip by:

v4l2-ctl --set-ctrl horizontal_flip=1

or the vertical_flip by:

v4l2-ctl --set-ctrl vertical_flip=1

and then you can call your video device to capture a new video (see example below), and the video will be rotated/flipped.

ffmpeg -f v4l2 -video_size 640x480 -i /dev/video0 -vcodec libx264 -f mpegts input.mp4

Of-course that if you need to process an already existing video, than this method is not the solution you are looking for.

The advantage in this approach is that we flip the image in the sensor level, so the sensor of the driver already gives us the image flipped, and that's saves the application (like FFmpeg) any further and unnecessary processing.

J.M.
  • 151
  • 2
  • 9
  • Great information about v4l2 here. Rotating to the correct orientation at recording time is obviously preferable :) This almost 10 year old question (wow!) was originally asked about videos being uploaded from iOS devices if that helps provide any context :) – jocull May 07 '20 at 16:28
  • I'm a bit new in this community and this question is much older then my seniority here... I truly think it's a good (and common) question, so I believe this old question will keep helping many FFmpeg users. – J.M. May 07 '20 at 20:49
1

Smartphone: Recored a video in vertical format

Want to send it to a webside it was 90° to the left (anti clockwise, landscape format) hmm.

ffmpeg -i input.mp4 -vf "rotate=0" output.mp4

does it. I got vertical format back again

debian buster: ffmpeg --version ffmpeg version 4.1.4-1~deb10u1 Copyright (c) 2000-2019 the FFmpeg developers

f b
  • 105
  • 10
1

Unfortunately, the Ubuntu version of ffmpeg does support videofilters.

You need to use avidemux or some other editor to achieve the same effect.

In the programmatic way, mencoder has been recommended.

ldig
  • 19
  • 1
  • 1
    Did you mean "Ubuntu version of ffmpeg does *not* support videofilters"? – user1438038 Jan 04 '16 at 18:03
  • 2
    FYI: "FFmpeg has returned in Ubuntu 15.04 Vivid Vervet." or can be compiled for Ubuntu. --> http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu – l --marc l Jan 21 '16 at 00:08
  • avconv does (in 14.04), and seems to work exactly the same. (But for later versions, definitely use FFmpeg.) – SilverWolf Sep 02 '18 at 17:09
0

For me it works like this

Rotate clockwise

 ffmpeg -i "path_source_video.mp4" -filter:v "transpose=1" "path_output_video.mp4"

Rotate counterclockwise

 ffmpeg -i "path_source_video.mp4" -filter:v "transpose=0,transpose=1,transpose=0" -acodec copy "path_output_video.mp4"

the package I use zeranoe

AllanRibas
  • 339
  • 3
  • 10