我一直试图弄清楚如何使用FFmpeg旋转视频。我正在处理以纵向模式拍摄的iPhone视频。我知道如何使用MediaInfo(优秀的库,顺便说一句)确定当前的旋转度,但我现在卡在FFmpeg上。
根据我的阅读,您需要使用的是 vfilter 选项。根据我的看法,它应该是这样的:
ffmpeg -vfilters "rotate=90" -i input.mp4 output.mp4
然而,我无法让它发挥作用。首先, -vfilters 不再存在,现在只是 -vf 。其次,我收到了这个错误:
No such filter: 'rotate'
Error opening filters!
据我所知,我有一个FFmpeg的全选版本。运行 ffmpeg -filters 会显示:
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.
拥有 vflip 和 hflip 的选项非常棒,但他们只是不会让我到达我需要去的地方。我需要能够将视频至少旋转90度。 270度也是一个很好的选择。旋转选项哪里去了?
答案 0 :(得分:575)
顺时针旋转90度:
ffmpeg -i in.mov -vf "transpose=1" out.mov
对于转置参数,您可以传递:
0 = 90CounterCLockwise and Vertical Flip (default)
1 = 90Clockwise
2 = 90CounterClockwise
3 = 90Clockwise and Vertical Flip
使用-vf "transpose=2,transpose=2"
180度。
确保使用最近的ffmpeg版本from here(静态版本可以正常工作)。
请注意,这将重新编码音频和视频部分。您通常可以使用-c:a copy
复制音频而不触摸它。要更改视频质量,请设置比特率(例如使用-b:v 1M
),或者如果需要VBR选项,请查看H.264 encoding guide。
解决方案也是使用此convenience script。
答案 1 :(得分:101)
如果您不想重新编码视频并且您的播放器可以处理旋转元数据,您只需使用ffmpeg更改元数据中的旋转:
ffmpeg -i input.m4v -metadata:s:v rotate="90" -codec copy output.m4v
答案 2 :(得分:80)
你有没有试过transpose
?喜欢(来自其他答案)
ffmpeg -i input -vf transpose=2 output
如果您使用的是旧版本,则必须更新ffmpeg,如果您想使用2011年10月添加的转置功能。</ p>
FFmpeg download页面提供了静态构建,您可以直接执行而无需编译它们。
答案 3 :(得分:17)
我在搜索相同的答案时遇到了这个页面。现在已经有六个月的时间了,这个问题从那时起已经多次更新了版本。但是,我想为在这里遇到的其他人寻找此信息添加答案。
我正在使用这些存储库中的Debian Squeeze和 FFmpeg 版本。
ffmpeg的MAN页面说明了以下用途
ffmpeg -i inputfile.mpg -vf "transpose=1" outputfile.mpg
关键是您不要使用度数变量,而是使用MAN页面中的预定义设置变量。
0=90CounterCLockwise and Vertical Flip (default)
1=90Clockwise
2=90CounterClockwise
3=90Clockwise and Vertical Flip
答案 4 :(得分:13)
要顺时针旋转图片,您可以使用旋转滤镜,指示以弧度为单位的正角度。 90度相当于PI / 2,你可以这样做:
ffmpeg -i in.mp4 -vf "rotate=PI/2" out.mp4
逆时针方向角度必须为负
ffmpeg -i in.mp4 -vf "rotate=-PI/2" out.mp4
转置滤镜在90度时同样适用,但对于其他角度,这是一个更快或唯一的选择。
答案 5 :(得分:11)
ffmpeg -vfilters "rotate=90" -i input.mp4 output.mp4
即使有最新的消息来源,也行不通......
必须更改顺序:
ffmpeg -i input.mp4 -vf vflip output.mp4
工作正常
答案 6 :(得分:6)
如果您收到“Codec是实验性的但未启用实验性编解码器”错误,请使用以下命令:
ffmpeg -i inputFile -vf "transpose=1" -c:a copy outputFile
发生了一些带有aac音频的.mov文件。
答案 7 :(得分:3)
此脚本将在“fixedFiles”下输出目录结构的文件。目前固定为MOV文件,并将根据视频的原始“旋转”执行多个转换。适用于运行Mavericks的Mac上的iOS捕获视频,但应该可以轻松导出。依赖于安装 exiftool 和 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
答案 8 :(得分:2)
Alexy的回答几乎对我有用,只是我收到了这个错误:
MPEG 4标准不支持时基1/90000,最大值 时基分母的准入值为65535
我只需要在命令中添加一个参数(-r 65535/2733)即可。因此,完整的命令是:
ffmpeg -i in.mp4 -vf "transpose=1" -r 65535/2733 out.mp4
答案 9 :(得分:2)
与上述解决方案不同的另一种解决方案是检查相机驱动程序是否支持v4l2相机控件(这是非常常见的)。
在终端中输入:
v4l2-ctl -L
如果您的摄像头驱动程序支持v4l2摄像头控件,则应该获得以下内容(以下列表取决于您的摄像头驱动程序支持的控件):
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
如果幸运的话,它支持 horizontal_flip 和 vertical_flip 。
然后,您要做的就是通过以下方式设置 horizontal_flip :
v4l2-ctl --set-ctrl horizontal_flip=1
或 vertical_flip :
v4l2-ctl --set-ctrl vertical_flip=1
然后您可以调用视频设备捕获新视频(请参见下面的示例),视频将被旋转/翻转。
ffmpeg -f v4l2 -video_size 640x480 -i /dev/video0 -vcodec libx264 -f mpegts input.mp4
当然,如果您需要处理一个已经存在的视频,那么这种方法并不是您要寻找的解决方案。
这种方法的优点是我们可以在传感器级别翻转图像,因此驱动程序的传感器已经为我们提供了翻转的图像,这可以节省应用程序(如FFmpeg)任何进一步和不必要的处理。
答案 10 :(得分:1)
不幸的是,ffmpeg的Ubuntu版本确实支持视频过滤器。
您需要使用avidemux或其他编辑器来达到同样的效果。
以编程方式,推荐使用mencoder。
答案 11 :(得分:1)
由于ffmpeg转置命令非常慢,请使用以下命令将视频顺时针旋转90度。
快速命令-
ffmpeg -i input.mp4 -c copy -metadata:s:v:0 rotate=270 output.mp4
用于完整的视频编码(慢速命令)
ffmpeg -i inputFile -vf "transpose=1" -c:a copy outputFile
答案 12 :(得分:0)
智能手机:录制了垂直格式的视频
想要将其发送到网络侧,它的位置是向左90度(逆时针,横向格式)。
ffmpeg -i input.mp4 -vf "rotate=0" output.mp4
做到了。我又恢复了垂直格式
debian buster:ffmpeg --version ffmpeg版本4.1.4-1〜deb10u1版权所有(c)2000-2019 FFmpeg开发人员
答案 13 :(得分:0)
对我来说,它是这样的
顺时针旋转
ffmpeg -i "path_source_video.mp4" -filter:v "transpose=1" "path_output_video.mp4"
逆时针旋转
ffmpeg -i "path_source_video.mp4" -filter:v "transpose=0,transpose=1,transpose=0" -acodec copy "path_output_video.mp4"
我使用的zeranoe包裹