php 如何使用php从图像创建视频?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9268842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create videos from images with php?
提问by mirza
Let's say I have 10 images and I want to combine those images in a video like a slideshow.
假设我有 10 张图像,我想将这些图像组合在一个视频中,就像幻灯片一样。
For example I want to show each image for 5 seconds and then continue with next image for another 5 seconds.
例如,我想将每个图像显示 5 秒钟,然后再继续显示下一个图像 5 秒钟。
If it's possible, it will be perfect to include music and some descriptive text too.
如果可能,最好包含音乐和一些描述性文字。
Is there a sample code for this may be with ffmpeg library ?
ffmpeg 库中是否有这方面的示例代码?
采纳答案by Timeout
My first thought was to shell out to the ffmpeg command with something like this.
我的第一个想法是用类似这样的东西对 ffmpeg 命令进行攻击。
Creating a Video from Images
ffmpeg can be used to stitch several images together into a video. There are many options, but the following example should be enough to get started. It takes all images that have filenames of XXXXX.morph.jpg, where X is numerical, and creates a video called "output.mp4". The qscale option specifies the picture quality (1 is the highest, and 32 is the lowest), and the "-r" option is used to specify the number of frames per second.
ffmpeg -r 25 -qscale 2 -i %05d.morph.jpg output.mp4
(The website that this blurb was taken from is gone. Link has been removed.)
从图像创建视频
ffmpeg 可用于将多个图像拼接成一个视频。有很多选择,但下面的例子应该足以开始。它获取文件名为 XXXXX.morph.jpg 的所有图像,其中 X 是数字,并创建一个名为“output.mp4”的视频。qscale选项指定图片质量(1为最高,32为最低),“-r”选项用于指定每秒帧数。
ffmpeg -r 25 -qscale 2 -i %05d.morph.jpg output.mp4
(此简介来自的网站已不复存在。链接已删除。)
Where 25 means 25 images per second. You could set this to 1 for a slight (1 sec) delay or use decimals, IE: 0.5 for a 2 second delay.
其中 25 表示每秒 25 张图像。您可以将其设置为 1 以表示轻微(1 秒)延迟或使用小数,即:0.5 表示 2 秒延迟。
You can then combine a video and audio stream with something like this.
然后,您可以将视频和音频流与这样的内容结合起来。
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -b:a 128k final.mp4
Of course choose your appropriate codecs. If you want an mp4 use libx264 for video and aac (built into ffmpeg and no longer "experimental") for audio.
当然,选择合适的编解码器。如果你想要一个 mp4 使用 libx264 视频和 aac(内置在 ffmpeg 中,不再是“实验性的”)音频。
Just remember that if you choose to use a method like this that ffmpeg output goes, by default, to stderr for when you try to read it. It can be redirected to stdout if you prefer.
请记住,如果您选择使用这样的方法,默认情况下,当您尝试读取它时,ffmpeg 输出会转到 stderr。如果您愿意,可以将其重定向到标准输出。
回答by M. Laing
The first thing that came to mind for me was imagemagick. I've used it with PHP for a lot of image manipulation and I know it supports reading a decent amount of video formats and according to that link it supports writing to some too.
我首先想到的是imagemagick。我已经将它与 PHP 一起用于大量图像处理,我知道它支持读取相当数量的视频格式,并且根据该链接,它也支持写入一些视频格式。
回答by Michal
yes, ffmpeg is the right solution for you. i just recently made something similar - a video site with animated thumbnails. i used ffmpeg to put together images in an aminated gif. however, the output can be whatever you need... unfortunately, in my searches into this topic i have not found any sample code that would combine all the points you are after, so i suppose you will have to try manually with ffmpeg... in my project i used php video toolkit http://sourceforge.net/projects/phpvideotoolkit/in some parts to make it a bit easier...
是的,ffmpeg 是适合您的解决方案。我最近刚刚制作了类似的东西 - 一个带有动画缩略图的视频网站。我使用 ffmpeg 将图像放在一个胺化的 gif 中。但是,输出可以是您需要的任何内容......不幸的是,在我对这个主题的搜索中,我没有找到任何可以结合您所追求的所有要点的示例代码,所以我想您将不得不使用 ffmpeg 手动尝试.. . 在我的项目中,我在某些部分使用了 php 视频工具包http://sourceforge.net/projects/phpvideotoolkit/以使其更容易...
回答by Makio
You can use blend effect with ffmpeg:
您可以在 ffmpeg 中使用混合效果:
ffmpeg -framerate 20 \
-loop 1 -t 0.5 -i 1.jpg \
-loop 1 -t 0.5 -i 2.jpg \
-loop 1 -t 0.5 -i 3.jpg \
-loop 1 -t 0.5 -i 4.jpg \
-c:v libx264 \
-filter_complex " \
[1:v][0:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b1v]; \
[2:v][1:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b2v]; \
[3:v][2:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b3v]; \
[0:v][b1v][1:v][b2v][2:v][b3v][3:v]concat=n=7:v=1:a=0,format=yuv420p[v]" -map "[v]" out.mp4
You should check bellow link for more effect of ffmpeg :D
您应该检查以下链接以获取 ffmpeg 的更多效果:D