TypechoJoeTheme

半醉残影

统计

ffmeg常用命令

2021-10-29
/
0 评论
/
254 阅读
/
正在检测是否收录...
10/29

视频加图像水印

ffmpeg -i a.mp4 -i b.png -filter_complex overlay=x=0:y=0 out.mp4

# 每个30s 加3s水印
 ffmpeg -i 1.mp4 -i 123.png -filter_complex "overlay=x=0:y=0:enable='lt(mod(t,30),3)'" -y out.mp4
ffmpeg -i 1.mp4 -i header1.mov -t 15 -filter_complex "amix=inputs=2:duration=longest;[1]scale=iw*0.5:-1[open];[0][open]overlay=x=0:y=0:repeatlast=0[o1];movie=water.png [watermark];[o1][watermark]overlay=x=0:y=0:enable='gte(t,4)'[o1];[o1]subtitles=y2.ass[o1]" -map "[o1]"  ooo.mp4

图片给合成视频

ffmpeg -f image2 -i image%d.jpg video.mpg
ffmpeg -f image2 -r 60 -i output_%05d.png  1080p.mp4

ffprobe json

ffprobe -v quiet -print_format json -show_format -show_streams a.mp4

ffprobe -v quiet  -print_format json -show_format -show_streams -i 2.mp4

视频截取

裁剪视频

按时间

ffmpeg  -i 1.mp4 -vcodec copy -acodec copy -ss 00:01:00 -to 00:02:59 ./1_cut.mp4 -y

ffmpeg -ss [start] -t [duration] -i [in].mp4 -c:v libx264 -c:a aac -strict experimental -b:a 98k [out].mp4

