Home Backend Development PHP Tutorial A PHP class for FFMPEG video conversion

A PHP class for FFMPEG video conversion

Jul 28, 2016 am 08:26 AM
ftp server this

<?php
class local_video
{
    public $options;
    public $ffmpeg;
    public $phpcms_path;
    public $backup;
    function __construct($options,$ffmpeg,$backup=true)
    {
        $this->opti
        $this->options = array_filter($options) + $this->options;
        $this->ffmpeg=$ffmpeg;	//ffmpeg路径
        $this->backup=$backup;
    }
    //获取视频信息
    function video_info($file)
    {
        ob_start();
        passthru(sprintf($this->ffmpeg . ' -i "%s" 2>&1', $file));//ffmpeg -i test.avi 2>&1
        $info = ob_get_contents();
        ob_end_clean();
        // 通过使用输出缓冲,获取到ffmpeg所有输出的内容。
        $ret  = array();
        // Duration: 01:24:12.73, start: 0.000000, bitrate: 456 kb/s
        if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/",$info, $match))
        {
            $ret['duration'] = $match[1]; // 提取出播放时间
            $da              = explode(':', $match[1]);
            $ret['seconds']  = $da[0] * 3600 + $da[1] * 60 + $da[2]; // 转换为秒
            $ret['start']    = $match[2]; // 开始时间
            $ret['bitrate']  = $match[3]; // bitrate 码率 单位 kb
        }
        // Stream #0.1: Video: rv40, yuv420p, 512x384, 355 kb/s, 12.05 fps, 12 tbr, 1k tbn, 12 tbc
        if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match))
        {
            $ret['vcodec']     = $match[1]; // 编码格式
            $ret['vformat']    = $match[2]; // 视频格式
            $ret['resolution'] = $match[3]; // 分辨率
            $a                 = explode('x', $match[3]);
            $ret['width']      = $a[0];
            $ret['height']     = $a[1];
        }
        // Stream #0.0: Audio: cook, 44100 Hz, stereo, s16, 96 kb/s
        if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match))
        {
            $ret['acodec']      = $match[1];       // 音频编码
            $ret['asamplerate'] = $match[2];  // 音频采样频率
        }
        if (isset($ret['seconds']) && isset($ret['start']))
        {
            $ret['play_time'] = $ret['seconds'] + $ret['start']; // 实际播放时间
        }
        $ret['size'] = filesize($file); // 文件大小
        return $ret;
    }
    function convert()
    {
        $verifyToken = md5('unique_salt' . $this->options['timestamp']);
        if ($verifyToken == $this->options['token'])
        {
            $orgFile  = $this->options['org_path'] . $this->options['org'];
            $setting=' ';
            if(isset($this->options['video_size']))
                $setting=$setting.'-vf scale="'.$this->options['video_size'].'"';
            $mp4      = $this->ffmpeg . ' -i  '  . $orgFile . ' -ss 00:01:02 -vcodec libx264 -strict -2 '.$setting.' ' . $this->options['mp4_path_temp'] . $this->options['org'] . '';	//转换视频
            exec($mp4);
            if(isset($this->options['watermark'])){
            	$watermark=' "'.$this->options['watermark'].'" ';
            	$mp4 = $this->ffmpeg.' -i '.$this->options['mp4_path_temp'] . $this->options['org'] . ''.' -vf '.$watermark.' '.$this->options['mp4_path'] . $this->options['org'] . '';	//增加水印
            	exec($mp4);
            	@unlink($this->options['mp4_path_temp'] . $this->options['org'] . '');
            }
            $duration = $this->video_info($orgFile);
            $seconds  = intval($duration['seconds']);
            $offset   = intval($seconds / 21);	//截图间隔 秒
            $thumbs = explode(',', $this->options['thumb_size']);
            for ($i = 0; $i <= count($thumbs); $i++)
            {
                $targetPath = $this->options['thumb_path'] . $this->options['org'] . '/';
                if (!file_exists($targetPath))
                    @mkdir(rtrim($targetPath, '/'), 0777);
                if ($i == 0)
                {
                    $time     = 1;
                    $name     = $i == 0 ? 'default.jpg' : $i . '.jpg';
                    $img_size = $this->options['main_size'];
                }
                else
                {
	                $time       = $i * $offset;
	                $name       = $i . '.jpg';
	                $img_size   = $thumbs[$i-1];
                }
                $jpg = $this->ffmpeg . ' -i  ' . $orgFile . ' -f  image2  -ss ' . $time . ' -vframes 1  -s ' . $img_size . ' ' . $targetPath . $name;	//截图
                @exec($jpg);
            }
            //复制文件到对应的FTP服务器
            $ftp_server    = pc_base::load_config('ftp_server');
            $remote_server = $_POST['remote_server'];
            $ftp_server    = $ftp_server[$remote_server];
            if($ftp_server['ftp_server'])
                pc_base::ftp_upload($orgFile,
                                $ftp_server['ftp_server'],
                                $ftp_server['ftp_user_name'],
                                $ftp_server['ftp_user_pass']);
            //备份到所有FTP服务器
            if($this->backup){
                $ftp_backup = pc_base::load_config('ftp_backup');
                foreach ($ftp_backup as $v)
                {
                    pc_base::ftp_upload( $orgFile,
                                        $ftp_backup['ftp_server'],
                                        $ftp_backup['ftp_user_name'],
                                        $ftp_backup['ftp_user_pass']);
                }
            }
            $result['url']=$ftp_server['ftp_server']['http_address']. $this->options['mp4_path'] . $this->options['uniqid'] . '.mp4';//记录视频播放地址
            $result['uniqid']=$this->options['uniqid'];
            $result['videoTime'] = $this->video_info($orgFile);
            $result['videoTime'] = $result['videoTime']['seconds'];
            return $result;//返回处理结果
        }
        else
            die('验证失败!');
    }
}
?>
Copy after login

Reference

http://www.fieryrain.com/blog/FFMPEG_VIDEO_TIME

http://keren.iteye.com/blog/1773536

http://www.cnblogs.com/ dwdxdy/p/3240167.html

http://www.cnblogs.com/chen1987lei/archive/2010/12/03/1895242.html

The above introduces a PHP class for FFMPEG video conversion, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up nginx reverse proxy ftp server How to set up nginx reverse proxy ftp server May 17, 2023 am 09:31 AM

1. Install nginx 2. Install vsftpd 3. Modify the nginx configuration file nginx.conf 3.1 Add the ftp user userftpuser in the first line; 3.2 Configure the relevant path server{ listen80; #nginx proxy port server_namelocalhost; #ftp server address location/images{root /home/ftpuser; #The absolute path of the folder of the proxy ftp server indexftpuser; #Set the welcome page

Using FTP in Go: A Complete Guide Using FTP in Go: A Complete Guide Jun 17, 2023 pm 06:31 PM

With the rapid development of the Internet, File Transfer Protocol (FTP) has always been an important file transfer method. In Go language, using FTP to transfer files may be a need of many developers. However, maybe many people don't know how to use FTP in Go language. In this article, we will explore how to use FTP in Go language, from connecting to FTP server to file transfer, and how to handle errors and exceptions. Create FTP connection In Go language, we can use the standard "net" package to connect to FTP

PHP and FTP: realizing file sharing among multiple departments in website development PHP and FTP: realizing file sharing among multiple departments in website development Jul 28, 2023 pm 01:01 PM

PHP and FTP: Achieve file sharing among multiple departments in website development. With the development of the Internet, more and more companies are beginning to use website platforms for information release and business promotion. However, the problem that arises is how to achieve file sharing and collaboration among multiple departments. In this case, PHP and FTP become one of the most commonly used solutions. This article will introduce how to use PHP and FTP to achieve file sharing among multiple departments in website development. 1. Introduction to FTP FTP (FileTransferPr

What are the ftp commands under linux? What are the ftp commands under linux? Mar 21, 2023 am 09:59 AM

The ftp commands under Linux include: 1. ftp command; 2. close command; 3. disconnect command; 4. open command; 5. user command; 6. account command; 7. bye command; 8. quit command; 9. help command ;10. rhelp command; 11. ascii command; 12. binary/bi command; 13. bell command, etc.

How to compare directories and files on an FTP server via PHP How to compare directories and files on an FTP server via PHP Jul 28, 2023 pm 02:09 PM

How to compare directories and files on an FTP server through PHP In web development, sometimes we need to compare local files with files on the FTP server to ensure consistency between the two. PHP provides some functions and classes to implement this functionality. This article will introduce how to use PHP to compare directories and files on an FTP server, and provide relevant code examples. First, we need to connect to the FTP server. PHP provides the ftp_connect() function to establish an FTP server

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

What does linux ftp 530 mean? What does linux ftp 530 mean? Mar 14, 2023 am 10:16 AM

linux ftp530 means linux ftp login error 530. The solution is: 1. Check "cat /etc/shells" to see if your user's home directory and login shell are there. If not, add them; 2. Check "/var /log/secure" file and reset the password expiration time.

How to use FTP to upload files in Python How to use FTP to upload files in Python Apr 29, 2023 am 09:49 AM

Introduction to FTP FTP is File Transfer Protocol (FileTransferProtocol), which is a standard protocol for file transfer on the network. FTP client can upload files from local to server or download from server to local. The ftplib module Python provides a standard library ftplib for implementing FTP client functions in Python. Using ftplib, we can connect to the FTP server and perform various FTP operations, such as uploading and downloading files, etc. Code explanation The following is a sample code for uploading files through FTP using Python: fromftplibimportFTPimportargparsed

See all articles