代码之家  ›  专栏  ›  技术社区  ›  jocull

使用FFmpeg旋转视频

  •  339
  • jocull  · 技术社区  · 15 年前

    我一直在想如何用FFmpeg旋转视频。我正在处理以肖像模式拍摄的iPhone视频。我知道如何使用 MediaInfo

    据我所知,你需要使用的是 选择。据我所见,应该是这样的:

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

    但是,我不能让这个工作。第一, -过滤器 已经不存在了,现在只是 -心室颤动 . 其次,我得到这个错误:

    No such filter: 'rotate'
    Error opening filters!
    

    ffmpeg-过滤器

    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公司

    10 回复  |  直到 11 年前
        1
  •  667
  •   slhck    10 年前

    顺时针旋转90:

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

    对于transpose参数,可以传递:

    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 )或者看看 H.264 encoding guide 如果你想要VBR选项。

    convenience script .

        2
  •  139
  •   llogan    11 年前

    如果不想重新编码视频,并且播放器可以处理旋转元数据,则可以使用ffmpeg更改元数据中的旋转:

    ffmpeg -i input.m4v -metadata:s:v rotate="90" -codec copy output.m4v
    
        3
  •  82
  •   slhck    10 年前

    transpose 是吗?就像(从另一个答案)

     ffmpeg -i input -vf transpose=2 output
    

    FFmpeg download 页面提供了可以直接执行的静态构建,而无需编译它们。

        4
  •  19
  •   Sk8erPeter miku    13 年前

    我在用Debian挤压 FFmpeg 来自那些仓库的版本。

    ffmpeg的手册页说明了以下用途

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

    关键是不要使用度数变量,而是要使用手册页中的预定义设置变量。

    0=90CounterCLockwise and Vertical Flip  (default) 
    1=90Clockwise 
    2=90CounterClockwise 
    3=90Clockwise and Vertical Flip
    
        5
  •  16
  •   Bijou Trouvaille    12 年前

    要顺时针旋转图片,可以使用旋转过滤器,以弧度表示正角度。90度等于π/2,你可以这样做:

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

    对于逆时针方向,角度必须为负

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

        6
  •  12
  •   nano    15 年前
    ffmpeg -vfilters "rotate=90" -i input.mp4 output.mp4 
    

    不起作用,即使有最新消息来源。。。

    必须更改顺序:

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

    工作正常

        7
  •  7
  •   sabujp    13 年前

    如果您收到“Codec is experiential but experiential Codec a re not enabled”错误,请使用:

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

        8
  •  3
  •   David Costa Faidella    12 年前

    这个脚本将输出目录结构在“fixedFiles”下的文件。目前固定为MOV文件,并将根据视频的原始“旋转”执行一些转换。可以在运行Mavericks的Mac上使用iOS捕获的视频,但应该很容易导出。依赖于安装了 出口工具 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
    
        9
  •  2
  •   smoyth    12 年前

    亚历克西的回答几乎对我有效,只是我犯了个错误:

    MPEG 4标准不支持时基1/90000,最大 时基分母的允许值是65535

    ffmpeg -i in.mp4 -vf "transpose=1" -r 65535/2733 out.mp4
    
        10
  •  1
  •   ldig    14 年前

    不幸的是,Ubuntu版本的ffmpeg确实支持视频过滤器。

    您需要使用avidemex或其他编辑器来实现相同的效果。

        11
  •  1
  •   f b    6 年前

    智能手机:以垂直格式录制视频

    想把它发送到一个网页,它是90°到左边(逆时针,横向格式)嗯。

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

    做到了。我又恢复了垂直格式

    ffmpeg版本4.1.4-1~deb10u1版权所有(c)2000-2019 ffmpeg开发者