我一直试图弄清楚如何旋转视频与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度也是个不错的选择。旋转选项去哪儿了?
这个脚本将输出目录结构在“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
另一个解决方案与前面提到的解决方案不同,就是检查您的相机驱动程序是否支持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)节省了任何进一步和不必要的处理。