【问题标题】:Convert video to MP4 with php使用 php 将视频转换为 MP4
【发布时间】:2017-08-19 13:02:06
【问题描述】:

这是我的问题:在我的网站上,我希望允许用户上传任何类型的视频。但是对于 HTML5 标签,只能使用 .mp4 视频。 所以我想将用户提交的任何类型的视频转换为 MP4,然后将路径添加到数据库。

我读过一些关于 FFmpeg 的文章,但我不知道如何使用它。我尝试使用 shell_exec("ffmpeg -i ".$vid." -vcodec libx264 -crf 20 out.mp4 2>&1") 没有成功。

html

<form method="post" enctype="multipart/form-data" name="form" action="post.php">
			
																					
	<input type="file" name="media-vid"  class=" file_multi_video" accept="video/*">
														
</form>

php 脚本:

if(file_exists($_FILES['media-vid']['tmp_name']) && is_uploaded_file($_FILES['media-vid']['tmp_name']))
	{
		
		
		
		$targetvid = md5(time());
		$target_dirvid = "videos/";
		$target_filevid =  $targetvid.basename($_FILES["media-vid"]["name"]);
		$uploadOk = 1;
		$videotype = pathinfo($target_filevid,PATHINFO_EXTENSION);
	
		
			
		if ($_FILES["media-vid"]["size"] > 500000000) {
		$uploadOk = 0;
		echo "Sorry, your file is too large.";
		}
	
	// Check if $uploadOk is set to 0 by an error
	if ($uploadOk == 0) {
		echo "Sorry, your video was not uploaded."; 
	// if everything is ok, try to upload file
		} else {
		
		 $target_filevid = strtr($target_filevid, 
			  'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 
			  'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
		 $target_filevid = preg_replace('/([^.a-z0-9]+)/i', '_', $target_filevid);
	   
		if (!move_uploaded_file($_FILES["media-vid"]["tmp_name"], $target_dirvid. $target_filevid)) {
		 
			echo "Sorry, there was an error uploading your file. Please retry.";
		}else{
			
			$vid= $target_dirvid.$target_filevid;
	
			shell_exec("ffmpeg -i ".$vid." -vcodec libx264 -crf 20  out.mp4 2>&1");

		
			}
		}
	}
	
	

【问题讨论】:

  • 我们无法查看shell_exec 的结果。试试看怎么样?
  • 它实际上什么也没做。我使用正确吗?
  • 再次:打印函数结果。
  • 我该怎么做?

标签: javascript php html


【解决方案1】:

我会这样做:

试试这个代码! (经过测试,工作正常)

<form method="post" enctype="multipart/form-data" name="form">
			
																					
<input type="file" name="media-vid"  class=" file_multi_video" accept="video/*">
  
<input type="submit" name="submit" value="upload"/>	

</form>
        <?

    if (isset($_POST['submit'])) {

        if (file_exists($_FILES['media-vid']['tmp_name']) && is_uploaded_file($_FILES['media-vid']['tmp_name'])) {

            $targetvid     = md5(time());
            $target_dirvid = "videos/";

            $target_filevid = $targetvid . basename($_FILES["media-vid"]["name"]);

            $uploadOk = 0;

            $videotype = pathinfo($target_filevid, PATHINFO_EXTENSION);

    //these are the valid video formats that can be uploaded and
                  //they will all be converted to .mp4

            $video_formats = array(
                "mpeg",
                "mp4",
                "mov",
                "wav",
                "avi",
                "dat",
                "flv",
                "3gp"
            );

            foreach ($video_formats as $valid_video_format) {

      //You can use in_array and it is better

                if (preg_match("/$videotype/i", $valid_video_format)) {
                    $target_filevid = $targetvid . basename($_FILES["media-vid"] . ".mp4");
                    $uploadOk       = 1;
                    break;

                } else {
              //if it is an image or another file format it is not accepted
                    $format_error = "Invalid Video Format!";
                }

            }

            if ($_FILES["media-vid"]["size"] > 500000000) {
                $uploadOk = 0;
                echo "Sorry, your file is too large.";
            }

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0 && isset($format_error)) {

                echo $message;

                // if everything is ok, try to upload file

            } else if ($uploadOk == 0) {


                echo "Sorry, your video was not uploaded.";

            }

            else {

                $target_filevid = strtr($target_filevid, 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
                $target_filevid = preg_replace('/([^.a-z0-9]+)/i', '_', $target_filevid);

                if (!move_uploaded_file($_FILES["media-vid"]["tmp_name"], $target_dirvid . $target_filevid)) {

                    echo "Sorry, there was an error uploading your file. Please retry.";
                } else {

                    $vid = $target_dirvid . $target_filevid;

                }
            }
        }

    }

    ?>

测试它,让我知道它是怎么回事。如果您有任何其他问题或需要其他任何东西,请随时提出。我总是在这里为您提供帮助,我希望您对这个文件进行完全编码。祝兄弟好运!

【讨论】:

  • 我还有一个问题:stackoverflow.com/q/45773272/8186242你能帮帮我吗?很抱歉打扰您
  • @Dilak 您是否设法将视频路径保存到 MySQL 数据库表中?如果不告诉我,我可以帮助您在 MySQL 数据库表中存储和检索视频!
  • 是的,伙计!谢谢 -) 你帮了我很多!
  • 我又有问题了。我使用此方法转换的视频无法使用 video 标签播放。该视频实际上已转换,我可以使用 VLC 播放每个示例,但在我的网站上它无法正常工作。你能帮帮我吗?
  • 你能帮帮我吗? @MrProPop
猜你喜欢
  • 2018-02-14
  • 2015-06-14
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 2010-12-22
  • 2018-11-15
相关资源
最近更新 更多