BabitMF / bmf

Cross-platform, customizable multimedia/video processing framework. With strong GPU acceleration, heterogeneous design, multi-language support, easy to use, multi-framework compatible and high performance, the framework is ideal for transcoding, AI inference, algorithm integration, live video streaming, and more.
https://babitmf.github.io/
Apache License 2.0
816 stars 71 forks source link

test push data with raw frame got an error #63

Closed lanch closed 1 year ago

lanch commented 1 year ago

I use h264 as encode codec, and got the error bellow, and I change this to be mpeg4 and it works well. My os is ubuntu 20.04 and python is 3.9 and pip install BabitMF

[2023-09-25 03:08:33.274] [error] node id:1 Codec 'libx264' not found [2023-09-25 03:08:33.274] [error] node id:1 init codec error [2023-09-25 03:08:33.274] [error] node id:1 catch exception: BMF(0.0.8) /project/bmf/engine/c_engine/src/node.cpp:352: error: (-5:Bad argument) [Node_1_c_ffmpeg_encoder] Process result != 0. in function 'process_node'

[2023-09-25 03:08:33.274] [error] node id:1 Process node failed, will exit.

import io

import numpy as np
import bmf
from bmf import GraphMode, Module, Log, LogLevel, InputType, ProcessResult, Packet, Timestamp, scale_av_pts, av_time_base, BmfCallBackType, VideoFrame, AudioFrame, BMFAVPacket
from PIL import Image

def init_push_graph(output):
    graph = bmf.graph({"dump_graph": 1, "loglevel": "debug"})
    video_stream = graph.input_stream("video_stream")
    # audio_stream = graph.input_stream("wav_stream")
    decode_stream = video_stream.decode({
        "loglevel": "trace",
        's': '720:1280',
        'pix_fmt': 'rgb24',
        "push_raw_stream": 1,
        "video_codec": "bmp",
        "video_time_base": "1,30000"
        })

    bmf.encode(
            decode_stream,
            None,
            {
                "video_params": {
                    "codec": "h264",
                    "width": 720,
                    "height": 1280,
                    "max_fr": 30,
                    "crf": "23",
                    "preset": "veryfast"
                },
                # "audio_params": {"sample_rate": 44100, "codec": "aac"},
                "loglevel": "trace",
                "output_path": output
            },
        )
    graph.run_wo_block(mode=GraphMode.PUSHDATA)
    return graph

graph = init_push_graph('./test1.mp4')

pts = 0
timestamp = 0
for _ in range(100):
    frame = np.zeros((1280, 720, 3), dtype=np.uint8)
    image = Image.fromarray(frame, mode="RGB")
    byte_stream = io.BytesIO()
    image.save(byte_stream, format='BMP')
    image_bytes = byte_stream.getvalue()
    pkt = BMFAVPacket(len(image_bytes))
    memview = pkt.data.numpy()
    memview[:] = np.frombuffer(image_bytes, dtype=np.uint8)
    pkt.pts = pts
    packet = Packet(pkt)
    packet.timestamp = timestamp
    pts += 1001
    timestamp += 1
    graph.fill_packet("video_stream", packet)

graph.fill_packet("video_stream", Packet.generate_eof_packet())
graph.close()
lanch commented 1 year ago

I fix this by reinstall ffmpeg with

sudo apt install ffmpeg x264