Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • P PyAV
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 37
    • Issues 37
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 26
    • Merge requests 26
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • PyAV
  • PyAV
  • Merge requests
  • !287

Decoding raw packets without streams using codecs that needs extradata

  • Review changes

  • Download
  • Email patches
  • Plain diff
Closed Alireza Davoudi requested to merge github/fork/adavoudi/master into master 7 years ago
  • Overview 0
  • Commits 2
  • Pipelines 0
  • Changes 2

For decoding raw packets without streams, some codecs need a few extradata (like Huffman tables). In this commit, I provided two functions, one for extracting these data and the other for setting them when we want to decode without a stream.

An example usage:

import av

container = av.open('somevideo.mp4')

# Find index of the video stream 
video_stream_index = 0
for index, stream in enumerate(container.streams):
    if isinstance(stream, av.video.stream.VideoStream):
        video_stream_index = index
        break

buffered_packets = []
count = 0
now = False
for packet in container.demux(streams=video_stream_index):
    count += 1
    # We only buffer some packets (here from 100 to 500)
    if count > 100:
        new_packet = av.packet.Packet(input=packet.to_bytes())
        # First packet need to be a keyframe
        if packet.is_keyframe or now:
            buffered_packets.append(new_packet)
            now = True

    if count > 500:
        break

codec_name = container.streams[video_stream_index].codec_context.name
codec_origin = container.streams[video_stream_index].codec_context
codec_new = av.codec.CodecContext.create(codec_name, 'r')


extradata = codec_origin.extradata
if not extradata is None:
    codec_new.extradata = extradata

for packet in buffered_packets:
    try:
        for frame in codec_new.decode(packet):
            frame.to_image().save('frame-%04d.jpg' % frame.index)
    except av.AVError as e:
        print(e)
Checking approval status

Closed by (Jul 26, 2025 3:11pm UTC)

Loading

Activity


  • Alireza Davoudi mentioned in issue #155 (closed) 7 years ago

    mentioned in issue #155 (closed)

  • Mike Boers closed 7 years ago

    closed

  • Mike Boers mentioned in issue #296 (closed) 7 years ago

    mentioned in issue #296 (closed)

  • Administrator mentioned in issue #340 (closed) 7 years ago

    mentioned in issue #340 (closed)

  • Administrator mentioned in issue #722 (closed) 4 years ago

    mentioned in issue #722 (closed)

Please register or sign in to reply
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
3
3 participants
Mike Boers
Administrator
Alireza Davoudi
Reference: PyAV-Org/PyAV!287
Source branch: github/fork/adavoudi/master

Menu

Explore Projects Groups Snippets