<?php namespace Source\Api;
use Dever;
class Test
{
    public function ffmpeg()
    {
        $start = '02';
        $end = '03';
        $text = '哈哈哈';
        $x = 50;
        $y = 50;
        $fontsize = 25;
        $file = '1.mp4';
        $ts_file = str_replace('1.mp4', '10.mp4', $file);

        //$command[] = 'ffmpeg -ss 00:00:'.$start.' -t 00:00:'.$end.' -y -i '.$file.' -vf "drawtext=fontfile=/share/lib/php/dever_package/poster/fonts/PingFang_Bold.ttf: text=\''.$text.'\':x='.$x.':y='.$y.':fontsize='.$fontsize.':fontcolor=white:shadowy=2" -c copy -bsf:v h264_mp4toannexb -an -f mpegts ' . $ts_file;

        $command[] = '/usr/local/ffmpeg/bin/ffmpeg -ss 00:00:'.$start.' -t 00:00:'.$end.' -y -i '.$file.' -an -vn -sn -vf "drawtext=fontfile=/share/lib/php/dever_package/poster/fonts/PingFang_Bold.ttf: text=\''.$text.'\':x='.$x.':y='.$y.':fontsize='.$fontsize.':fontcolor=white:shadowy=2" -b:v 10000k ' . $ts_file;

        $convert[] = $ts_file;

        $file = '2.mp4';
        $ts_file = str_replace('2.mp4', '20.mp4', $file);

        //$command[] = 'ffmpeg -ss 00:00:'.$start.' -t 00:00:'.$end.' -y -i '.$file.' -c copy -bsf:v h264_mp4toannexb -an -f mpegts ' . $ts_file;

        $command[] = '/usr/local/ffmpeg/bin/ffmpeg -ss 00:00:'.$start.' -t 00:00:'.$end.' -y -i '.$file.' -an -vf "drawtext=fontfile=/share/lib/php/dever_package/poster/fonts/PingFang_Bold.ttf: text=\''.$text.'\':x='.$x.':y='.$y.':fontsize='.$fontsize.':fontcolor=white:shadowy=2" -b:v 10000k ' . $ts_file;

        $convert[] = $ts_file;
        $output = 'test.mp4';

        $command[] = '/usr/local/ffmpeg/bin/ffmpeg -y -i "concat:'.implode('|', $convert).'" -acodec copy -vcodec copy -bsf:a aac_adtstoasc -movflags +faststart ' . $output;

        $command = implode(' && ', $command);

        print_r($command);die;

        /*
        视频合并
        ffmpeg -i 10.mp4 -c copy -bsf:v h264_mp4toannexb -y -f mpegts 10.ts
        ffmpeg -i 20.mp4 -c copy -bsf:v h264_mp4toannexb -y -f mpegts 20.ts
        ffmpeg -i "concat:10.ts|20.ts" -y -c copy -an -bsf:a aac_adtstoasc -movflags +faststart ts.mp4

        # 淡入淡出
        ffmpeg -i 1.mp4 -vf fade=in:0:90 out.mp4

        # 镜像
        ffmpeg -i 1.mp4 -vf "hflip" outut.mp4


        ffmpeg -i 1.mp4 -i 2.mp4 -filter_complex "[0:v]fade=t=out:st=3:d=1[vf];[1:v]fade=t=in:st=0:d=1[vf1];[vf][vf1]overlay=enable='between(t,3,4)'[v]" -map "[v]" -map 0:a output.mp4


        # 等比缩放+模糊背景

        # 同一个视频分开为a,b 两个流
        # a -> 按照308:544(544/960比例)截取视频 -> e -> 使用模糊效果 -> f -> 放大到544/960 尺寸 -> 1
        # b -> 缩小到544/-1(544/960比例) 尺寸 -> 2
        # 将 2 放到 1 的视频中间。

        ffmpeg -i 1.mp4 -vf "split[a][b];[a]crop=ih*(544/960):ih[e];[e]boxblur=5:5[f];[f]scale=544:960[1];[b]scale=544:308[2];[1][2]overlay=0:326" {commonFFmpegArg} -y 10.mp4

        ffmpeg -i /video_splicing/2077357_0_55257.ts -vf scale=544:-1,pad=544:960:0:326:black {commonFFmpegArg} -y /video_splicing/2077357_0_55257_scale.ts


        竖屏转横屏
        ffmpeg -i 1.mp4 -metadata:s:v rotate="0" -c:v copy -c:a copy 10.mp4
        ffmpeg -i 10.mp4  -vf "transpose=1,transpose=2"  out.mp4


        ffmpeg -i 2.mp4 -vf "transpose=1" -c:a copy 20.mp4
        ffmpeg -i 20.mp4 -vf scale=1920:1080,setsar=1:1 -c:a copy 200.mp4

        ffmpeg -i 200.mp4 -filter_complex "[0:v]split[vleft][vtmp];[vtmp]crop=iw/2:ih:ow-iw/2:0,boxblur=luma_radius=min(h\,w)/20:luma_power=1:chroma_radius=min(cw,ch)/20:chroma_power=1[crop_left];[vleft][crop_left]hstack" -c:a copy 2out.mp4


        ffmpeg -i 1.mp4 -vf "drawtext=fontfile=arial.ttf:text='welcome':x=(w-tw)/2:y=(h-th)/2" output.mp4

        ffmpeg -i 1.mp4 -i 2.mp4 -filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" output.mp4

        ffmpeg -i 1.mp4 -vf "drawtext=text='@小新小新':fontfile=/share/lib/php/dever_package/poster/fonts/PingFang_Bold.ttf:fontsize=30:y=h/2:x=w-tw-mod(t*50\, w-tw)" output.mp4

        ffmpeg -i 1.mp4 -vf "drawtext=text=string1 string2 string3 string4 string5 string6 string7 :expansion=normal:fontfile=foo.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.mp4 -vf "drawtext=fontfile=/share/lib/php/dever_package/poster/fonts/PingFang_Bold.ttf:fontsize=30:text='@我是鱼小新':x=0:y=h-th:enable='between(t,0,10)',drawtext=fontfile=arial.ttf:fontsize=30:text='Hello,World':x=w-tw:y=0:enable='between(t,10,20)'" output.mp4

        上下对称，水面效果
        ffmpeg -i 1.mp4 -filter_complex "[0:v]pad=h=2*ih[a];[0:v]vflip[b];[a][b]overlay=y=h" duichen3.mp4 -y

        左右对称，镜面效果
        ffmpeg -i 1.mp4 -filter_complex "[0:v]pad=w=2*iw[a];[0:v]hflip[b];[a][b]overlay=x=w" duichen2.mp4 -y

        //将视频的上半部分翻转，并覆盖在下半部分的区域
        ffmpeg -i 4.mp4 -vf "split [main][tmp];[tmp] crop=iw:ih/2:0:0, vflip [flip];[main][flip] overlay=0:H/2" 44.mp4 -y

        随机跑马灯
        ffmpeg -i input.mp4 -vf "drawtext=text='hello,mokev.com':fontfile=font.ttf:fontsize=24:fontcolor=white:x=if(eq(mod(trunc(t*50/(w-text_w))\,2)\,0)\,mod(t*50\,w-text_w)\,w-text_w-mod(t*50\,w-text_w)):y=if(eq(mod(trunc(t*50/(h-text_h))\,2)\,0)\,mod(t*50\,h-text_h)\,h-text_h-mod(t*50\,h-text_h))" output.mp4

        */
    }
}