Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def fix_doctests(suite):
for case in suite._tests:
# Add some more flags.
case._dt_optionflags = (
(case._dt_optionflags or 0) |
doctest.IGNORE_EXCEPTION_DETAIL |
doctest.ELLIPSIS |
doctest.NORMALIZE_WHITESPACE
)
case._dt_test.globs['av'] = av
case._dt_test.globs['video_path'] = av.datasets.curated('pexels/time-lapse-video-of-night-sky-857195.mp4')
for example in case._dt_test.examples:
# Remove b prefix from strings.
if is_py3 and example.want.startswith("b'"):
example.want = example.want[1:]
from PIL import Image
import numpy as np
import av
import av.datasets
container = av.open(av.datasets.curated('pexels/time-lapse-video-of-sunset-by-the-sea-854400.mp4'))
container.streams.video[0].thread_type = 'AUTO' # Go faster!
columns = []
for frame in container.decode(video=0):
print(frame)
array = frame.to_ndarray(format='rgb24')
# Collapse down to a column.
column = array.mean(axis=1)
# Convert to bytes, as the `mean` turned our array into floats.
column = column.clip(0, 255).astype('uint8')
# Get us in the right shape for the `hstack` below.
column = column.reshape(-1, 1, 3)
import av
import av.datasets
content = av.datasets.curated('pexels/time-lapse-video-of-night-sky-857195.mp4')
with av.open(content) as container:
# Signal that we only want to look at keyframes.
stream = container.streams.video[0]
stream.codec_context.skip_frame = 'NONKEY'
for frame in container.decode(stream):
print(frame)
# We use `frame.pts` as `frame.index` won't make must sense with the `skip_frame`.
frame.to_image().save(
'night-sky.{:04d}.jpg'.format(frame.pts),
quality=80,
)
container = av.open(av.datasets.curated('pexels/time-lapse-video-of-night-sky-857195.mp4'))
start_time = time.time()
for packet in container.demux():
print(packet)
for frame in packet.decode():
print(frame)
default_time = time.time() - start_time
container.close()
print("Decoding with auto threading...")
container = av.open(av.datasets.curated('pexels/time-lapse-video-of-night-sky-857195.mp4'))
# !!! This is the only difference.
container.streams.video[0].thread_type = 'AUTO'
start_time = time.time()
for packet in container.demux():
print(packet)
for frame in packet.decode():
print(frame)
auto_time = time.time() - start_time
container.close()
print("Decoded with default threading in {:.2f}s.".format(default_time))
print("Decoded with auto threading in {:.2f}s.".format(auto_time))
import av
import av.datasets
input_ = av.open(av.datasets.curated('pexels/time-lapse-video-of-night-sky-857195.mp4'))
output = av.open('remuxed.mkv', 'w')
# Make an output stream using the input as a template. This copies the stream
# setup from one to the other.
in_stream = input_.streams.video[0]
out_stream = output.add_stream(template=in_stream)
for packet in input_.demux(in_stream):
print(packet)
# We need to skip the "flushing" packets that `demux` generates.
if packet.dts is None:
continue
# We need to assign the packet to the new stream.
import os
import subprocess
import av
import av.datasets
# We want an H.264 stream in the Annex B byte-stream format.
# We haven't exposed bitstream filters yet, so we're gonna use the `ffmpeg` CLI.
h264_path = 'night-sky.h264'
if not os.path.exists(h264_path):
subprocess.check_call([
'ffmpeg',
'-i', av.datasets.curated('pexels/time-lapse-video-of-night-sky-857195.mp4'),
'-vcodec', 'copy',
'-an',
'-bsf:v', 'h264_mp4toannexb',
h264_path,
])
fh = open(h264_path, 'rb')
codec = av.CodecContext.create('h264', 'r')
while True:
chunk = fh.read(1 << 16)
packets = codec.parse(chunk)