Turn image sequence into video with transparency

2019-01-31 07:42发布

I've got what seems like it should be a really simple problem, but it's proving much harder than I expected. Here's the issue:

I've got a fairly large image sequence consisting of numbered frames (output from Maya, for what its worth). The images are currently in Targa (.tga) format, but I could convert them to PNGs or other arbitrary format if that matters. The important thing is, they've got an alpha channel.

What I want to do is programatically turn them into a video clip. The format doesn't really matter, but it needs to be lossless and have an alpha channel. Uncompressed video in a Quicktime container would probably be ideal.

My initial thought was ffmpeg, but after wasting most of a day on it it seems it's got no support at all for alpha channels. Either I'm missing something, or the underlying libavcodec just doesn't do it.

So, what's the right way here? A command line tool like ffmpeg would be nice, but any solution that runs on Windows and could be called from a script would be fine.

Note: Having an alpha chanel in your video isn't actually all that uncommon, and it's really useful if you want to composite it on top of another video clip or a still image. As far as I know uncompressed video, the Quicktime Animation codec, and the Sorenson Video 3 codec all support tranparency, and I've heard H.264 does as well. All we're really talking about is 32-bit color depth, and that's pretty widely supported; both Quicktime .mov files and Windowss .avi files can handle it, and probably a lot more too.

Quicktime Pro is more than happy to turn an image sequence into a 32-bit .mov file. Hit export, change color depth to "Millions of Colors+", select the Animation codec, crank the quality up to 100, and there you are - losslessly compressed video, with an alpha chanel, and it'll play back almost anywhere since the codec has been part of Quicktime since version 1.0. The problem is, Quicktime Pro doesn't have any sort of command-line interface (at least on Windows). ffmpeg supports encoding using the Quicktime Animation codec (which it calls qtrle), but it only supports a bit-depth of 24 bits.

The issue isn't finding a video format that supports an alpha channel. Quicktime Animation would be ideal, but even uncompressed video should work. The problem is finding a tool that supports it.

5条回答
男人必须洒脱
2楼-- · 2019-01-31 07:50

For web developers reaching this question and banging their heads against the wall in frustration… It is possible to create a transparent WebM video, but at the moment you might need to compile ffmpeg and the required libraries from source.

I wanted to display a rendered Blender video in a website but preserve the transparency. The first step was to render the Blender output as individual PNG files. After that, I spent quite a while trying to coerce ffmpeg to convert those PNG files into a single video. The basic command is simple:

ffmpeg -i input%04d.png output.webm

This command loads all PNGs with the filenames input0000.png through input9999.png and turns them into a video. The transparency was promptly lost. Combing through the output I realized ffmpeg was helpfully selecting a non-transparent format:

Incompatible pixel format 'yuva420p' for codec 'flv', auto-selecting format 'yuv420p'

At this point I was realizing I might have to recompile ffmpeg from scratch. I struggled with a few other tools, but ultimately ended up back with ffmpeg. After compiling libvbx and ffmpeg from the latest source, things worked a charm.

查看更多
该账号已被封号
3楼-- · 2019-01-31 07:51

Cody,

You can write your own command line utility using the Quicktime SDK for Windows, I would recommend sticking with the higher level Quicktime COM apis and only delving into the C-APIs if you really have to.

-Nick

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-31 07:53

I'm assuming there's a strong possibility that you or others would want to use such video in an interactive flash piece. Here's a tutorial on doing so including ffmpeg compiling to uncompressed avi with alpha channel from png.

http://away3dtutorials.blogspot.com/2011/02/ffmpeg-to-compile-jpeg-to-video-with.html

查看更多
神经病院院长
5楼-- · 2019-01-31 07:57

I know this topic is a bit old, but I am posting anyway.

FFMPEG with Quicktime Animation (RLE) or FFVHUFF/HUFFYUV will do.

  • ffmpeg -i yoursequence%d.png -vcodec qtrle movie_with_alpha.mov
  • ffmpeg -i yoursequence%d.png -vcodec ffvhuff movie_with_alpha.avi
  • ffmpeg -i yoursequence%d.png -vcodec huffyuv movie_with_alpha.avi

You will get video files with transparency(alpha channel) preserved.

I have also heard On2-VP6 variation (Not the WebM-VP8 yet) can handle alpha, but I do not have their codec at hand.

This also works. - ffmpeg -i yoursequence%d.png -vcodec png movie_with_alpha.mov

查看更多
beautiful°
6楼-- · 2019-01-31 08:08

Yes ffmpeg certainly does support alpha channel in a video file. Not all codecs in ffmpeg seem to support alpha yet tho. Motion PNG in a .MOV file is one good combination for alpha.

To encode/import images with alpha to a video with alpha try: ffmpeg -i %d.png -vcodec png z.mov

Quicktime will play that.

To decode/export a video with alpha to images with alpha try: ffmpeg -i z.mov -f image2 export2\%d.png

Note that I exported them to a directory called 'export2'. Be sure to leave the %d parts in there. These commands will work as is on a Windows system. Linux/Mac users may need to add quote marks and swap some \ for / as usual.

查看更多
登录 后发表回答