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

encode timestamps #3883

Open
qishuhua opened this issue Feb 14, 2025 · 4 comments
Open

encode timestamps #3883

qishuhua opened this issue Feb 14, 2025 · 4 comments

Comments

@qishuhua
Copy link

When I use CV: CUDACodec: VideoWriter encoding, video cannot be played in VLC, converted to ffmeg and displayed as follows " Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly"

version:opencv_contrib-4.8.0

@cudawarped
Copy link
Contributor

Can you share the code you use for encoding? Specifically which codec and what is the file extension of the encoded video?

OpenCV 4.8.0 only supports raw output (not containers) so your file extension should be .h264 or .h265 which should play in vlc without issue.

If you want the encoded video to be stored in a container (.mp4) then you need to upgrade to the latest version of OpenCV (latest commits from the 4.x branches of the main and contrib repositories).

@qishuhua
Copy link
Author

qishuhua commented Feb 18, 2025

I want the encoded video to be stored in a container (.mp4),I will try again using the latest version of OpenCV.thank you for your answer

@qishuhua
Copy link
Author

`void TestHWEncoder(const std::string& inputPath, const std::string& outputPath) {
try {
cv::VideoCapture videoReader(inputPath);
if (!videoReader.isOpened())
{
std::cerr << "Can't open input video file" << std::endl;
return;
}
int Width = videoReader.get(cv::CAP_PROP_FRAME_WIDTH);
int Height = videoReader.get(cv::CAP_PROP_FRAME_HEIGHT);
double fps = videoReader.get(cv::CAP_PROP_FPS);

    cv::Mat frame;
    cv::cuda::GpuMat GpuFrame;
    cv::cuda::Stream stream;

    cv::cudacodec::EncoderParams params;
    params.nvPreset = ENC_PRESET_P1;
    params.encodingProfile = ENC_H264_PROFILE_BASELINE;
    params.averageBitRate = 2000000;

    cv::Ptr<cv::cudacodec::VideoWriter>  d_writer = cv::cudacodec::createVideoWriter(outputPath, cv::Size(Width, Height), 
        cv::cudacodec::Codec::H264, fps, cv::cudacodec::ColorFormat::BGR, params,0, stream);

    for (;;)
    {
        if (!videoReader.read(frame))
        {
            std::cout << "Stop" << std::endl;
            break;
        }
        GpuFrame.upload(frame, stream);
        d_writer->write(GpuFrame);
    }
}
catch (const cv::Exception& e) {
    std::cerr << "OpenCV Exception: " << e.what() << std::endl;
}

}`
General configuration for OpenCV 4.12.0-dev =====================================
Version control: 4.11.0-160-gb5f5540e8a

Extra modules:
Location (extra): D:/github/opencv_contrib/modules
Version control (extra): 4.11.0-14-gce3c6681

Platform:
Timestamp: 2025-02-22T10:10:38Z
Host: Windows 10.0.26100 AMD64
CMake: 3.31.5
CMake generator: Visual Studio 16 2019
CMake build tool: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe
MSVC: 1929
Configuration: Debug Release
Algorithm Hint: ALGO_HINT_ACCURATE

Still can't play

@cudawarped
Copy link
Contributor

I just tried your MRE using the big_buck_bunny.h264 test file and it played in vlc without any issues. e.g.

TestHWEncoder(big_buck_bunny.h264, big_buck_bunny.mp4);

Are you writing to an mp4 container? What errors do you get in vlc?

Note: The writer also excepts host frames. i.e. You can use d_writer->write(frame);

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

No branches or pull requests

3 participants