-
Notifications
You must be signed in to change notification settings - Fork 2
Tips: To flip a video
Synopsis:
To use ffmpeg to horizontal flip the video, and together with other common operations.
在 FFmpeg 中,对视频进行左右镜像(水平翻转)可以使用 hflip滤镜。以下是几种常见用法:
ffmpeg -i input.mp4 -vf "hflip" output.mp4
ffmpeg -i input.mp4 -vf "hflip" -c:a copy output.mp4
如需要同时进行其他处理(如缩放、加水印等):
如缩放 1280x720 ==> 720x406
ffmpeg -i input.mp4 -vf "scale=720:406,hflip" output.mp4
缩放(宽度80%,高度80%),然后水平翻转
ffmpeg -i input.mp4 -vf "scale=iw*0.8:ih*0.8,hflip" output.mp4
放大至 1280x720
ffmpeg -i input.mp4 -vf "scale=1280x720,hflip" output.mp4
缩小至(宽度50%,高度50%),然后水平翻转
ffmpeg -i input.mp4 -vf "scale=iw/2:ih/2,hflip" output.mp4
ffmpeg -i input.mp4 -vf "crop=w:h:x:y,hflip" output.mp4
假设视频原始分辨率为WxH,需要裁剪掉上方top像素,下方bottom像素,那么裁剪后的高度为H - top - bottom。裁剪滤镜的参数为:crop=W:H-top-bottom:0:top.
又如,
# 裁剪掉上下各10%
ffmpeg -i input.mp4 -vf "crop=iw:ih*0.8:0:ih*0.1" output.mp4
# 剪裁右半部分并镜像
ffmpeg -i input.mp4 -vf "crop=iw/2:ih:iw/2:0,hflip" output.mp4
原始分辨率:576x1280 裁剪参数:crop=576:800:0:236 即:从原始视频中裁剪出宽576,高800的区域,从(0,236)开始。 然后进行水平翻转(hflip)。 翻转后,我们想要在翻转后的右下角,即坐标范围大约为: 水平:从360到560(宽度200像素) 垂直:从720到770(高度50像素) 注意:翻转后,坐标原点在左上角,右下角区域即为上述矩形区域。 但是,由于我们进行了水平翻转,翻转后的坐标与原始坐标的对应关系是: 原始坐标(x, y)在翻转后变为(576-1-x, y) (因为宽度为576) 所以,我们想要在翻转后的视频的右下角区域(即原始视频翻转前的左下角区域)进行模糊。 具体来说,翻转后我们想要模糊的区域是:x从360到560,y从720到770。 在翻转前,这个区域对应的原始坐标是: x' = 576 - 1 - x,因为翻转,所以翻转后的x=360对应原始x'=576-1-360=215
步骤: 1. 裁剪原始视频:crop=576:800:0:236,得到视频A(576x800) 2. 将视频A水平翻转,得到视频B(576x800) 3. 在视频B上,在位置(360,720)处叠加一个模糊的矩形区域,大小为200x50。
- 裁剪原始视频:crop=576:800:0:236,得到视频:(576x800)
- 水平翻转:hflip
- 分割流:将翻转后的视频分成两个流,一个用于模糊区域,一个用于保持清晰
- 对其中一个流进行裁剪,裁剪出右下角区域(360,720,200,50),然后进行模糊
- 将模糊后的区域叠加回原视频的相应位置
因此,我们可以这样写滤镜:
输入视频
-> 裁剪:crop=576:800:0:236
-> 水平翻转:hflip
-> 分割成两个流:[main]和[blur]
[blur]流:裁剪出右下角区域:crop=200:50:360:720,然后进行高斯模糊:gblur=sigma=5(sigma是高斯模糊的标准差,可以调整)
[main]流:保持不变
然后将[blur]流叠加到[main]流的(360,720)位置。
但是注意,[blur]流被裁剪后只有200x50,我们需要将它叠加到[main]流的指定位置。
所以,完整的滤镜链为:
[0:v]crop=576:800:0:236,hflip,split=2[main][blur];
[blur]crop=200:50:360:720,gblur=sigma=5,setpts=PTS[blurred];
[main][blurred]overlay=360:720
注意:由于[blur]流被裁剪后时间戳可能有问题,我们使用setpts=PTS来保持时间戳。
ffmpeg cli command
INPUT="$1"
OUTPUT="$2"
SIGMA="${3:-5}" # 高斯模糊sigma值,默认5
BLUR_X="${4:-360}" # 翻转后x坐标,默认360
BLUR_Y="${5:-720}" # 翻转后y坐标,默认720
BLUR_W="${6:-200}" # 宽度,默认200
BLUR_H="${7:-50}" # 高度,默认50
PRE_FLIP_X=$((576 - 1 - BLUR_X - BLUR_W + 1))
echo "========================================"
echo "输入文件: $INPUT"
echo "输出文件: $OUTPUT"
echo "翻转后分辨率: 576x800"
echo "高斯模糊sigma值: ${SIGMA}"
echo ""
echo "翻转后模糊区域:"
echo " 坐标: (${BLUR_X}, ${BLUR_Y})"
echo " 大小: ${BLUR_W}x${BLUR_H}"
echo " 范围: ${BLUR_X}:${BLUR_Y} ~ $((BLUR_X+BLUR_W-1)):$((BLUR_Y+BLUR_H-1))"
echo ""
echo "翻转前对应区域:"
echo " 坐标: (${PRE_FLIP_X}, ${BLUR_Y})"
echo " 范围: ${PRE_FLIP_X}:${BLUR_Y} ~ $((PRE_FLIP_X+BLUR_W-1)):$((BLUR_Y+BLUR_H-1))"
echo "========================================"
ffmpeg -i "$INPUT"
-filter_complex "[0:v]crop=576:800:0:236,split=2[main][blur];
[blur]crop=${BLUR_W}:${BLUR_H}:${PRE_FLIP_X}:${BLUR_Y},gblur=sigma=${SIGMA},setpts=PTS[blurred];
[main]hflip[flipped];
[flipped][blurred]overlay=${BLUR_X}:${BLUR_Y}"
-c:a copy "$OUTPUT"
echo "处理完成! 输出文件: $OUTPUT"
User request:
The original video resolution is 576x1280. Use ffmpeg to crop and horizontal flip: crop=576:800:0:236, hflip. After that, delogo region (in the cropped+flipped frame) at the bottom-right coordinates: 360:720 to 560:770. I.e., use the delogo filter to treat a specific region (360:720 to 560:770) as a smooth, logo-free patch within a certain time segment of the video.
The video is 576x800, and the delogo region is at bottom-right: 360 to 560 x, 720 to 770 y, which a horizontal strip at the bottom, width 200, from x=360 to 560 (width 200), y=720 to 770 (height 50). The delogo filter's parameters are x:y:w:h:show (optional). So we need delogo x=360:y=720:w=200:h=50. The delogo expects the top-left corner of the rectangle and its width and height. So delogo=360:720:200:50:show=0 or 1.
So, full ffmpeg command:
# Input / output
INPUT="input.mp4"
OUTPUT="output.mp4"
# Original video size (for reference)
ORIG_W=576
ORIG_H=1280
# Crop parameters (applied first)
CROP_W=576
CROP_H=800
CROP_X=0
CROP_Y=236
# Horizontal flip (1 = enable, 0 = disable)
DO_HFLIP=1
# Delogo rectangle (coordinates after crop+flip)
LOGO_X=360
LOGO_Y=720
LOGO_W=200
LOGO_H=50
LOGO_SHOW=0 # 0=remove logo, 1=only show rectangle
# Time segment (seconds) where delogo is active
START_TIME=10.5
END_TIME=20.0
# ========== BUILD FILTER CHAIN ==========
FILTER="crop=${CROP_W}:${CROP_H}:${CROP_X}:${CROP_Y}"
FILTER="${FILTER},hflip"
# Append delogo with timeline enable
FILTER="${FILTER},delogo=x=${LOGO_X}:y=${LOGO_Y}:w=${LOGO_W}:h=${LOGO_H}:show=${LOGO_SHOW}:enable='between(t,${START_TIME},${END_TIME})'"
# ========== RUN FFMPEG ==========
ffmpeg -i "$INPUT" -vf "$FILTER" -c:a copy "$OUTPUT"Important notes:
-
The
delogofilter'senableoption uses timeline editing -- only frames withtbetweenSTART_TIMEandEND_TIMEare processed; the rest pass through unchanged. -
If you want to see the treated area before actual removal, temporarily set
LOGO_SHOW=1(a green rectangle will appear). -
One-liner without a script:
ffmpeg -i "$INPUT" -vf "crop=$CROP_W:$CROP_H:$CROP_X:$CROP_Y,hflip,delogo=x=$LOGO_X:y=$LOGO_Y:w=$LOGO_W:h=$LOGO_H:show=$LOGO_SHOW:enable='between(t,$START_TIME,$END_TIME)'" -c:a copy "$OUTPUT" -
If to trim a media file using start and stop times as well, using
ffmpeg -i "$INPUT" -ss 00:00:03 -t 00:00:52.6 -vf "..." "$OUTPUT",
Then the filters see timestamps that start at 0 for the first output frame. Thus, the delogo times must be adjusted relative to the trim start:
Adjusted START = original_start - trim_start
Adjusted END = original_end - trim_start
For example, if delogo should be from 00:00:42 to 00:00:58 in the original, and you trim from is 00:00:03, then the adjusted times are 39 s to 55 s.