Skip to content

Setting the rotation property of a video stream #1012

Closed

Description

@hmaarrfk

h264 (and I think h265) support setting the orientation property of a video stream without invoking any filters at encoding time. This can help in situations where encoding time is critical.

one can read the metadata, I think it is populated through:

self.metadata = avdict_to_dict(

There doesn't seem like there is any way to set this property.

Overview

{{ A clear and concise description of what the feature is. }}

Existing FFmpeg API

I'm sorry, i'm not an expert at navigating this API. However, I can show you that you can READ it already.

https://ffmpeg.org/ffmpeg-all.html#h264_005fmetadata

Expected PyAV API

Adding something like: to the Stream
Example:

    property rotate:
        pass
    property flip:
        pass

Investigation

# Create a standard video file
size=256x128
codec=libopenh264

ffmpeg -hide_banner -y -v error \
    -f lavfi \
    -i testsrc=duration=2:size=${size}:rate=10 \
    -codec ${codec}\
    -metadata title="testsrc input" \
    test_src.mp4

# Rotate it using metadata 270 degrees
ffmpeg -hide_banner -y -v error \
    -i test_src.mp4 \
    -codec copy \
    -metadata:s:v:0 rotate=270 \
    -metadata title="copied from mp4 tried to rotate 270" \
    test_src_copied_270.mp4

# Rotate it using metadata 90 degrees
ffmpeg -hide_banner -y -v error \
    -i test_src.mp4 \
    -codec copy \
    -metadata:s:v:0 rotate=90 \
    -metadata title="copied from mp4 tried to rotate 270" \
    test_src_copied_90.mp4

In Python

import av
container_90 = av.open('test_src_copied_90.mp4', 'r')
container_270 = av.open('test_src_copied_270.mp4', 'r')
print(container_90.streams[0].metadata)
print(container_270.streams[0].metadata)

^^^ Only god know why the numbers are flipped above.

Versions

  • OS: Ubuntu 22.04
  • PyAV runtime:
PyAV v9.2.0
library configuration: --prefix=/home/mark/mambaforge/envs/mcam_dev --cc=/home/conda/feedstock_root/build_artifacts/ffmpeg_1654972201155/_build_env/bin/x86_64-conda-linux-gnu-cc --disable-doc --disable-openssl --enable-avresample --enable-demuxer=dash --enable-gnutls --disable-gpl --enable-hardcoded-tables --enable-libfreetype --enable-libopenh264 --enable-vaapi --enable-libaom --enable-libsvtav1 --enable-libxml2 --enable-libvpx --enable-pic --enable-pthreads --enable-shared --disable-static --enable-version3 --enable-zlib --enable-libmp3lame --pkg-config=/home/conda/feedstock_root/build_artifacts/ffmpeg_1654972201155/_build_env/bin/pkg-config
library license: LGPL version 3 or later
libavcodec     58.134.100
libavdevice    58. 13.100
libavfilter     7.110.100
libavformat    58. 76.100
libavutil      56. 70.100
libswresample   3.  9.100
libswscale      5.  9.100
  • PyAV build: conda-forge av
  • FFmpeg: from conda-forge
ffmpeg version 4.4.2 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 10.3.0 (conda-forge gcc 10.3.0-16)
configuration: --prefix=/home/conda/feedstock_root/build_artifacts/ffmpeg_1654972201155/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac --cc=/home/conda/feedstock_root/build_artifacts/ffmpeg_1654972201155/_build_env/bin/x86_64-conda-linux-gnu-cc --disable-doc --disable-openssl --enable-avresample --enable-demuxer=dash --enable-gnutls --disable-gpl --enable-hardcoded-tables --enable-libfreetype --enable-libopenh264 --enable-vaapi --enable-libaom --enable-libsvtav1 --enable-libxml2 --enable-libvpx --enable-pic --enable-pthreads --enable-shared --disable-static --enable-version3 --enable-zlib --enable-libmp3lame --pkg-config=/home/conda/feedstock_root/build_artifacts/ffmpeg_1654972201155/_build_env/bin/pkg-config
libavutil      56. 70.100 / 56. 70.100
libavcodec     58.134.100 / 58.134.100
libavformat    58. 76.100 / 58. 76.100
libavdevice    58. 13.100 / 58. 13.100
libavfilter     7.110.100 /  7.110.100
libavresample   4.  0.  0 /  4.  0.  0
libswscale      5.  9.100 /  5.  9.100
libswresample   3.  9.100 /  3.  9.100

Activity

  1. hmaarrfk commented on Jul 20, 2022

    @hmaarrfk
    ContributorAuthor

    I also had some code to attempt to write to the metadata, but I think it is written in imageio's wrapper, and I feel bad providing it.

  2. hmaarrfk commented on Jul 20, 2022

    @hmaarrfk
    ContributorAuthor

    I think I have to set the encoding before the stream is created. Sorry for the noise.

  3. hmaarrfk commented on Jul 20, 2022

    @hmaarrfk
    ContributorAuthor

    It should be that horizontal and vertical flips are also supported, but honestly, i can't get the bsf to work. So i can't really inspect to see if pyav / ffmpeg decode the flips correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions