简体   繁体   English

ffmpeg转换任何用户视频文件以在手机上运行h264 mp4

[英]ffmpeg convert any user video file to run on mobiles h264 mp4

So, i have kind of accepted this task on work but im really not sure if its possible. 因此,我有点接受这项工作任务,但我真的不确定是否可行。

We are going to build a website where users can upload videos from their computers and mobile phone browsers. 我们将建立一个网站,让用户可以从他们的计算机和手机浏览器上传视频。 The video files can be a large range of aspect ratios, width, height, codex and file formats. 视频文件的宽高比,宽度,高度,编码和文件格式可以很大。

I will have access to ffmpeg from php exec command on a web server. 我将可以从Web服务器上的php exec命令访问ffmpeg。

Is it possible to use this to convert the user files to one file format that works on computers, android and iphone. 是否可以使用它来将用户文件转换为可在计算机,Android和iPhone上使用的一种文件格式。

The requirements is that we can set a max width, to witch the video will be scaled, dynamically to match height. 要求是我们可以设置最大宽度,以动态调整视频以匹配高度,以适应视频的缩放。

Does anyone know is this can be done, and be done in a reasonable amount of time. 谁知道这是可以做到的,并且要在合理的时间内完成。 Will do project on 2 days. 将在2天内进行项目。 And if so some pointers in the right direction would be nice. 如果是这样,那么朝正确方向的一些指针将是不错的选择。

Had the same problem but solved by using HandBrake the open source video transcoder 遇到了同样的问题,但是通过使用HandBrake开源视频代码转换器解决了

https://handbrake.fr/ https://handbrake.fr/

If your target can only be one file format, then I would choose mp4 baseline. 如果您的目标只能是一种文件格式,那么我将选择mp4基准。 (However some browsers won't play it, which is why the html tag offers multiple source flags, which usually include webm and ogg video...) (但是,某些浏览器不会播放它,这就是html标记提供多个源标志的原因,这些标志通常包括webm和ogg视频...)

Using ffprobe -show_streams $uploadedFile you can get the dimensions (and aspect ratio) of the file. 使用ffprobe -show_streams $uploadedFile您可以获取文件的尺寸(和纵横比)。 Using math you can get the new dimensions based on your needs. 使用数学,您可以根据需要获得新的尺寸。

$newDim=$new_width.":".$new_height;
$output  = shell_exec("/usr/bin/ffmpeg -i $uploadedFile -f mp4 \
-c:a libfdk_aac -b:a 128k -c:v libx264 -vprofile baseline \
-movflags faststart -vf scale=$newDim $output");```

Here is the breakdown: 这是细分:

f mp4              > format mp4
c:a libfdk_aac     > audio codec
c:v libx264        > video codec
vprofile baseline  > minimal codec usage for mobile
movflags faststart > put the moov atom at the beginning of file
$output            > should have '.mp4' as a file ending

Of course the devil is in the details (and the number of processing cores you can throw at an online converter), but this will get you up and running at least. 当然,关键在于细节(以及可以在在线转换器上投入的处理核心数量),但这至少可以使您正常运行。

Edit: Actually answered the question. 编辑:实际回答了这个问题。 :) :)

By the way, ffmpeg does offer the vf flag: -vf scale=320,-1 , but sometimes it gives you a dimension not divisible by 2 which throws an error in x264 encoding. 顺便说一句,ffmpeg确实提供了vf标志: -vf scale=320,-1 ,但是有时它为您提供了一个不能被2整除的维,这会在x264编码中引发错误。 Its better to do the math yourself. 最好自己做数学。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM