旋转 mp4视频而不需要重新编码

我正在寻找一种方法来旋转视频拍摄与我的 Nexus4在我的 Debian 喘息系统。视频是在肖像模式拍摄,我想旋转他们到景观模式。最好是命令行驱动的旋转。

我已经发现了几个以前的问题,暗示了一个很好的解决方案,但我似乎不能设法得到它的工作。

首先有这样一个问题: 使用 FFmpeg 旋转视频

但是它表明 ffmpeg 已经过时了,我应该使用 avconv。我发现这个问题详细说明了前进的方向。 Https://askubuntu.com/questions/269429/how-can-i-rotate-video-by-180-degrees-with-avconv

这使我使用以下命令:

avconv -i original.mp4 -vf "transpose=1" -codec:v libx264 -preset slow -crf 25 -codec:a copy flipped.mp4

然而,这是极其缓慢的(最后一次测试花了我6个多小时,不到3分钟的镜头) ,并没有产生一个可播放的电影。我还在日志输出中得到一个错误,该错误指出 Mb Rate > level limit。

重新编码有什么问题吗?我应该首先重新编码视频从我的手机到另一个,更“可行”的编码之前,应用旋转?还是我漏掉了另一个重点?

92359 次浏览

This answer is simply a summary of the comments provided by LordNeckbeard.

Rotating without encoding

Rotating without re-encoding is not possible unless:

  • your input is MJPEG
  • you rotate upon playback

Rotate with encoding using the correct ffmpeg

To correctly understand the steps needed to this, one should start by reading or at least skimming this question:

What are the differences and similarities between ffmpeg, libav, and avconv?

Summary: avconv is a fork of ffmpeg, debian maintainer chose avconv, you have to compile the correct ffmpeg from source.

The next step would be compiling the correct ffmpeg from source as is detailed here:

Compilation guide of ffmpeg for Debian

The final step is using the commands found in other posts:

How to flip a video 180° (vertical/upside down) with FFmpeg? or Rotating videos with FFmpeg

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

There are several things that you've touched on in your question:

  1. There is almost no chance that you will able able to rotate without reencoding. The exception to that rule (MJPEG codec) has already been pointed out, but it's unlikely that you are using it, so it goes beyond the scope of this question. I will mention that the reason for this ability is that JPEG can be converted via metadata. Thus if you'll be able to find a container which has the metadata rotation, you'll be able to rotate, but none exist (or are wide spread enough) so far.

    an update: This answer now provides a way to losslessly rotate MP4 container.

  2. If it took your hours to rotate and reencode 3 minutes of the video, then the problem can lie on an enormous resolution. And i mean huge! Can you please provide the output of the avprobe original.mp4 so that it can be ascertained.

  3. Libav versus FFmpeg debates are very counterproductive (you can see that by the amount of fud posted here already). Basically what has happened was a split of the project with some developers going one way and some another, the fact that FFmpeg project has managed to keep the name is just a fluke and it makes no sense to call one project original and another a fork. The differences between projects are mostly in the development style and on the philosophy. If you would characterise FFmpeg as more Open Source and Libav as more Free Software, you'd not be all that mistaken. Rational people hope that eventually the developers will come to their senses, and maybe not merge the projects, but cooperate to the larger extent. When Ubuntu came out originally, on every Debian GNU/Linux chat there were huge messages along the line of "Ubuntu is NOT Debian!!!!!!!", but now the situation has calmed down and both sides are quite happy with one another.

If you just want to change the metadata such that mediaplayers that consider the flag play the file rotated, try something like:

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

as found elsewhere on stackoverflow.

Rotation=0 fixed my issue. I started recording video in portrait mode, realized my mistake and immediately turn my phone to landscape to continue recording. My iphone had marked the video as portrait for the entire video.

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

Fixed it.

FFmpeg and similar programs change the metadata even with the -map_metadata option. exiftool can read the rotation matrix and rotation flag, and since version 10.89 also write it as described below.

To get true lossless (incl. metadata) rotation, I couldn't find a solution, so I grabbed a hex editor (eg HxD) and analyzed the rotated video files.

True lossless rotation of MP4:

  • open mp4 with hex editor and search for vide to find the metadata of the video track

  • some rows above (for my files mostly 9, sometimes 12) you should see trak...\tkhd

  • in between there should be an @ sign (HEX 40)

  • in the two rows before it the rotation matrix is stored

  • no rotation:

     00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    40
    
  • 180°:

     FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    40
    
  • 90° cw:

     00 00 00 00 00 01 00 00 00 00 00 00 FF FF 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    40
    
  • 90° ccw:

     00 00 00 00 FF FF 00 00 00 00 00 00 00 01 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    40
    

Alter the file as you need it, and it should be rotated in players that support the rotation flag (most current players do).

In case your video contains stereo audio, this is obviously not switched, so in case you want the sound to match with video rotation (180°), you need to switch the left and right channels.

ExifTool lossless rotation:

Source.

Set rotation to 270°:

exiftool -rotation=270 FILE.mp4

Add 90° to existing rotation value:

exiftool "-rotation<${rotation;$_ += 90}" FILE.mp4

One Line Solution

npx rotate-video --source=/sourceVideoPath --destination=/Users/destinationVideoDir --extension=MP4 --angle=270

Note: You need to install FFMPEG CLI first install