用ffmpeg连接两个视频

时间:2019-03-09 22:45:07

标签: ffmpeg

我有两个视频。一个将通过youtube-dl下载,另一个将通过ffmpeg和循环功能由图像制成。

public function createTimePic($id, $image_width, $image_height, $text) {
    $font_size = 20;
    $angle = 0;
    $font = 'public/fonts/arial.ttf';
    $im = imagecreatetruecolor($image_width, $image_height);
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, $image_width, $image_height, $black);

    $text_box = imagettfbbox($font_size, $angle, $font, $text);
    $text_width = $text_box[2]-$text_box[0];
    $text_height = $text_box[7]-$text_box[1];
    $x = ($image_width/2) - ($text_width/2);
    $y = ($image_height/2) - ($text_height/2);

    imagettftext($im, $font_size, 0, $x, $y+1, $grey, $font, $text);
    imagettftext($im, $font_size, 0, $x, $y, $white, $font, $text);

    $file = $this->videoPath.'time_difference_'.$id.'.jpg';
    imagejpeg($im, $file);

    $video = 'time_difference_'.$id.'.mp4';
    if (!file_exists($this->videoPath.$video)) {
        $this->createVideoFromImg($file, $video);
    }

    return [
        'img' => $file,
        'video' => $video
    ];
}

public function createVideoFromImg($img, $video) {
    $command = './ffmpeg -loop 1 -i '.$img.' -t 5 -c:v libx264 -preset slow -crf 18 -c:a copy -pix_fmt yuv420p '.$this->videoPath.$video;
    exec($command, $output);
}

public function downloadYouTubeVideo($video_id, $audio_only = false) {
    $file_type = $audio_only ? 'mp3' : 'mp4';
    $file = $this->videoPath.$video_id.'.'.$file_type;

    if (!file_exists($file)) {
        $command = '/usr/local/bin/youtube-dl -o "'.$file.'" ';
        if ($audio_only) {
            $command .= '--extract-audio --audio-format '.$file_type.' ';
        }

        $command .= ' https://www.youtube.com/watch\?v\='.$video_id;
        exec($command, $output);

        $command = './ffmpeg -i '.$file.' -c:v libx264 -preset slow -crf 18 -c:a copy -pix_fmt yuv420p '.str_replace('.mp4', '', $file).'_encoded.mp4';
    }

    return $file;
}

$command = './ffmpeg -f concat -safe 0 -i list.txt -c copy '.$this->videoPath.$id.'_new1.mp4';
exec($command, $output);

在我的list.txt文件中是这两个视频的路径,一个是从jpeg创建的,另一个是从youtube下载的。在底部运行该命令可创建一个新视频。只是和youtube视频是一样的东西。永远不会添加其他图像。可能与编码有关吗?

0 个答案:

没有答案