FFMPEG - Extract color-accurate image sequence from UHD MOV in bt.2020 - image-processing

Im trying to use FFMPEG to extract an image sequence from an MOV that comes from an iphone 13 with bt2020
Metadata:
major_brand : qt
minor_version : 0
compatible_brands: qt
creation_time : 2021-10-11T16:12:07.000000Z
com.apple.quicktime.make: Apple
com.apple.quicktime.model: iPhone 13 Pro
com.apple.quicktime.software: 15.0.1
com.apple.quicktime.creationdate: 2021-10-11T18:12:07+0200
Duration: 00:00:07.13, start: 0.000000, bitrate: 8693 kb/s
Stream #0:0(und): Video: hevc (Main 10) (hvc1 / 0x31637668), yuv420p10le(tv, bt2020nc/bt2020/arib-std-b67), 1920x1080, 8472 kb/s, 29.99 fps, 30 tbr, 600 tbn, 600 tbc (default)
But i haven't been able to get a color match on any extracted images. I have tried following a number of posts, a few on jpgs (most said it would have to be a PNG) and a few on PNG. But no matter what I seem to try, i end up with a lifted, less saturated image. I can only use jpg or PNG in this case - am I just stuck with a non-accurate color image extraction on HDR, or is there something I am missing here?
I have tried playing with scale and colormatrix commands, trying different options to go from bt2020:bt709 (though im not sure this is the correct approach, or if I mis-formatted something). I also tried a suggestion on another post to get 10bit PNG out - but ended up with the exact same color issue as just extracting a standard jpeg:
ffmpeg -i img_2106.mov -vsync 0 -f image2 test.%04d.png
and many other variations of options.
Anyone have any suggestions?
as added info im using:
ffmpeg version 4.4 Copyright (c) 2000-2021 the FFmpeg developers
built with Apple clang version 12.0.5 (clang-1205.0.22.9)

Related

ffmpeg psnr after cropping

I am trying to calculate PSNR of two images that have slightly different width/height:
ffmpeg -y -i /tmp/ff-021407597-sn-1029-516-C-0-956542137
-i /tmp/ff-021407597-ff-1029-516-C-0-362227908
-filter_complex "[0:v]crop=1028:514;[1:v]crop=1028:514"
-filter_complex "psnr" %03d.png
This does not work and I get this error:
Input #0, png_pipe, from
'/tmp/ff-021407597-sn-1029-516-C-0-956542137': Duration: N/A,
bitrate: N/A
Stream #0:0: Video: png, rgb24(pc), 1029x515, 25 tbr, 25 tbn, 25 tbc Input #1, png_pipe, from
'/tmp/ff-021407597-ff-1029-516-C-0-362227908': Duration: N/A,
bitrate: N/A
Stream #1:0: Video: png, rgb24(pc), 1029x516, 25 tbr, 25 tbn, 25 tbc Cannot find a matching stream for unlabeled input pad 0 on filter
Parsed_psnr_0
Is there some way to map the outputs of the crop filter so that it is passed into the psnr filter?
Use
ffmpeg -y -i /tmp/ff-021407597-sn-1029-516-C-0-956542137
-i /tmp/ff-021407597-ff-1029-516-C-0-362227908
-filter_complex "[0:v]crop=1028:514[a];[1:v]crop=1028:514[b];[a][b]psnr" -f null -

Adding mp4 mime-type prevents streaming of video with Unicorn and Rails 4

I'm seeing some strange behaviour where an .mp4 video file (located in /assets/images/) streams perfectly well without anything registered in mime_types.rb. But, if I register the video/mp4 mime-type, the video will not stream.
mime_types.rb:
Mime::Type.register "video/mp4", :mp4
Safari displays the first picture of the video, but cannot stream the video. This is only an issue with Safari and only with Unicorn (not Webrick).
We need the mime-type registered, because we're responding to mp4 requests in other locations of the app.
Any ideas how to solve this?
Rails 4.1.3
Ruby 2.1.2
Unicorn 4.8.3
Video details from ffmpeg -I
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'background_video.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 2015-03-06 14:03:09
encoder : Lavf54.6.100
Duration: 00:00:11.29, start: 0.000000, bitrate: 893 kb/s
Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1114x494, 903 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc (default)
Metadata:
creation_time : 2015-03-06 14:03:09
handler_name : VideoHandler
Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 2 kb/s (default)
Metadata:
creation_time : 2015-03-06 14:03:09
handler_name : SoundHandler
Moving the videos files to the /public/ folder fixes the issue.

JavaCV - strange black screen when playing a file with FFmpegFrameGrabber

