使用php将视频转换为MP4 [英] Convert video to MP4 with php
问题描述
这是我的问题:在我的网站上,我想让用户上传任何类型的视频。但是使用HTML5标签只能使用.mp4视频。
所以我想将用户提交的任何类型的视频转换为MP4,然后添加数据库的路径。
我读过有关FFmpeg的内容但是我无法弄清楚如何使用它。我尝试使用shell_exec(ffmpeg -i。$ vid。-vcodec libx264 -crf 20 out.mp4 2>& 1),但没有成功。
html
< form method =postenctype =multipart / form-dataname =formaction =post.php> < input type =filename =media-vidclass =file_multi_videoaccept =video / *> < / form>
php脚本:
以下是我的做法:
试用此代码! (已测试并正常工作)
< form method =postenctype =multipart / form-dataname =form> < input type =filename =media-vidclass =file_multi_videoaccept =video / *> < input type =submitname =submitvalue =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。基本名($ _ FILES [ 媒体-VID] [ 名称]);
$ uploadOk = 0;
$ videotype = pathinfo($ target_filevid,PATHINFO_EXTENSION);
//这些是可以上传的有效视频格式和
//他们将全部转换为.mp4
$ video_formats = array(
mpeg,
mp4,
mov,
wav,
avi,
dat,
flv,
3gp
);
foreach($ video_formats as $ valid_video_format){
//你可以使用in_array,如果(preg_match(/ $ videotype / i,$ valid_video_format)){
$ target_filevid = $ targetvid。 basename($ _ FILES [media-vid]。.mp4);
$ uploadOk = 1;
休息;
} else {
//如果是图片或其他文件格式,则不接受
$ format_error =无效的视频格式!;
}
}
if($ _FILES [media-vid] [size]> 500000000){
$ uploadOk = 0;
回声对不起,你的文件太大了。;
}
//检查$ uploadOk是否设置为0错误
if($ uploadOk == 0&& isset($ format_error)){
echo $ message;
//如果一切正常,请尝试上传文件
}否则if($ uploadOk == 0){
回声抱歉,您的视频未上传。;
}
其他{
$ 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对不起,上传文件时出错,请重试。;
} else {
$ vid = $ target_dirvid。 $ target_filevid;
}
}
}
}
?>
测试它并让我知道它是如何发生的。如果您有任何其他问题或需要任何其他信息,请不要犹豫,问问。我一直在这里提供帮助,我希望你能够完整编码这个文件。祝你好运!
here is my problem : On my website I want to allow users to upload any type of video. But with the HTML5 tag only .mp4 video can be used. So I want to convert any type of video submit by the user to MP4 then add the path to the databse.
I've read something about FFmpeg but I can't figure out how to use it. I tried to use shell_exec("ffmpeg -i ".$vid." -vcodec libx264 -crf 20 out.mp4 2>&1") without success.
The 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>
The php script:
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");
}
}
}
Here is how I would do it:
Try out this code! ( Tested and works fine )
<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;
}
}
}
}
?>
Test it and let me know how it goes. If you have any other questions or need anything else, please do not hesitate to ask. I am always here to help and I would like you to have this file fully coded. Good luck bro!
这篇关于使用php将视频转换为MP4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!