def split_by_time(self, index, s, e):
    dst = os.path.join(self.cache_video, "%s.mp4" % index)
    args = ["ffmpeg", "-accurate_seek", "-i", self.media_path, "-ss", str(s), "-to", str(e), "-c:v", "libx264","-c:a", "copy", "-strict","experimental","-avoid_negative_ts", "1",dst, "-y"]
    r = subprocess.run(args=args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8")
    if r.returncode != 0:
        print(r)
        return
    print("success %s" % dst)

按帧

ffmpeg -i ./input.mp4 -vf "select=between(n\,20\,200)" -y -acodec copy ./output.mp4

提取视频中的帧

ffmpeg -i data/1_cut.mp4 -s 480x270 -vf "select=between(n\,5\,3569)*not(mod(n\,5))" -vsync 0 "data/sc/image-%05d.jpg"

主体是一个select 的过滤语句:
其中:

  • between(n,*) 是指 从第几帧到第几帧之间进行提取...
  • not(mode(n, K))是指每隔几帧输出一帧。

使用between可以指定需要提取开始帧和结束帧的位置,如果想要对视频中所有的帧进行间隔采样,命令如下

command_extract = "select=(gte(n\,%d))*not(mod(n\,%d))"%(60,60)
com_str = 'ffmpeg -i ' + video_path + ' -vf "%s" -vsync 0 '%command_extract + save_jpg_tmp_path + '/%06d.jpg'

按时间间隔取帧


commands := []string{
`-i`, src,
"-s", "192x108",
"-vf", `select=(gte(t\,0))*(isnan(prev_selected_t)+gte(t-prev_selected_t\,1))`,
"-vsync", "0",
"-y", path.Join(dst, `%05d.jpg`),
}

ffmpeg -i <input> -vf "select=(gte(t\,0))*(isnan(prev_selected_t)+gte(t-prev_selected_t\,21))" -vsync 0 image_%05d.jpg
    
ffmpeg -i <input> -vf "select=(gte(t\,120))*(isnan(prev_selected_t)+gte(t-prev_selected_t\,120))" -vsync 0 image_%05d.jpg

上面命令中的120的单位是s,指每120s提取视频中的一帧图像

视频信息获取

获取帧总量

ffprobe -i .\data\1_cut.mp4 -select_streams v -show_entries stream=nb_frames -of default=nk=1:nw=1 -v quiet

获取视频总时长

ffprobe <input> -select_streams v -show_entries stream=duration -of default=nk=1:nw=1 -v quiet

视频切图

ffmpeg -i 2332103105.mp4 -vf "crop=540:540:0:420" -s "384x216" -y out.jpg

缩小视频并且加上水印

ffmpeg -i demo.mp4 -vf "movie=preview.watermark.png [watermark]; [in]scale=640:360 [scale]; [scale][watermark] overlay=0:0" c.mp4

多轨道视频叠加

[0]表示第一个输入,[1]表示第二个输入,大写W表示第一个宽,小写w表示第二个宽

ffmpeg -i a.mp4 -i b.mov   -filter_complex "[1]scale=iw*0.5:-1[open];[0][open]overlay=W/2:H/2"  -vcodec libx264 -acodec aac  out.mp4 -y

# repeatlast表示不统一长度,该结束就结束
ffmpeg -i 1.mp4 -i open.mov -t 10 -filter_complex "amix=inputs=2:duration=longest;[1]scale=640:-1[open];[0][open]overlay=x=0:y=0:repeatlast=0, subtitles=1.ass" out.mp4 -y

删除所有音轨

ffmpeg -i input_file.mp4 -vcodec copy -an output_file.mp4

加黑边

 ffmpeg -i demo.mp4 -vf 'scale=1080:608,pad=1080:810:0:106:black' y.mp4

缩放裁剪

ffmpeg -i 1-1080p.mp4 -vf "scale='3413:1920',crop='1080:1920:1160:0'" -c copy -threads 4 -vcodec libx264 -y 1-1080p-h.mp4

给视频加高斯模糊边框

ffmpeg -i 2.mp4 -vf "split[a][b];[a]scale=3413:1920,crop=1080:1920:1160:0,boxblur=15:15[1];[b]scale=1080:608[2];[1][2]overlay=0:(1920-608)/2" -c copy -threads 4 -vcodec libx264 -y 2-out.mp4

生成静音音频

ffmpeg -f lavfi -t 10 -i anullsrc test.aac -y
`-bsf:v`, `h264_mp4toannexb`,

给视频图片加上文字水印

ffmpeg -i 640.jpg -filter_complex "[0]scale=220:-1[img];[img]drawtext=text='07\:43':x=w-tw-10:y=h-th-10:fontsize=24:fontcolor=white:borderw=2:bordercolor=black" o-3.jpg -y
# 封面添加文字
 ffmpeg -i 1.png -filter_complex "[0]scale=1920:1080[img];[img]drawtext=x=(w-tw)/2:y=(h-th)/2:fontfile=1.ttf:fontsize=160:fontcolor=white:borderw=10:bordercolor=orange@0.8:box=0:boxborderw=5:boxcolor=black:shadowcolor=red:textfile=1.txt:line_spacing=80" o.jpg -y

滚动文字


 ffmpeg -i 1.mp4 -t 10 -vf "drawtext=text=string1 string2 string3 string4 string5 string6 string7 :expansion=normal:fontfile=1.ttf: y=h-line_h-10:x=(mod(5*n\,w+tw)-tw): fontcolor=white: fontsize=40: shadowx=2: shadowy=2" output.mp4

ffmpeg -i 1.png -filter_complex "[0]scale=1920:1080[img];[img]drawtext=x=(w-tw)/2:y=(h-th)/2:fontfile=font/fonts/25.ttf:fontsize=140:fontcolor=white:borderw=14:bordercolor=orange@0.9:box=0:boxborderw=5:boxcolor=black:shadowcolor=red:textfile=1.txt:line_spacing=40" o.jpg -y

视频封面生成

# https://blog.csdn.net/toopoo/article/details/105603154
ffmpeg -i a.mp4 -frames:v 1 -y a.jpg

生成纯黑视频


ffmpeg -f lavfi -i color=black:1920x1080:d=3 -format rgb32 -f matroska -vcodec libx264  test1.mp4

延长视频时间

# 调整视频流速度的setpts和调整音频流的atempo应是互为倒数的,
# 这样才能保证新视频里画面和声音同步。

# 只缩放视频
ffmpeg -i open.mov  -filter_complex "setpts=2*PTS" open2.mov

# 声画同步
ffmpeg -i input.MOV -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" video.mp4

ff录屏

ffmpeg -y -f gdigrab -t 25 -r 30 -i  desktop -draw_mouse 0 -c:v h264_amf 1.mp4
pix_fmt yuv420p

缩放裁剪视频

// 裁剪视频,缩放裁剪视频到1080p
func (t *VideoTask) cropVideo(srcPath, dstPath string, width, height int64) error {
    //计算视频尺寸
    j := lib.GetFileParam(srcPath, "video")
    videoWidth := j.Get("width").Int()
    videoHeight := j.Get("height").Int()

    lib.ReportLog(t.task.Id, fmt.Sprintf("协程[%d] 视频原始尺寸:%dx%d, 缩放裁剪到:%dx%d \n", t.taskId, videoWidth, videoHeight, width, height))

    // 如果视频尺寸和目标尺寸相同,直接返回
    if videoWidth == width && videoWidth == height {
        lib.CopyFile(dstPath, srcPath)
        return nil
    }
    var format string = ""
    videoRate := float64(videoWidth) / float64(videoHeight)
    dstRate := float64(width) / float64(height)
    if videoRate == dstRate {
        // 等比例的直接缩放
        format = fmt.Sprintf("scale=%d:%d", width, height)
    } else if videoRate > dstRate {
        // 如果原视频的宽高比大于目标视频,则说明原视频宽度有多余,缩放高,裁剪宽
        dstVideoWidth := float64(height) / float64(videoHeight) * float64(videoWidth)
        beginWidth := (dstVideoWidth - float64(width)) / 2
        format = fmt.Sprintf("scale=-1:%d,crop=%d:%d:%d:0", height, width, height, int(beginWidth))
    } else {
        // 如果原视频的宽高比小于目标视频,则说明原视频高度有多余,缩放宽,裁剪高
        dstVideoHeight := float64(width) / float64(videoWidth) * float64(videoHeight)
        beginHeight := (dstVideoHeight - float64(height)) / 2
        format = fmt.Sprintf("scale=%d:-1,crop=%d:%d:0:%d", width, width, height, int(beginHeight))

    }
    commands := []string{
        "-i", srcPath,
        "-filter_complex", format,
        "-acodec", "mp3",
        "-vcodec", "libx264",
        "-pix_fmt", "yuv420p",
        "-y", dstPath,
    }
    cmd := exec.Command("ffmpeg", commands...)
    b, err := cmd.CombinedOutput()
    if err != nil {
        log.Printf(string(b))
        log.Println(commands)
        lib.ReportLog(t.task.Id, fmt.Sprintf("协程[%d] 命令:%s\n异常:\n%s", t.taskId, commands, string(b)))
        return err
    }
    return nil
}

ffmpeg 重复,延长时间

ffmpeg -i in.mp4 -vf tpad=stop_mode=clone:stop_duration=2 out.mp4
ffmpeg -i i1.mp4 -filter_complex "[0]scale=640:360[o1];[o1]tpad=start_mode=clone:start_duration=3" -y ddd.mp4

png转mov

ffmpeg -r 80  -i img/%03d.png  -vf fps=60 -vcodec qtrle out.mov -y

叠加水印并把图像转成视频

ffmpeg -i 1.png -i 2.png -filter_complex "overlay=0:0[o1];[o1]tpad=start_mode=clone:start_duration=5" -vcodec libx264 -r 32 -y 3.mp4

视频是否异常

ffmpeg -i 1.mp4 -v error -f null -

合并视频

ffmpeg -f concat -i filelist.txt  -c copy -y z1.mp4

png转rgba

ffmpeg -i demo.png -filter_complex "format=rgba" demo1.png -y
ffmpeg
朗读
赞(0)
赞赏
感谢您的支持,我会继续努力哒!

三合一收款

下面三种方式都支持哦

微信
QQ
支付宝
打开支付宝/微信/QQ扫一扫,即可进行扫码打赏哦
版权属于:

半醉残影

本文链接:

https://blog.dengyb.com/archives/113/(转载时请注明本文出处及文章链接)

评论 (0)

人生倒计时

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

最新回复

  1. 搭建自用导航网站 R11; JKblog
    2022-03-10
  2. JK
    2022-01-13

    {!{data:image/webp;base64,UklGRhwNAABXRUJQVlA4WAoAAAAwAAAAAQ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5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJwoAAP7/rR4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==}!}

  3. 搭建自用导航网站 R11; JKblog
    2022-01-12
  4. 搭建自用导航网站 R11; JKblog
    2022-01-12
  5. MrGao
    2019-09-03

标签云