gpt4 book ai didi

javascript - 使用 php 将视频转换为 MP4

转载 作者:行者123 更新时间:2023-11-28 17:50:00 25 4
gpt4 key购买 nike

这是我的问题:在我的网站上,我想允许用户上传任何类型的视频。但对于 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");


}
}
}

最佳答案

这是我的做法:

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

<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;

}
}
}

}

?>

测试一下并让我知道效果如何。如果您有任何其他问题或需要任何其他信息,请随时询问。我随时为您提供帮助,并且希望您对该文件进行完整编码。祝兄弟好运!

关于javascript - 使用 php 将视频转换为 MP4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45771853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com