Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lossless crop, flip, rotate, aspect ratio in video bitstream or container format metadata #643

Open
mifi opened this issue Feb 5, 2021 · 25 comments

Comments

@mifi
Copy link
Owner

mifi commented Feb 5, 2021

Format metadata

This is metadata that affects presentation of the video, and is stored in the container format (not in the individual streams).

Display aspect ratio (-aspect)

From ffmpeg.org:

-aspect: If used together with -vcodec copy, it will affect the aspect ratio stored at container level, but not the aspect ratio stored in encoded frames, if it exists.

Supported in VLC and MacOS QuickLook/QuickTime (tested by me). See also #493

Matroska specific metadata

Crop

PixelCropBottom=40
PixelCropTop=0
PixelCropLeft=240
PixelCropRight=40

#504 (comment)

Actually, this is possible with a Matroska container. I've done it multiple times in the past with MKVToolNix GUI, and it plays perfectly fine in MPC-BE. For not supporting players like VLC, bug reports should be filed.

The only caveat is that the video data stays the same and the file size is not reduced. But for playing purposes, it's very useful.

In MKVToolNix GUI, import a video file, click the video track, and hover over the text box of "Video properties > Cropping" for help. There's no visual cropping tool, though. That's why Lossless Cut should have it.

I wish this would be implemented. You'd just have to communicate that the feature is limited to MKV output, and that it's just the cropping information that is saved, so the file size isn't reduced and the player is responsible for the actual cropping.

