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

using LGPL compiled ffmpeg shared for FFmpeg n5.0 from [here](https:/ #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

camerongibbs-eic
Copy link
Owner

@camerongibbs-eic camerongibbs-eic commented Nov 28, 2023

  • specifically - ffmpeg-n5.0-latest-win64-lgpl-shared-5.0.zip

targets openH264 rather than the GPL x264 encoder

modifies FFmpeg.AutoGen.Example to output an MP4 rather than .h264 file by way of:

  • adding AVFormatContext and AVStream
  • using ffmpeg.avio_open to create / write the file instead of System.IO.xxx
  • writing a header and trailer
  • using ffmpeg.av_interleaved_write_frame()

includes a somewhat mangled but updated and somewhat working version of the Transcoding gist shared by Ruslan-B

Summary by CodeRabbit

  • New Features

    • Introduced a new transcoding project with capabilities to handle video and audio stream processing.
    • Added a new class for registering FFmpeg binaries, enhancing cross-platform support.
    • Implemented improved error handling with more informative messages in FFmpeg helper classes.
  • Bug Fixes

    • Updated the video encoding process to handle different file formats and frame rates.
  • Documentation

    • Updated .gitignore to exclude specific test files and images from version control.
  • Refactor

    • Removed unused data structures and functions related to post-processing.
    • Updated project files to reflect changes in the codebase and library versions.
  • Chores

    • Updated FFmpeg library version to include the latest fixes and improvements.

…/github.com/BtbN/FFmpeg-Builds/releases)

* specifically - ffmpeg-n5.0-latest-win64-lgpl-shared-5.0.zip

targets openH264  rather than the GPL x264 encoder

modifies FFmpeg.AutoGen.Example to output an MP4 rather than .h264 file by way of:
* adding AVFormatContext and AVStream
* using ffmpeg.avio_open to create / write the file instead of System.IO.xxx
* writing a header and trailer
* using ffmpeg.av_interleaved_write_frame()

includes a somewhat mangled but updated and somewhat working version of the Transcoding gist shared by Ruslan-B
Copy link

coderabbitai bot commented Nov 28, 2023

Walkthrough

The changes involve updates to the FFmpeg.AutoGen project, including the addition of a new transcoding example project, enhancements to error handling, and the removal of post-processing features. The .gitignore file was updated to ignore new patterns, and the FFmpeg library version was bumped. The transcoding example adds comprehensive functionality for video and audio processing, while other modifications improve usability and maintainability of the code.

Changes

File(s) Summary
.gitignore Added patterns to ignore video test files and frame images.
FFmpeg.AutoGen.Example.Transcoding/... Added a new transcoding example project with classes and methods for FFmpeg binaries registration, error handling, and the transcoding process.
FFmpeg.AutoGen.Example/FFmpegHelper.cs, FFmpeg.AutoGen.Example/H264VideoStreamEncoder.cs, FFmpeg.AutoGen.Example/Program.cs, FFmpeg.AutoGen.Example/VideoFrameConverter.cs Improved error messaging, updated video encoding settings and logic, and reformatted code for better readability.
FFmpeg.AutoGen.sln, FFmpeg.AutoGen.Example.Transcoding/...csproj Added a new transcoding project to the solution and set up a .NET 6.0 project with nullable reference types and unsafe code allowance.
FFmpeg.AutoGen/FFmpeg.arrays.g.cs, FFmpeg.AutoGen/FFmpeg.functions.export.g.cs, FFmpeg.AutoGen/FFmpeg.libraries.g.cs, FFmpeg.AutoGen/FFmpeg.macros.g.cs Removed data structures, function definitions, and constants related to the deprecated post-processing library.
FFmpeg/include/libavutil/ffversion.h Updated the FFmpeg library version to a newer release.

🐇✨
In the realm of code, where binaries align,
A rabbit hopped through, with changes so fine.
Transcoding anew, with errors caught tight,
No postproc in view, the code's future bright. 🌟
🐇✨


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 19

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 24da0e7 and d6af667.
Files ignored due to filter (8)
  • FFmpeg/bin/x64/avcodec-59.dll
  • FFmpeg/bin/x64/avdevice-59.dll
  • FFmpeg/bin/x64/avfilter-8.dll
  • FFmpeg/bin/x64/avformat-59.dll
  • FFmpeg/bin/x64/avutil-57.dll
  • FFmpeg/bin/x64/postproc-56.dll
  • FFmpeg/bin/x64/swresample-4.dll
  • FFmpeg/bin/x64/swscale-6.dll
Files selected for processing (16)
  • .gitignore (1 hunks)
  • FFmpeg.AutoGen.Example.Transcoding/FFmpeg.AutoGen.Example.Transcoding.csproj (1 hunks)
  • FFmpeg.AutoGen.Example.Transcoding/FFmpegBinariesHelper.cs (1 hunks)
  • FFmpeg.AutoGen.Example.Transcoding/FFmpegHelper.cs (1 hunks)
  • FFmpeg.AutoGen.Example.Transcoding/Program.cs (1 hunks)
  • FFmpeg.AutoGen.Example.Transcoding/Transcoding.cs (1 hunks)
  • FFmpeg.AutoGen.Example/FFmpegHelper.cs (1 hunks)
  • FFmpeg.AutoGen.Example/H264VideoStreamEncoder.cs (2 hunks)
  • FFmpeg.AutoGen.Example/Program.cs (6 hunks)
  • FFmpeg.AutoGen.Example/VideoFrameConverter.cs (2 hunks)
  • FFmpeg.AutoGen.sln (2 hunks)
  • FFmpeg.AutoGen/FFmpeg.arrays.g.cs (1 hunks)
  • FFmpeg.AutoGen/FFmpeg.functions.export.g.cs (1 hunks)
  • FFmpeg.AutoGen/FFmpeg.libraries.g.cs (1 hunks)
  • FFmpeg.AutoGen/FFmpeg.macros.g.cs (2 hunks)
  • FFmpeg/include/libavutil/ffversion.h (1 hunks)
Files skipped from review due to trivial changes (7)
  • .gitignore
  • FFmpeg.AutoGen.Example.Transcoding/FFmpeg.AutoGen.Example.Transcoding.csproj
  • FFmpeg.AutoGen.Example/FFmpegHelper.cs
  • FFmpeg.AutoGen.Example/VideoFrameConverter.cs
  • FFmpeg.AutoGen/FFmpeg.arrays.g.cs
  • FFmpeg.AutoGen/FFmpeg.libraries.g.cs
  • FFmpeg/include/libavutil/ffversion.h
Additional comments: 9
FFmpeg.AutoGen.Example.Transcoding/FFmpegHelper.cs (1)
  • 13-13: The conversion from the buffer to a string assumes ANSI encoding, which is appropriate for FFmpeg error messages.
FFmpeg.AutoGen.Example/H264VideoStreamEncoder.cs (4)
  • 19-88: The constructor has been significantly refactored to initialize FFmpeg structures and perform error checking. Ensure that all instances where H264VideoStreamEncoder is instantiated are updated to pass a filename instead of a Stream object.

  • 119-163: The Encode method now takes an additional parameter bool skipFrameCheck and returns an int. Verify that all calls to Encode have been updated to handle the new parameter and return type correctly.

  • 106-117: The Flush method is a new addition to the class. Review this method to ensure it correctly handles the flushing of the stream encoder according to FFmpeg's requirements.

  • 19-88: Improved exception handling with more specific error messages is a good practice. Ensure that the error messages provided are informative and accurately describe the error condition.

FFmpeg.AutoGen.Example/Program.cs (1)
  • 207-207: Verify that the "./images" directory exists before attempting to read files from it to avoid potential exceptions.
FFmpeg.AutoGen/FFmpeg.functions.export.g.cs (1)
  • 15996-15998: The hunk appears to be incomplete, showing only the definition of a delegate and the start of a lambda function. Ensure that the lambda function is correctly implemented and that the delegate is properly used within the codebase.
FFmpeg.AutoGen/FFmpeg.macros.g.cs (2)
  • 1466-1471: The constants related to LIBAVUTIL and LIBSWRESAMPLE remain unchanged and appear to be correctly defined.

  • 1522-1527: The parser flags are defined correctly, and the commented-out macros for UTF encoding and rounded division do not affect the functionality. If these macros are no longer needed, it's good to keep them commented out or remove them entirely for clarity.

