Hi folks, I'm running into a bit of a problem installing on High Sierra: import av
returns:
ImportError: dlopen(/Users/samn/local-projects/tenpercent/rd-hot-desk-radio-studio/rd-recording/split_shows/venv/lib/python3.6/site-packages/av/_core.cpython-36m-darwin.so, 2): Symbol not found: ____chkstk_darwin
Referenced from: /Users/samn/local-projects/tenpercent/rd-hot-desk-radio-studio/rd-recording/split_shows/venv/lib/python3.6/site-packages/av/.dylibs/libavutil.56.31.100.dylib (which was built for Mac OS X 10.15)
Expected in: /usr/lib/libSystem.B.dylib
in /Users/samn/local-projects/tenpercent/rd-hot-desk-radio-studio/rd-recording/split_shows/venv/lib/python3.6/site-packages/av/.dylibs/libavutil.56.31.100.dylib
I think that's down to an ABI change in newer MacOS versions (and is the same problem as pyca/cryptography#5215) - I'm halfway through writing an Issue but the template says I should ask on here first.
Anyone come across this already?
Hi there! I'm running into a bit of a problem trying to write to a video file both an audio and video stream (from pytorch tensors) ---
This is my code so far:
def write_video(
filename,
video_array,
video_fps,
video_codec="libx264",
audio_array=None,
audio_fps=None,
audio_codec=None,
audio_layout=None,
sample_format=None,
options=None
):
video_array = torch.as_tensor(video_array, dtype=torch.uint8).numpy()
container = av.open(filename, mode="w")
stream = container.add_stream(video_codec, rate=video_fps)
stream.width = video_array.shape[2]
stream.height = video_array.shape[1]
stream.pix_fmt = "yuv420p" if video_codec != "libx264rgb" else "rgb24"
stream.options = options or {}
for img in video_array:
frame = av.VideoFrame.from_ndarray(img, format="rgb24")
frame.pict_type = "NONE"
for packet in stream.encode(frame):
container.mux(packet)
# Flush stream
for packet in stream.encode():
container.mux(packet)
if audio_array is not None:
audio_array = torch.as_tensor(audio_array).numpy()
a_stream = container.add_stream(audio_codec, rate=audio_fps)
a_stream.options = {}
frame = av.AudioFrame.from_ndarray(
audio_array, format=sample_format, layout=audio_layout
)
for packet in a_stream.encode(frame):
container.mux(packet)
for packet in a_stream.encode():
container.mux(packet)
# Close the file
container.close()
this is how im calling the function:
video_tensor, audio_tensor, info = torchvision.io.video.read_video("2.mp4", pts_unit="sec")
write_video(
"0.mp4",
video_tensor,
int(info["video_fps"]),
"h264",
audio_tensor,
info["audio_fps"],
"aac",
"stereo",
"fltp",
)
However, I keep getting the following error:
Requested input sample rate 0 is invalid
Traceback (most recent call last):
File "write.py", line 80, in <module>
"fltp",
File "write.py", line 59, in write_video
for packet in a_stream.encode(frame):
File "av/stream.pyx", line 152, in av.stream.Stream.encode
File "av/codec/context.pyx", line 466, in av.codec.context.CodecContext.encode
File "av/audio/codeccontext.pyx", line 40, in av.audio.codeccontext.AudioCodecContext._prepare_frames_for_encode
File "av/audio/resampler.pyx", line 106, in av.audio.resampler.AudioResampler.resample
File "av/audio/resampler.pyx", line 103, in av.audio.resampler.AudioResampler.resample
File "av/error.pyx", line 336, in av.error.err_check
av.error.ValueError: [Errno 22] Invalid argument; last error log: [SWR] Requested input sample rate 0 is invalid
Traceback (most recent call last):
File "av/container/output.pyx", line 25, in av.container.output.close_output
TypeError: 'NoneType' object is not iterable
Exception ignored in: 'av.container.output.OutputContainer.__dealloc__'
Traceback (most recent call last):
File "av/container/output.pyx", line 25, in av.container.output.close_output
TypeError: 'NoneType' object is not iterable
I'm not sure why it says that the sample rate is set to zero when I set it as I add streams, so I'm not sure how to move forward. The video is still being produced, but it just has no audio. Would really appreciate any help!
container.mux(...)
? I think start_encoding() is only run once on the first mux call to write file headers, so the audio stream won't get written into the file headers after that (which tallies up with the libavformat docs). However I've not actually tested that I'm afraid!
AudioFrame.from_ndarray
doesn't set the sample rate, try setting it directly after you create the frame, which is what the tests seem to do
I am having trouble installing PyAV.
$ pip install av
Collecting av
Using cached https://files.pythonhosted.org/packages/4d/fe/170a64b51c8f10df19fd2096388e51f377dfec07f0bd2f8d25ebdce6aa50/av-8.0.1.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-4yV5M1/av/setup.py", line 9, in <module>
from shlex import quote
ImportError: cannot import name quote
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-4yV5M1/av/
copy
"dummy" codecContext? I'm opening a container with av.open(file="/dev/video0", format="v4l2", mode="r", options={"video_size": "1920x1080", "framerate": "30", "input_format": "h264"})
since RPi camera and Logitech C920 can give HW accelerated H264 stream, but then I have trouble working with the Packet object to bitstream over webRTC. Any clues?
ffmpeg -i book.m4b -f ffmetadata book.metadata.txt
). With pyav, I can see a data stream that has 56 'frames', but I'm uncertain how (or if) I can process that chapter info. Does anyone know if this is possible as things stand now?
$PYAV_PYTHON setup.py bdist_wheel
...
if __name__ == "__main__":
fh = open('video_bytes.bin', 'rb')
codec = av.CodecContext.create('h264', 'r')
while True:
chunk = fh.read(1 << 16)
packets = codec.parse(str(chunk))
print("Parsed {} packets from {} bytes:".format(len(packets), len(chunk)))
for packet in packets:
print(' ', packet)
frames = codec.decode(packet)
for frame in frames:
print(' ', frame)
chunk
directly, but parse complained that chunk
was not a str
packet.to_bytes()
) and to a stream container
in_cont = av.open(curr_tempfile.name, mode='r')
out_cont = av.open(output_tempfile.name, mode='w')
in_stream = in_cont.streams.audio[0]
out_stream = out_cont.add_stream(
codec_name=in_stream.codec_context.codec,
rate=audio_signal.sample_rate
)
graph = Graph()
fchain = [graph.add_abuffer(sample_rate=in_stream.rate,
format=in_stream.format.name, layout=in_stream.layout.name,
channels=audio_signal.num_channels)]
for _filter in filters:
fchain.append(_filter(graph))
fchain[-2].link_to(fchain[-1])
fchain.append(graph.add("abuffersink"))
fchain[-2].link_to(fchain[-1])
graph.configure()
for ifr in in_cont.decode(in_stream):
graph.push(ifr)
ofr = graph.pull()
ofr.pts = None
for p in out_stream.encode(ofr):
out_cont.mux(p)
out_cont.close()
filters
contains lambda functions of the format: lambda graph: graph.add("name_of_filter", "named_filter_arguments")
_filter
in filters
would be lambda graph: graph.add("tremolo", "f=15:d=.5")