moviepy icon indicating copy to clipboard operation
moviepy copied to clipboard

Video file clip width and height do not take rotation metadata into account

Open thenewguy opened this issue 4 years ago • 3 comments

clip.w and clip.h for VideoFileClip does not take rotation metadata into account

Sample output from ffprobe for a cell phone video taken in portrait mode:

        {
            "index": 0,
            "codec_name": "h264",
            "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
            "profile": "High",
            "codec_type": "video",
            "codec_time_base": "92917/5580000",
            "codec_tag_string": "avc1",
            "codec_tag": "0x31637661",
            "width": 1920,
            "height": 1080,
            "coded_width": 1920,
            "coded_height": 1088,
            "closed_captions": 0,
            "has_b_frames": 0,
            "sample_aspect_ratio": "1:1",
            "display_aspect_ratio": "16:9",
            "pix_fmt": "yuvj420p",
            "level": 41,
            "color_range": "pc",
            "color_space": "smpte170m",
            "color_transfer": "smpte170m",
            "color_primaries": "smpte170m",
            "chroma_location": "left",
            "refs": 1,
            "is_avc": "true",
            "nal_length_size": "4",
            "r_frame_rate": "30/1",
            "avg_frame_rate": "2790000/92917",
            "time_base": "1/90000",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 92917,
            "duration": "1.032411",
            "bit_rate": "23376114",
            "bits_per_raw_sample": "8",
            "nb_frames": "31",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "rotate": "90",
                "language": "eng",
                "handler_name": "VideoHandle"
            },
            "side_data_list": [
                {
                    "side_data_type": "Display Matrix",
                    "displaymatrix": "\n00000000:            0       65536           0\n00000001:       -65536           0           0\n00000002:            0           0  1073741824\n",
                    "rotation": -90
                }
            ]
        }

Notice the rotation metadata. I would expect Movepy to set the clip width to 1080 and the clip height to 1920 since it is rotated to portrait mode. However, the clip is set with a width of 1920 and a height of 1080

thenewguy avatar Sep 21 '21 19:09 thenewguy

        if abs(video_clip.rotation) in (90, 270):
            video_clip = video_clip.resize(answer_clip.size[::-1])
            video_clip.rotation = 0

chenbin0522 avatar Nov 09 '21 07:11 chenbin0522

        if abs(video_clip.rotation) in (90, 270):
            video_clip = video_clip.resize(answer_clip.size[::-1])
            video_clip.rotation = 0

Thx @chenbin0522, it works.

2h4dl avatar Feb 10 '22 06:02 2h4dl

Any tips on how you would accomplish this with write_videofile using ffmpeg_params so that the file written to disk is rotated without relying on rotation metadata without modifying the referenced clip?

edit - I arrived at the conclusion that it needed to be an input option so it has to happen before ffmpeg_params are applied so went your direction (@chenbin0522) with a clip copy but just curious if I could skip that step

thenewguy avatar Mar 01 '22 14:03 thenewguy

This was working for iPhone videos, but it got broken when fixing something else. Now the rotation metadata for portrait videos always shows "0", morphing this into #1871. And now there is no workaround

melyux avatar Jun 19 '23 21:06 melyux