{
FFmpegBinariesHelper.RegisterFFmpegBinaries();
var transcoding = new Transcoding();
transcoding.main(args.Length, args);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name main does not follow C# naming conventions. Consider renaming it to Main or another appropriate PascalCase name to align with best practices.

Comment on lines +7 to +9
FFmpegBinariesHelper.RegisterFFmpegBinaries();
var transcoding = new Transcoding();
transcoding.main(args.Length, args);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no error handling for exceptions that might be thrown by RegisterFFmpegBinaries or transcoding.main. Consider adding try-catch blocks to handle potential exceptions gracefully.

{
public static unsafe string av_strerror(int error)
{
var bufferSize = 1024;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider defining the buffer size as a constant to improve code readability and maintainability.

- var bufferSize = 1024;
+ const int ErrorBufferSize = 1024;
+ var bufferSize = ErrorBufferSize;

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
var bufferSize = 1024;
const int ErrorBufferSize = 1024;
var bufferSize = ErrorBufferSize;

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FFmpeg.AutoGen.Examples.ResamplingAudio", "FFmpeg.AutoGen.Examples.ResamplingAudio\FFmpeg.AutoGen.Examples.ResamplingAudio.csproj", "{A192914F-DAA8-400B-B5CA-BA188AEBB42B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FFmpeg.AutoGen.Examples.ResamplingAudio", "FFmpeg.AutoGen.Examples.ResamplingAudio\FFmpeg.AutoGen.Examples.ResamplingAudio.csproj", "{A192914F-DAA8-400B-B5CA-BA188AEBB42B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FFmpeg.AutoGen.Example.Transcoding", "FFmpeg.AutoGen.Example.Transcoding\FFmpeg.AutoGen.Example.Transcoding.csproj", "{50CC883F-A967-4880-BF1A-0DBBB78B9454}"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project type GUID for "FFmpeg.AutoGen.Example.Transcoding" should be updated to match the new format used by other projects in the solution: "{9A19103F-16F7-4668-BE54-9A1E7A4F7556}".

- Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FFmpeg.AutoGen.Example.Transcoding", "FFmpeg.AutoGen.Example.Transcoding\FFmpeg.AutoGen.Example.Transcoding.csproj", "{50CC883F-A967-4880-BF1A-0DBBB78B9454}"
+ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FFmpeg.AutoGen.Example.Transcoding", "FFmpeg.AutoGen.Example.Transcoding\FFmpeg.AutoGen.Example.Transcoding.csproj", "{50CC883F-A967-4880-BF1A-0DBBB78B9454}"

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FFmpeg.AutoGen.Example.Transcoding", "FFmpeg.AutoGen.Example.Transcoding\FFmpeg.AutoGen.Example.Transcoding.csproj", "{50CC883F-A967-4880-BF1A-0DBBB78B9454}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FFmpeg.AutoGen.Example.Transcoding", "FFmpeg.AutoGen.Example.Transcoding\FFmpeg.AutoGen.Example.Transcoding.csproj", "{50CC883F-A967-4880-BF1A-0DBBB78B9454}"

Comment on lines +17 to +21
public static int ThrowExceptionIfError(this int error)
{
if (error < 0) throw new ApplicationException(av_strerror(error));
return error;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name ThrowExceptionIfError could be improved to follow .NET naming conventions for methods that can throw exceptions. Additionally, consider using a more specific exception type than ApplicationException.

- public static int ThrowExceptionIfError(this int error)
+ public static int EnsureSuccessOrThrow(this int error)
{
    if (error < 0) throw new FFmpegException(av_strerror(error));
    return error;
}

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public static int ThrowExceptionIfError(this int error)
{
if (error < 0) throw new ApplicationException(av_strerror(error));
return error;
}
public static int EnsureSuccessOrThrow(this int error)
{
if (error < 0) throw new FFmpegException(av_strerror(error));
return error;
}

Comment on lines +430 to +433
if (in_stream->codecpar->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
filter_spec = "null"; /* passthrough (dummy) filter for video */
else
filter_spec = "anull"; /* passthrough (dummy) filter for audio */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic strings like "null" and "anull" used as filter specifications should be defined as constants to improve code readability and maintainability.

Comment on lines +574 to +575
if ((ret = ffmpeg.av_read_frame(ifmt_ctx, packet)) < 0)
break;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that all possible error codes returned by ffmpeg.av_read_frame are handled appropriately, not just AVERROR_EOF.

Comment on lines +564 to +567
if ((ret = open_input_file(argv[1])) < 0)
goto end;
if ((ret = open_output_file(argv[2])) < 0)
goto end;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider refactoring the use of goto statements for error handling to a more structured approach, such as try-catch blocks or other control flow mechanisms.

Comment on lines +116 to +117
ffmpeg.avformat_alloc_output_context2(&ofmt_ctx_fixed, null, null, filename);
if (ofmt_ctx_fixed == null)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of ffmpeg.avformat_alloc_output_context2 is not checked. Ensure that the function succeeded and handle the case where it returns a null pointer.

Comment on lines +662 to +684
ffmpeg.av_packet_unref(packet);
ffmpeg.av_packet_free(&packet);
ffmpeg.av_frame_free(&frame);
for (i = 0; i < ifmt_ctx->nb_streams; i++)
{
ffmpeg.avcodec_free_context(&stream_ctx[i].dec_ctx);
if (ofmt_ctx != null && (ofmt_ctx->nb_streams > i) && ofmt_ctx->streams[i] != null && stream_ctx[i].enc_ctx != null)
ffmpeg.avcodec_free_context(&stream_ctx[i].enc_ctx);
if (filter_ctx != null && filter_ctx[i].filter_graph != null)
ffmpeg.avfilter_graph_free(&filter_ctx[i].filter_graph);
}

ffmpeg.av_free(filter_ctx);
ffmpeg.av_free(stream_ctx);

AVFormatContext* ifmt_ctx_fixed = ifmt_ctx;
ffmpeg.avformat_close_input(&ifmt_ctx_fixed);

if (ofmt_ctx != null && (ofmt_ctx->oformat->flags & ffmpeg.AVFMT_NOFILE) == 0)
ffmpeg.avio_closep(&ofmt_ctx->pb);
ffmpeg.avformat_free_context(ofmt_ctx);

if (ret < 0)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before freeing resources in the 'end' label section, check if the pointers are null to avoid potential undefined behavior.

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

Successfully merging this pull request may close these issues.

1 participant