在 ffmpeg-python 中使用滤镜做个淡入淡出马赛克效果

ffmpeg-python仅仅是对ffmpeg的wrap,它所支持滤镜也是ffmpeg所支持的滤镜,滤镜可以用命令查看:

ffmpeg -filters 

Filters:
  T.. = Timeline support
  .S. = Slice threading
  ..C = Command support
  A = Audio input/output
  V = Video input/output
  N = Dynamic number and/or type of input/output
  | = Source or sink filter
前略...
.TS. fade              V->V       Fade in/out input video.
 T.. fftdnoiz          V->V       Denoise frames using 3D FFT.
 T.. fftfilt           V->V       Apply arbitrary expressions to pixels in frequency domain.
 ... field             V->V       Extract a field from the input video.
 ... fieldhint         V->V       Field matching using hints.
 ... fieldmatch        N->V       Field matching for inverse telecine.
 T.. fieldorder        V->V       Set the field order.
 T.C fillborders       V->V       Fill borders of the input video.
 ... find_rect         V->V       Find a user specified object.
 T.. floodfill         V->V       Fill area with same color with another color.
 ... format            V->V       Convert the input video to one of the specified pixel formats.
 ... fps               V->V       Force constant framerate.
 ... framepack         VV->V      Generate a frame packed stereoscopic video.
 .S. framerate         V->V       Upsamples or downsamples progressive source between specified frame rates.
 T.. framestep         V->V       Select one frame every N frames.
 ... freezedetect      V->V       Detects frozen video input.
 ... freezeframes      VV->V      Freeze video frames.
 T.C frei0r            V->V       Apply a frei0r effect.
 T.. fspp              V->V       Apply Fast Simple Post-processing filter.
 TSC gblur             V->V       Apply Gaussian Blur filter.
后略.....

比如 fade 就是淡入淡出滤镜,可以查看fade参数:

ffmpeg -h filter=fade

Filter fade
  Fade in/out input video.
    slice threading supported
    Inputs:
       #0: default (video)
    Outputs:
       #0: default (video)
fade AVOptions:
  type              <int>        ..FV....... set the fade direction (from 0 to 1) (default in)
     in              0            ..FV....... fade-in
     out             1            ..FV....... fade-out
  t                 <int>        ..FV....... set the fade direction (from 0 to 1) (default in)
     in              0            ..FV....... fade-in
     out             1            ..FV....... fade-out
  start_frame       <int>        ..FV....... Number of the first frame to which to apply the effect. (from 0 to INT_MAX) (default 0)
  s                 <int>        ..FV....... Number of the first frame to which to apply the effect. (from 0 to INT_MAX) (default 0)
  nb_frames         <int>        ..FV....... Number of frames to which the effect should be applied. (from 1 to INT_MAX) (default 25)
  n                 <int>        ..FV....... Number of frames to which the effect should be applied. (from 1 to INT_MAX) (default 25)
  alpha             <boolean>    ..FV....... fade alpha if it is available on the input (default false)
  start_time        <duration>   ..FV....... Number of seconds of the beginning of the effect. (default 0)
  st                <duration>   ..FV....... Number of seconds of the beginning of the effect. (default 0)
  duration          <duration>   ..FV....... Duration of the effect in seconds. (default 0)
  d                 <duration>   ..FV....... Duration of the effect in seconds. (default 0)
  color             <color>      ..FV....... set color (default "black")
  c                 <color>      ..FV....... set color (default "black")

下例给出使用 gblur,scale, fade滤镜把视频马赛克并淡入淡出

# coding: utf-8
import ffmpeg

VIDEO_FRAMERATE = 30

def image2mp4(fullpath,duration):
    # 输入图片地址fullpath,生成视频时长duration,
    v_in = ffmpeg.input(fullpath, t=duration, framerate=VIDEO_FRAMERATE, loop=1)
    # 高斯模糊滤镜,看起来有镜头不聚焦的效果
    v_g = ffmpeg.filter(v_in, filter_name='gblur', sigma=8)
    # 缩放滤镜,视频分辨率到427x240
    v_s1 = ffmpeg.filter(v_g, filter_name='scale', size='32:18')
    # 缩放滤镜,视频分辨率到1280x720
    v_s = ffmpeg.filter(v_s1, filter_name='scale', size='1280:720')
    # 淡入滤镜,效果时长1秒,白色
    v_f = ffmpeg.filter(v_s, filter_name='fade', type=0, duration=1, color='white')
    # 淡出滤镜,效果时长1秒,所以start_time等于视频时长减1秒,白色
    video = ffmpeg.filter(v_f, filter_name='fade', type=1, start_time=duration-1, duration=1, color='white')
    try:
        out, err = (ffmpeg
            .output(
                video, 
                'new.mp4',
                vcodec='libx264',
                f='mp4')
            .overwrite_output()
            .run(cmd=["ffmpeg"])
        )
    except ffmpeg.Error as error:
        print("stderr:",error.stderr)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值