Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android MP4文件写入 #360

Open
yymjr opened this issue Nov 8, 2024 · 5 comments
Open

Android MP4文件写入 #360

yymjr opened this issue Nov 8, 2024 · 5 comments

Comments

@yymjr
Copy link

yymjr commented Nov 8, 2024

我在Android上使用了该库,但是目前出现了一个问题我不知道是否正常。

关闭mp4写入时会调用mov_writer_destroy,但是几乎需要很长时间都没有执行完该函数。我想知道如何优化或者是什么原因导致的呢?
火焰图如下
image

@yymjr
Copy link
Author

yymjr commented Nov 8, 2024

构造部分:

    void *param = new weak_ptr<MP4Muxer>(shared_from_this());
    mov_write = shared_ptr<mov_writer_t>(mov_writer_create(&mov_io, param, MOV_FLAGS),
                                         mov_writer_destroy);

写入部分:

void MP4Muxer::write_video_data(uint8_t *buffer, AMediaCodecBufferInfo *info) {
    int flags = 0;
    if (unlikely((info->flags & 1) != 0)) {
        flags = MOV_AV_FLAG_KEYFREAME;
    }

    shared_lock<shared_mutex> lock{_m};
    if (unlikely(track_id_invalid())) {
        _cv.wait(lock, [=]() -> bool {
            return !track_id_invalid();
        });
    }
    hxxx_iterator_ctx_t ctx;
    hxxx_iterator_init(&ctx, buffer + info->offset, info->size, 0);
    const uint8_t *p_nal;
    size_t i_nal;
    int64_t pts = info->presentationTimeUs;
    while (hxxx_annexb_iterate_next(&ctx, &p_nal, &i_nal)) {
        mov_writer_write(mov_write.get(), video_track_id, p_nal, i_nal, pts, pts, flags);
    }
}

@ireader
Copy link
Owner

ireader commented Nov 8, 2024

mov_flags值是什么,有没有包含FastStart? FastStart Flag会在mp4文件结束时,把moov移动到文件头部,会导致整个mp4文件重写一遍。

可以尝试去掉FastStart标记后再试试。

@yymjr
Copy link
Author

yymjr commented Nov 8, 2024

在去掉MOV_FLAG_FASTSTART后,确实是可以快速关闭了

const int MOV_FLAGS = MOV_FLAG_SEGMENT;

但是出现了moov atom not found的情况

@yymjr
Copy link
Author

yymjr commented Nov 8, 2024

附上生成的视频文件

2024-11-08_16_49_29.mp4

@ireader
Copy link
Owner

ireader commented Nov 9, 2024

MOV_FLAG_SEGMENT 这个是生成fmp4的,普通文件没必要用这些参数。 如果不太明确每个参数的作用,可以参考libmov/test目录下的例子,或者把flags设置成0。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants