PyAV-Org / PyAV

Pythonic bindings for FFmpeg's libraries.
https://pyav.basswood-io.com/
BSD 3-Clause "New" or "Revised" License
2.45k stars 359 forks source link

How can i use pyav to copy some frame all the same #821

Closed i3587616 closed 2 years ago

i3587616 commented 2 years ago

hello: i build a class like import av import av.datasets class VideoWriter(): def init(self, fileStr, size, fps,codec):

self.fileStr = fileStr

    #self.size = size
    #self.fps = fps
    #self.codec = codec
    self.container = av.open(fileStr, mode='w')
    self.stream = self.container.add_stream(codec, rate=fps)
    self.stream.width,self.stream.height = size
    #self.stream.pix_fmt = 'yuv420p'
    self.stream.thread_type = 'AUTO'
    self.stream.profile = 'High'
def write(self,frame):
    ndArray = frame.to_ndarray(format='bgr24')
    frame_new = av.VideoFrame.from_ndarray(ndArray, format='bgr24')

    for packet in self.stream.encode(frame_new):
        self.container.mux(packet)
def release(self):
    for packet in self.stream.encode():
        self.container.mux(packet)
    self.container.close()

my problem : i use this to write frames what i need to video .but the rsl video is different with inputvideo eg.inputvideo is avc 格式档次 : Main@L4.0 but outvideo is avc 格式档次 : High@L4.0 格式参数设置, CABAC : Yes and size is 10M and i use opencv VideoWrite.write ,then outvideo is avc Baseline@L4.1 and size is 100M

i find opencv VideoWrite ,its frame to image is more good than my class did my problem is how can i choose the 格式档次 main or high or Baseline

i try self.stream.profile = 'High' but failed: AttributeError: attribute 'profile' of 'av.codec.context.CodecContext' objects is not writable

i only need to write frame what i need to a video and hope the video image as the same with orivideo how can i do it with pyav

i3587616 commented 2 years ago

i think should bitrate be assign can do.but how can i assign the bitrate value by pyav

i3587616 commented 2 years ago

self.stream.codec_context.bit_rate = 2150000 i use this to do it.

i3587616 commented 2 years ago

it is fault,this can not solve the problem

i3587616 commented 2 years ago

vbr is good idea ,but how can i let pyav do it

i3587616 commented 2 years ago

i am so sorry,my unstand is wrong.vbr is just add cloud.then video is vbr. and bitrate preset will affect.