I am using JavaCV and it's FFmpegFrameGrabber in my project. It loads and player files OK, when I launch the project from Eclipse, but shows either a black screen or a still 1st frame when I run a compiled project. Sometimes comming with this error:
Input #0, avi, from 'C:/path/Start_Cut.avi':
Duration: 00:00:20.03, start: 0.000000, bitrate: 26002 kb/s
Stream #0.0: Video: mpeg4, yuv420p, 1920x1080, 30 tbr, 30 tbn, 30 tbc
Cannot allocate memory. com.googlecode.javacv.FrameGrabber$Exception: Cannot initialize the conversion c ontext.
at com.googlecode.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.jav a:280)
and sometimes with
[mpeg4 # 6A95DF20] Invalid and inefficient vfw-avi packed B frames detected
Input #0, avi, from 'C:/path/Start_Cut.avi':
Duration: 00:00:20.03, start: 0.000000, bitrate: 26002 kb/s
Stream #0.0: Video: mpeg4, yuv420p, 1920x1080 [PAR 1:1 DAR 16:9], 25 tbr, 30
tbn, 25 tbc
does anyone has a clue on this?
UPDATE:
I have been able to narrow down the problem. For some reason, FFmpegFrameGrabber returns null upn calling the grab() method. Why does it work in Eclipse and does not work in standalone build? All libraries are included and I suppose, it would come up with an error if they did not.
Upgrading to the 2.4.0 version of OpenCV and to the current version of JavaCV did the trick. If someone gets in the same situation (which is unlikely), just perform a brand new installation following the instruction on the JavaCV website.

iPad Doesn't Render H.264 Video with HTML5

I have some H.264-encoded videos which render in HTML5 correctly in the web browser, but do not render correctly on the iPad. When I use a H.264 video I downloaded off the internet, my video renders correctly on the iPad, so it is not an HTML problem.
Here is the ffmpeg info about my videos --
My original .mov video:
Seems stream 1 codec frame rate differs from container frame rate: 6000.00 (6000/1) -> 30.00 (30/1)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'a_video.mp4':
Metadata:
major_brand : qt
minor_version : 537199360
compatible_brands: qt
Duration: 00:00:42.74, start: 0.000000, bitrate: 220 kb/s
Stream #0.0(eng): Audio: aac, 44100 Hz, stereo, s16, 94 kb/s
Stream #0.1(eng): Video: h264, yuv420p, 762x464, 122 kb/s, 30 fps, 30 tbr, 3k tbn, 6k tbc
After using Handbrake to convert my .mov to a mp4, yet doesn't render on the iPad:
Seems stream 0 codec frame rate differs from container frame rate: 180000.00 (180000/1) -> 29.97 (30000/1001)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'a_video.m4v':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42isomavc1
encoder: HandBrake 0.9.5 2011010300
Duration: 00:00:42.77, start: 0.000000, bitrate: 169 kb/s
Stream #0.0(und): Video: h264, yuv420p, 752x464 [PAR 381:376 DAR 381:232], 35 kb/s, PAR 145161:141376 DAR 145161:87232, 29.97 fps, 29.97 tbr, 90k tbn, 180k tbc
Stream #0.1(eng): Audio: aac, 44100 Hz, stereo, s16, 128 kb/s
Here is a .mp4 I found online which does render on the iPad:
Seems stream 1 codec frame rate differs from container frame rate: 180000.00 (180000/1) -> 25.00 (25/1)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'a_video_3_emu.mp4':
Metadata:
major_brand : M4VP
minor_version : 1
compatible_brands: M4VPM4A mp42isom
encoder : CoreMediaAuthoring 677, CoreMedia 420.17, i386
Duration: 00:01:38.01, start: 0.000000, bitrate: 1023 kb/s
Stream #0.0(und): Audio: aac, 32000 Hz, mono, s16, 97 kb/s
Stream #0.1(und): Video: h264, yuv420p, 480x360 [PAR 1:1 DAR 4:3], 914 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc
Does anyone see something wrong with the way I am encoded my videos?
Edit
At first my theory was that the iPad was sensitive to different container formats; but that appears not to be the case. I took a video which does render correctly on the iPad and converted it to a .mov, and it still played correctly on the iPad. So there must be a problem with how the iPad deals with the underlying H.264 stream.
If you have a H.264 video stream -- regardless of the container (mov, m4v, mp4) -- and your HTML5 video renders in a web browser but doesn't render on the iPad, there are two possible fixes:
The first solution is to convert the H.264 video stream to mpeg4.
ffmpeg -i video_h264_not_working.mov -acodec copy -vcodec mpeg4 video_mpeg.mov
(Alternatively, you can select MPEG4 instead of H.264 in Handbrake.)
The second solution is to re-process the H.264 video with the following parameters:
ffmpeg -i video_h264_not_working.mov -vcodec libx264 -r 25 -b 516k -bt 516k -crf 22 -vpre normal video_h264.mov
The second solution came from: http://houseoflaudanum.com/navigate/howtos/html5-video-no-webm/
I'm guessing in the former case, the mpeg4 codec is more relaxed on the iPad; and in the latter case, the iPad didn't like some of the stream parameters from the original H.264 encoding, so a "cleaning" was necessary.
To determine what your video stream actually is, just do ffmpeg -i myvideo.mov.
I've had great success encoding and transcoding HTML5 video using the free Miro Video Converter (OS X, Windows) as recommended in the Video on the Web section of Dive Into HTML5 by Mark Pilgrim.
Miro Video Converter can convert virtually any video file to MP4, Theora, or MP3 (the audio only). It has presets that will convert video to the correct sizes and formats for popular phones, iPods, and other media players. Just convert your video and copy it to your device.
It's a super simple way to convert almost any video to MP4, WebM (vp8), Ogg Theora, or for Android, iPhone, and more. You'll feel good that it's 100% Free and open-source, too.
An another veritable video conversion utility is Video Monkey, a free video encoding application exclusively for Mac. It was created after the demise of the great tool Visual Hub. Video Monkey borrows heavily from the Visual Hub video conversion tool, both conceptually and from the original code dump posted to SourceForge as TranscoderRedux.
sudo port install ffmpeg and try this secret sauce:
#!/bin/bash
BR=512k
WIDTH=640
HEIGHT=272
input=${1}
# strip off the file extension
output=$(echo ${input} | sed 's/\..*//' )
# works for most videos
ffmpeg -y -i ${input} -f mpegts -acodec libmp3lame -ar 48000 -ab 64k -s ${WIDTH}x${HEIGHT} -vcodec libx264 -b ${BR} -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 7 -trellis 0 -refs 0 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate ${BR} -bufsize ${BR} -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 30 -qmax 51 -qdiff 4 -level 30 -aspect ${WIDTH}:${HEIGHT} -g 30 -async 2 ${output}-iphone.ts
Adjust width and height to the original video and set the bitrate as you see fit. Pass the original video as parameter to the script. Works with almost any source format. You need macports for the port command.

iPad MP4 playback issues

I'm wondering weather this is an issue with iOS devices or just the iPad (I've actually only had an iPad to play on).
If I were to write a HTML link that linked to an MP4 on a server (over HTTP), the iPad launches it in Safari but doesn't recognise that it's an MP4 and just serves a blank page.
I've had a look on other devices, such as Android for eg, and this fires up the media player and streams correctly.
Is there a specific way to get it to play in the default media player on iPad/iOS?
The problem is more than likely the particular encoding characteristics of the MP4. h264 and MPEG-4 have loads of little options and tweakable parameters. iOS devices are fairly specific about what they do and do not like. (To be fair, this is not a problem specific to iOS devices.)
Have a look at Brightcove's encoding recommendations and re-encode your media.
That failing, and assuming you have FFmpeg installed, post the results of ffmpeg -i problemfile.mp4 and comment below so I can have a look.
I'm experiencing a very similar problem with the iPad in playing an mp4 file, which in fact was encoded by Brightcove.
The video won't start inside an html5 video tag using the iPad, while it works perfectly in Chrome, Safari and IE9.
You can grab the video here: http://tinyurl.com/796c5ub and this is the output of $ffmpeg -i video1.mp4:
ffmpeg version 0.7.1, Copyright (c) 2000-2011 the FFmpeg developers
built on Jul 31 2011 18:31:40 with clang 2.1 (tags/Apple/clang-163.7.1)
configuration: --prefix=/opt/local --enable-gpl --enable-postproc --enable-swscale --enable-avfilter --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libdirac --enable-libschroedinger --enable-libfaac --enable-libxvid --enable-libx264 --enable-libvpx --enable-libspeex --enable-nonfree --mandir=/opt/local/share/man --enable-shared --enable-pthreads --disable-indevs --cc=/Developer/usr/bin/clang --arch=x86_64
libavutil 50. 43. 0 / 50. 43. 0
libavcodec 52.122. 0 / 52.122. 0
libavformat 52.110. 0 / 52.110. 0
libavdevice 52. 5. 0 / 52. 5. 0
libavfilter 1. 80. 0 / 1. 80. 0
libswscale 0. 14. 1 / 0. 14. 1
libpostproc 51. 2. 0 / 51. 2. 0
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video1.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 2011-12-06 08:00:00
encoder : Lavf53.17.0
Duration: 00:00:45.60, start: 0.000000, bitrate: 1818 kb/s
Stream #0.0(eng): Video: h264 (High), yuv420p, 1280x852, 1687 kb/s, 25 fps, 25 tbr, 25 tbn, 50 tbc
Metadata:
creation_time : 2011-12-06 08:00:00
Stream #0.1(eng): Audio: aac, 44100 Hz, stereo, s16, 128 kb/s
Metadata:
creation_time : 2011-12-06 08:00:00
At least one output file must be specified
Found this while looking for a solution for iOS-related MP4-audio problems: Some of my MP4 audio files would start to play, but at a certain point in the file, playback on iOS would hang, while non-iOS players played the same MP4 files flawlessly till the end.
MP4 is a container format. Said to also contain non-audio/non-video streams and metadata headers that describe audio/video. iOS seems to dislike some stream types and if some headers are at unexpected positions in the file.
mp4box and ffmpeg can fix these headers and remove unwanted streams.
I only fiddled with mp4box, that did it for me to fix the audio problems:
mp4box -single 1 input.mp4 -out outputfixed.mp4
The "1" is the stream number containing the audio in my case (may vary, mp4box can also list the contained streams).
Later on I found that the GUI tool "MP4tag", that is primarily meant to edit tags, has a menu function called "MP4 optimize" that is said to be able to fix the same problem.
mp4box should also be able to fix vid+audio combinations, by retrieving more than one stream from the input file.

Resources