If FFMPEG is the problem (can't it set metadata in the output file?), maybe it must be adapted or a multi-tool solution with the command line equivalent of the MKVToolNix GUI header editor might be the way to go.

Aspect ratio / SAR / DAR

PixelWidth=320
PixelHeight=240
DisplayWidth=320
DisplayHeight=240

Status: Matroska crop and aspect metadata seems to be not supported by FFmpeg yet.

https://trac.ffmpeg.org/ticket/4489
https://trac.ffmpeg.org/ticket/5301
https://forum.videohelp.com/threads/346278-MKVToolnix-cropping-or-changing-display-width-height
https://lists.ffmpeg.org/pipermail/ffmpeg-devel/2016-October/201451.html

MP4 specific metadata

CLAP atom / aperture width / height

Status: Seems to be supported in FFmpeg some time after 11 july 2024, however I can't find any sample file that has this, and I don't know how to use it.

https://trac.ffmpeg.org/ticket/7437
https://trac.ffmpeg.org/ticket/1485

Codec video stream metadata (-bsf)

This is metadata stored in the individual video streams themselves. aka. bitstream filters.

Crop (crop_left, crop_right, crop_top, crop_bottom)

Supported by H264 and H265.

#38 (comment)

Supported in windows media player

Rotate (rotate)

See also display_orientation.

Flip (flip)

See also display_orientation.

Sample aspect ratio (sample_aspect_ratio)

Supported by H264 and H265. #493

Display aspect ratio

  • MPEG-2: display_aspect_ratio
  • HEVC width/height ("Set width and height after crop" - not sure if the same)
@h-h-h-h
Copy link

h-h-h-h commented Feb 5, 2021

Note that, in MKVToolNix GUI, additionally to "Cropping", "Display width/height" must be set to the new video size for distortion-free cropping (should also be possible via "Set aspect ratio").

(This brings us to the idea that Matroska medadata would also make possible the following: aspect ratio correction [spacial stretching/compressing]; adjusting audio delay; temporal stretching/compressing by setting stretch factors and/or FPS per track [even for synced combination of, say, 24-fps video track with English audio track of file 1 and sped-up German audio track of 25-fps video file 2].)

@shodanx2
Copy link

Hello,
I would like to signal interest in this feature.
I was searching for a "lossless crop" app as efficient as losslesscut but that doesn't appear to exist yet !
So, an arbitrary crop is not possible without a transcode because of the partial blocks where you're cropping.
For that reason, I think a limited crop that can only crop whole quantization blocks, could that work ?

You'd lose some accuracy on the crop dimensions depending on the size of the blocks, but I think that would still be very usable way to crop video and the benefits of not having to transcode are massive

@Craftyawesome
Copy link

For an alternative, some codecs like H264 and HEVC also have crop metadata. E.g. for H264 ffmpeg -i inputfile.mp4 -bsf:v h264_metadata=crop_left=128:crop_right=128:crop_top:0:crop_bottom=0 -codec copy outputfile.mp4
It actually seems to work in a lot of players, although VLC gets it wrong

@shodanx2
Copy link

shodanx2 commented Sep 3, 2023

Wow that would be great ! I just needed to re-frame a scene and didn't want to recompress, losing quality, this would be perfect, to draw a crop rectangle on the screen and move it around at various key frames !

@mifi
Copy link
Owner Author

mifi commented Sep 4, 2023

nice find! i wonder if it works on the most popular video players and tools like

  • chrome's built-in html5 video player
  • safari's & ios safari's built in html5 video player
  • firefox html5 video
  • iphone photos app
  • android built in video player
  • android photos
  • apple quicktime
  • apple quicklook
  • windows media player
  • ffmpeg (e.g. read a losslessly cropped video and re-encode it, observing that the encoded video is cropped correctly)

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

Hello,

Please note where was a typo in @Craftyawesome 's command

ffmpeg -i inputfile.mp4 -bsf:v h264_metadata=crop_left=128:crop_right=128:crop_top:0:crop_bottom=0 -codec copy outputfile.mp4

should be

ffmpeg -i inputfile.mp4 -bsf:v h264_metadata=crop_left=128:crop_right=128:crop_top=0:crop_bottom=0 -codec copy outputfile.mp4

FFmpeg output


ffmpeg -hide_banner -i testvideo.mp4 -bsf:v h264_metadata=crop_left=128:crop_right=128:crop_top=0:crop_bottom=0 -codec copy testvideo.metacropped.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'testvideo.mp4':
  Metadata:
    major_brand     : dash
    minor_version   : 0
    compatible_brands: iso6avc1mp41
    creation_time   : 2023-02-14T13:25:33.000000Z
  Duration: 00:01:09.74, start: 0.000000, bitrate: 1985 kb/s
  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 7 kb/s, 29.97 fps, 29.97 tbr, 30k tbn (default)
    Metadata:
      creation_time   : 2023-02-14T13:25:33.000000Z
      handler_name    : ISO Media file produced by Google Inc.
      vendor_id       : [0][0][0][0]
Output #0, mp4, to 'testvideo.metacropped.mp4':
  Metadata:
    major_brand     : dash
    minor_version   : 0
    compatible_brands: iso6avc1mp41
    encoder         : Lavf60.11.100
  Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 7 kb/s, 29.97 fps, 29.97 tbr, 30k tbn (default)
    Metadata:
      creation_time   : 2023-02-14T13:25:33.000000Z
      handler_name    : ISO Media file produced by Google Inc.
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[out#0/mp4 @ 0000025d8cfe5200] video:16874kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.110405%
frame= 2090 fps=0.0 q=-1.0 Lsize=   16893kB time=00:01:09.66 bitrate=1986.3kbits/s speed=1.48e+03x

mediainfo
2023-09-04 22_38_49-MediaArea net_MediaInfo - D__shodan_Downloads_testvideo metacropped mp4

vlc
2023-09-04 22_40_26-testvideo metacropped mp4 - VLC media player

Hmm, I might need a more concrete test video, but it's doing something

@Craftyawesome
Copy link

Craftyawesome commented Sep 5, 2023

Please note where was a typo in @Craftyawesome 's command

My bad, copied it from one of the related issues in that vlc issue link without checking myself.

Hmm, I might need a more concrete test video, but it's doing something

As mentioned in the issue link vlc seems to interpret left and top as an additional right and bottom. Try almost any other player, it seems to be quite compatible barring vlc.

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

Ok,

I have found a more suitable test file

testvideo.mp4

I have run the following commands to crop it different ways for testing

ffmpeg -hide_banner -i testvideo.mp4 -bsf:v h264_metadata=crop_left=500:crop_right=0:crop_top=0:crop_bottom=0 -codec copy testvideo.metacropped.cropleft500.mp4
ffmpeg -hide_banner -i testvideo.mp4 -bsf:v h264_metadata=crop_left=0:crop_right=500:crop_top=0:crop_bottom=0 -codec copy testvideo.metacropped.cropright500.mp4
ffmpeg -hide_banner -i testvideo.mp4 -bsf:v h264_metadata=crop_left=0:crop_right=0:crop_top=500:crop_bottom=0 -codec copy testvideo.metacropped.croptop500.mp4
ffmpeg -hide_banner -i testvideo.mp4 -bsf:v h264_metadata=crop_left=0:crop_right=0:crop_top=0:crop_bottom=500 -codec copy testvideo.metacropped.cropbottom500.mp4
ffmpeg -hide_banner -i testvideo.mp4 -bsf:v h264_metadata=crop_left=250:crop_right=250:crop_top=0:crop_bottom=0 -codec copy testvideo.metacropped.cropmiddlehorizon.mp4
ffmpeg -hide_banner -i testvideo.mp4 -bsf:v h264_metadata=crop_left=0:crop_right=0:crop_top=250:crop_bottom=250 -codec copy testvideo.metacropped.cropmiddlevertical.mp4
ffmpeg -hide_banner -i testvideo.mp4 -bsf:v h264_metadata=crop_left=250:crop_right=250:crop_top=250:crop_bottom=250 -codec copy testvideo.metacropped.cropcenter.mp4

Here is the console output of that
ffmpeg-metacrop.log

And here are the test files

testvideo.metacropped.cropbottom500.mp4
testvideo.metacropped.cropcenter.mp4
testvideo.metacropped.cropmiddlehorizon.mp4
testvideo.metacropped.cropmiddlevertical.mp4
testvideo.metacropped.cropright500.mp4
testvideo.metacropped.croptop500.mp4
testvideo.metacropped.cropleft500.mp4

Here is mediainfo about these files.

2023-09-04 23_40_33-MediaArea net_MediaInfo - D__shodan_Downloads_testvideo metacropped cropbottom50
2023-09-04 23_41_28-MediaArea net_MediaInfo - D__shodan_Downloads_testvideo metacropped cropcenter m

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

Here is how VLC handles those files.

2023-09-04 23_44_45-Why I Am Bigger Than MR BEAST - And Why is YouTube Broken - YouTube — Mozilla Fi

2023-09-04 23_53_03-testvideo metacropped cropcenter mp4 - VLC media player

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

Here is how MPC-HC abandonned version MPC-HC.1.7.13.x64.exe handles it

2023-09-04 23_58_30-testvideo metacropped cropright500 mp4
2023-09-04 23_58_56-testvideo metacropped croptop500 mp4
2023-09-05 00_00_18-testvideo metacropped cropcenter mp4

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

Here is how MPC-HC.1.9.24.x64.exe handles it
2023-09-05 00_06_26-testvideo metacropped cropright500 mp4
2023-09-05 00_06_51-testvideo metacropped croptop500 mp4
2023-09-05 00_08_23-testvideo metacropped cropcenter mp4

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

Here is how MPC-HC.2.0.0.x64.exe handles it

2023-09-05 00_21_04-
2023-09-05 00_21_22-testvideo metacropped cropbottom500 mp4
2023-09-05 00_23_31-

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

Here is how mpv-x86_64-v3-20230903-git-67368ac.7z handles it

2023-09-05 00_36_49-testvideo metacropped cropcenter mp4 - mpv
2023-09-05 00_35_41-testvideo metacropped croptop500 mp4 - mpv
2023-09-05 00_35_04-testvideo metacropped cropright500 mp4 - mpv

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

How WACUP_Preview_v1_0_21_7236.exe handles it
Actually it is completely broken, it only shows the first frame then skips to the next time.
2023-09-05 00_42_48-Winamp Video
2023-09-05 00_42_37-Winamp Video
2023-09-05 00_42_26-MKV lossless cropping · Issue #643 · mifi_lossless-cut — Mozilla Firefox
2023-09-05 00_42_16-Winamp Video

Even the original file does not play either.
Still I managed to capture the crop factor

2023-09-05 00_42_02-Winamp Video
2023-09-05 00_43_05-Winamp Video
2023-09-05 00_42_58-Winamp Video

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

How smplayer-23.6.0-x64-unsigned.exe handles it
This is using the MPV engine
2023-09-05 00_55_21-testvideo metacropped cropbottom500 mp4 - SMPlayer

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

How smplayer-23.6.0-x64-unsigned.exe handles it
But using the mplayer engine
2023-09-05 01_01_04-Downloads

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

How apprenticevideo-git.replay.20230729.181616.745a82e1-win32-x64.msi handles it

2023-09-05 01_08_52-Apprentice Video_ testvideo metacropped croptop500 mp4

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

Here is how firefox 117.0 (64-bit) handles it

2023-09-05 01_12_58-testvideo metacropped cropbottom500 mp4 — Mozilla Firefox

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

How Microsoft Photos 2019.19071.12548.0 handles it

2023-09-05 01_15_52-Photos
2023-09-05 01_18_12-Photos
2023-09-05 01_17_53-Photos
2023-09-05 01_17_34-Photos
2023-09-05 01_16_57-Photos
2023-09-05 01_16_31-Photos
2023-09-05 01_16_17-Photos

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

How ffplay version 2023-09-04-git-f8503b4c33-full_build-www.gyan.dev handles it

2023-09-05 01_20_06-testvideo metacropped cropleft500 mp4
2023-09-05 01_22_03-testvideo metacropped cropcenter mp4
2023-09-05 01_21_42-testvideo metacropped cropmiddlevertical mp4
2023-09-05 01_21_23-testvideo metacropped cropmiddlehorizon mp4
2023-09-05 01_21_01-testvideo metacropped cropbottom500 mp4
2023-09-05 01_20_44-testvideo metacropped croptop500 mp4
2023-09-05 01_20_22-testvideo metacropped cropright500 mp4

@mifi
Copy link
Owner Author

mifi commented Sep 5, 2023

thanks for researching. maybe if we could make a summary/table of each player and how well it works for them, then we could look into whether it's worth implementing

@shodanx2
Copy link

shodanx2 commented Sep 5, 2023

Yes, there needs to be a more visual test pattern, make all outputs standard sized and in the same order, so at a glance the difference between players can be seen.

I'm also very curious to know, if I cut up the test pattern video into multiple chunks, set different crop factors for each chunk and then re-assemble them, Will the crop factor change on the fly.

Also all these tests need to be tried with all codecs that support metadata cropping (at least h.265 and AV1) to see whether this feature is getting improvements over new codecs.

Having lossless cropping would be awesome, when I started doing videos it was my second most used action and 3rd was masking.

@Jamim
Copy link

Jamim commented Oct 2, 2023

Note that, in MKVToolNix GUI, additionally to "Cropping", "Display width/height" must be set to the new video size for distortion-free cropping (should also be possible via "Set aspect ratio").

(This brings us to the idea that Matroska medadata would also make possible the following: aspect ratio correction [spacial stretching/compressing]; adjusting audio delay; temporal stretching/compressing by setting stretch factors and/or FPS per track [even for synced combination of, say, 24-fps video track with English audio track of file 1 and sped-up German audio track of 25-fps video file 2].)

I found that this method works perfectly for YouTube since it respects metadata when transcoding uploaded videos. In my case, I've cut out exactly 108 pixels from the left. By the way, it seems like it's impossible to achieve the same result using h264_metadata.

So @h-h-h-h, thanks a lot!

@mifi mifi mentioned this issue Dec 21, 2023
@mifi mifi changed the title MKV lossless cropping Lossless cropping and video bitstream metadata Dec 21, 2023
@mifi mifi changed the title Lossless cropping and video bitstream metadata Lossless cropping and video bitstream or container format metadata Dec 21, 2023
@mifi mifi changed the title Lossless cropping and video bitstream or container format metadata Lossless crop and video bitstream or container format metadata Dec 29, 2023
@mifi mifi mentioned this issue Dec 29, 2023
4 tasks
@mifi mifi mentioned this issue Oct 22, 2024
4 tasks
@mifi mifi changed the title Lossless crop and video bitstream or container format metadata Lossless crop, flip, rotate, aspect ratio in video bitstream or container format metadata Nov 28, 2024
@dr0biwan
Copy link

dr0biwan commented Dec 6, 2024

WACUP

Apologies bumping this but I assume there's stuff I still need to look into fixing for wacup? Just wanted to check which file(s) I should be testing against or if there's anything else I need to be thinking about, thanks.

@shodanx2
Copy link

Hello dr0biwan

If you scroll back enough, you will find the test files in this post.

This thread was an exploration in cropping with metadata, a lossless form of crop (but the data does stay behind, it doesn't get nulled out)

In this exploration you can see that support for this rather obscure feature is not uniform accross all players.

Reading my notes from back then. In the single test I had performed, WACUP failed to play the video instead getting stuck on the first frame.

Keep in mind this was more than a year ago, I imagine things have changed.

For this thread, once most players support this feature well, it would be interesting to see if we can change the crop metadata values over the timeline of the video.

Perhaps create something like a "dolly pan" "dolly zoom" effect and maybe other combination of effects

@mifi mifi marked this as a duplicate of #2294 Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants