X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 02 Feb 2018 12:24 PM by  Kenneth Bowman
Creating lossless video output
 2 Replies
Sort:
You are not authorized to post a reply.
Author Messages

Kenneth Bowman



New Member


Posts:5
New Member


--
30 Jan 2018 03:25 PM
    I am trying to create a lossless MPEG-4 video from a sequence of PNG files. The basic code looks like this

    ni = 1540
    nj = 300
    fps = 6
    infile = FILE_SEARCH(indir, '*.png', COUNT = nframes)
    video = IDLffVideoWrite('test.mp4')
    stream = video.AddVideoStream(ni, nj, fps, PRESET = 'normal')
    FOR s = 0, nframes-1 DO BEGIN
    image = READ_PNG(infile[s])
    time = video.Put(stream, image)
    ENDFOR
    video = 0

    If I understand the documentation for IDLffVideoWrite correctly, PRESET = 'normal' should give 'lossless_fast' video output. When I look at the resulting video, however, it is clearly fuzzier than the original PNG files and shows the fringing artifacts that are typical of JPEG or MPEG compression. Different values of PRESET all give files that are the same size and appearance. I am viewing the files on a Mac with QuickTime Player.

    So how do I create lossless video?

    Thanks, Ken

    David Starbuck



    Basic Member


    Posts:143
    Basic Member


    --
    01 Feb 2018 03:22 PM
    The PRESET keyword is only available if you are using H.264 codec which is not supported by the default ffmpeg library that ships with IDL. In order to get access to this keyword, you will need to replace the ffmpeg library inside the IDL installation with a version of this library that supports the H.264 codec. Have you replaced this library? If not, general instructions on how to do this can be found on the following Help page:
    http://www.harrisgeospati...s/creatingvideo.html

    Another option might be to use the MJPEG2000 file format. This is a lossless format but most likely won't work with most video players. More information this file format and how it can be used in IDL (IDLffMJPEG2000) can be found using the link below:
    https://www.harrisgeospatial.com/docs/IDLffMJPEG2000.html

    David
    -HGS

    Kenneth Bowman



    New Member


    Posts:5
    New Member


    --
    02 Feb 2018 12:24 PM
    Thanks, David.

    I installed the current release of ffmpeg on my Mac using Homebrew, and after some struggle I managed to get IDL to find the video libraries by creating the necessary symbolic links:

    
    lrwxr-xr-x  1 root  wheel    31B Feb  2 08:43 libavcodec.55.dylib@ -> /usr/local/lib/libavcodec.dylib
    -rwxrwxr-x  1 root  wheel   4.2M Jul  8  2015 libavcodec.55.dylib.original*
    lrwxr-xr-x  1 root  wheel    19B Jan 26  2017 libavcodec.dylib@ -> libavcodec.55.dylib
    lrwxr-xr-x  1 root  wheel    32B Feb  2 08:43 libavformat.55.dylib@ -> /usr/local/lib/libavformat.dylib
    -rwxrwxr-x  1 root  wheel   646K Jul  8  2015 libavformat.55.dylib.original*
    lrwxr-xr-x  1 root  wheel    20B Jan 26  2017 libavformat.dylib@ -> libavformat.55.dylib
    lrwxr-xr-x  1 root  wheel    30B Feb  2 08:44 libavutil.52.dylib@ -> /usr/local/lib/libavutil.dylib
    -rwxrwxr-x  1 root  wheel   258K Jul  8  2015 libavutil.52.dylib.original*
    lrwxr-xr-x  1 root  wheel    18B Jan 26  2017 libavutil.dylib@ -> libavutil.52.dylib
    lrwxr-xr-x  1 root  wheel    31B Feb  2 08:44 libswscale.2.dylib@ -> /usr/local/lib/libswscale.dylib
    -rwxrwxr-x  1 root  wheel   480K Jul  8  2015 libswscale.2.dylib.original*
    lrwxr-xr-x  1 root  wheel    18B Jan 26  2017 libswscale.dylib@ -> libswscale.2.dylib
    


    I can see that IDL is using the newer versions of the ffmpeg libraries, but IDLffVideoWrite crashes when I try to create an output file.

    
    	IDL> video  = IDLffVideoWrite('test.mp4')         
    	% Loaded DLM: VIDEO.
    	IDL> result = video.GetCodecs(/LONG_NAMES, /VIDEO)
    	IDL> print, result                                
    	Multicolor charset for Commodore 64 Multicolor charset for Commodore 64, extended with 5th color (colram) Alias/Wavefront PIX image AMV Video
    	APNG (Animated Portable Network Graphics) image ASUS V1 ASUS V2 Avid 1:1 10-bit RGB Packer Avid Meridien Uncompressed
    	Uncompressed packed MS 4:4:4:4 BMP (Windows and OS/2 bitmap) Cinepak Cirrus Logic AccuPak VC3/DNxHD DPX (Digital Picture Exchange) image
    	DV (Digital Video) FFmpeg video codec #1 Huffyuv FFmpeg variant Flexible Image Transport System Flash Screen Video Flash Screen Video Version 2
    	FLV / Sorenson Spark / Sorenson H.263 (Flash Video) GIF (Graphics Interchange Format) H.261 H.263 / H.263-1996
    	H.263+ / H.263-1998 / H.263 version 2 Huffyuv / HuffYUV JPEG 2000 JPEG-LS Lossless JPEG MJPEG (Motion JPEG) MPEG-1 video MPEG-2 video
    	MPEG-4 part 2 MPEG-4 part 2 Microsoft variant version 2 MPEG-4 part 2 Microsoft variant version 3 Microsoft Video-1 PAM (Portable AnyMap) image
    	PBM (Portable BitMap) image PC Paintbrush PCX image PGM (Portable GrayMap) image PGMYUV (Portable GrayMap YUV) image
    	PNG (Portable Network Graphics) image PPM (Portable PixelMap) image Apple ProRes Apple ProRes Apple ProRes (iCodec Pro)
    	QuickTime Animation (RLE) video AJA Kona 10-bit RGB Codec Uncompressed RGB 10-bit raw video id RoQ video RealVideo 1.0 RealVideo 2.0 SGI image
    	Snow Sun Rasterfile image Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1 Truevision Targa image TIFF image Ut Video
    	Uncompressed 4:2:2 10-bit Uncompressed packed 4:4:4 Uncompressed packed QT 4:4:4:4 Uncompressed 4:4:4 10-bit SMPTE VC-2
    	AVFrame to AVPacket passthrough Windows Media Video 7 Windows Media Video 8 XBM (X BitMap) image X-face image XWD (X Window Dump) image
    	Uncompressed YUV 4:1:1 12-bit Uncompressed packed 4:2:0 LCL (LossLess Codec Library) ZLIB Zip Motion Blocks Video
    	libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB libxvidcore MPEG-4 part 2
    	VideoToolbox H.264 Encoder
    	IDL> .r mp4_animate
    	% Compiled module: MP4_ANIMATE.
    	IDL> MP4_ANIMATE
    	% Loaded DLM: PNG.
    			  1540         300       6
    	dyld: lazy symbol binding failed: Symbol not found: _avcodec_alloc_frame
    	  Referenced from: /Applications/exelis/idl85/bin/bin.darwin.x86_64/idl_video.so
    	  Expected in: flat namespace
    
    	dyld: Symbol not found: _avcodec_alloc_frame
    	  Referenced from: /Applications/exelis/idl85/bin/bin.darwin.x86_64/idl_video.so
    	  Expected in: flat namespace
    
    	Abort trap: 6
    	IDL> 
    


    I switched the video libraries back to versions that come with IDL.


    It is possible to use ffmpeg directly to make an .mp4 file, but a lossless-compression-file (-crf 0) will not play in QuickTime. The best I could do is -crf 1.

    ffmpeg -r 6 -s 1540x300 -i m_frame_%04d.png -vcodec libx264 -crf 1 -pix_fmt yuv420p test.mp4

    It is OK, but not great.

    So far, the best quality that I have been able to achieve is to open the PNG files in QuickTime Player 7 Pro. Save it as a .mov file. Because it uses the PNG codec, that version is actually lossless, but most people don't have QT Player 7. If you open it in QuickTime Player 10, however, it converts the file using the Apple ProRes 4444 codec, which actually makes it *larger* and slightly fuzzy. Oh well, it looks like that is the best I can do.

    It is too bad that there isn't a lossless video format (like PNG) that is simple and widely supported. But scientific animation is an infinitesimal market compared to movies.

    Cheers, Ken
    You are not authorized to post a reply.