如何在 C# 中从视频中提取字幕(教程)
在本教程中,我们将学习如何从视频文件中提取硬编码字幕。 我们将采用一个示例视频文件,并将硬编码字幕提取到文本文件中。我们将开发一个C# .NET程序,通过OCR过程提取硬编码的字幕。 我将保持本教程的简单易懂,即使是 C# 程序员初学者也能看懂。
我们需要一个高效的光学字符识别 (OCR) 引擎,它可以处理视频并获取字幕文件,而无需考虑字幕语言。
有许多库可以提供 OCR 结果。 这些工具中有些是收费的,有些难以使用,有些效率不高或不准确,因此很难找到一个免费、高效、易用且结果准确的库。
IronOCR 用于开发是免费的,用于商业目的则提供一个月的免费试用期。 它支持 150 多种语言,比现有的大多数其他 OCR 库具有更高的准确性。 同时还要高效易用。 我们将使用该库进行演示。
如何在 C# 中进行 OCR 字幕识别
- 安装 C# 库以对字幕执行 OCR 识别。
- 将带有字幕的图像导入到新的
OcrInput实例中 - 通过应用选定的滤波器对图像进行预处理。
- 指定图像中字幕的位置,以提高OCR性能和准确性
- 将检索到的文本导出为文本文件
IronOCR。
IronOCR是由 Iron Software 开发和维护的一个库,可帮助 C# 软件工程师在 .NET 项目中执行 OCR、Barcode 扫描和文本提取。
IronOCR 的功能包括
- 从多种格式中读取文本,如图像(JPEG、PNG、BMP)、GIF、TIF/TIFF、流和PDF。
- 使用大量滤镜(如纠偏、去噪、二值化、增强分辨率、放大等)修正 低质量扫描和照片。
- 读取 20 多种不同格式的 BarCode,以及 QR 代码支持。
- 使用 Tesseract OCR 的最新版本,其性能经过调整后已超越其他同类库
- 导出可搜索的 PDF、hOCR / HTML 导出和图像内容文本。
让我们开发一个读取车牌号码的演示应用程序。
创建Visual Studio项目
第一步是创建一个新项目。
打开 Visual Studio。 单击 创建新项目,然后选择控制台应用程序项目模板。
单击下一步按钮,并为项目命名(我将其命名为 "OCR 字幕",您可以根据自己的选择来命名)。
单击下一步按钮,选择目标框架。 最后,单击创建按钮创建项目。
项目创建如下所示。
。 在Visual Studio中创建新项目
现在,我们需要安装 IronOCR 库,以便在项目中使用它。 最简单的方法是通过 NuGet Package Manager for Solution 进行安装。
安装 IronOCR NuGet 软件包
点击顶部菜单栏中的工具,然后选择NuGet包管理器 > 管理解决方案的NuGet包,如下所示。
在 Visual Studio 中安装 IronOCR。
将出现以下窗口。
。 Visual Studio NuGet包管理器界面
点击浏览,搜索 IronOCR。 选择 IronOCR 软件包,然后单击Install按钮,如下图所示。
在 NuGet 软件包管理器用户界面中搜索 IronOCR。
IronOCR 库将已安装并可随时使用。
提取硬编码字幕
让我们编写一个提取硬编码字幕的程序。
我们将使用以下截图提取字幕。
。 将提取文本的示例视频截图
添加以下命名空间:
using IronOcr;using IronOcr;Imports IronOcr在命名空间声明下方编写以下代码。
// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input using the specified image path
using (var input = new OcrInput(@"D:\License Plate\plate3.jpg"))
{
// Perform OCR on the input image to extract text
var result = ocr.Read(input);
// Output the extracted text to the console
Console.WriteLine(result.Text);
}// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input using the specified image path
using (var input = new OcrInput(@"D:\License Plate\plate3.jpg"))
{
// Perform OCR on the input image to extract text
var result = ocr.Read(input);
// Output the extracted text to the console
Console.WriteLine(result.Text);
}' Initialize IronTesseract object
Dim ocr = New IronTesseract()
' Create an OCR Input using the specified image path
Using input = New OcrInput("D:\License Plate\plate3.jpg")
' Perform OCR on the input image to extract text
Dim result = ocr.Read(input)
' Output the extracted text to the console
Console.WriteLine(result.Text)
End Using上述代码的工作原理如下:
1.初始化 IronTesseract 对象。 它将创建 IronTesseract 的默认实例。 2.创建一个新的 OcrInput 对象,并填充输入图像文件或 PDF 文档。 OcrInput 是首选的输入类型,因为它允许对多页文档进行 OCR,并允许在 OCR 之前对图像进行增强,以获得更快、更准确的结果。
- 从OCR输入对象读取文本并返回OCR结果对象。
ocr.Read将从给定的输入截图中提取字幕。 4.result.Text将返回从给定输入中提取的全部内容。
示例程序产生的控制台输出如下:
。 使用IronOCR在示例图像上执行文本提取时生成的控制台输出
假设您有一个包含视频标题和字幕的视频帧:
。 较长视频的单帧,包含视频标题和视频字幕的文本区域
我们的目标是提取图像底部的硬编码字幕。 在这种情况下,我们需要指定显示字幕的文本区域。
在帧中指定字幕位置
我们可以使用 System.Drawing.Rectangle 来指定一个区域,在该区域中我们将从视频帧中读取字幕。 计量单位始终为 像素。
我们将使用以下示例代码来指定文本区域。
// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input and specify the region of interest
using (var input = new OcrInput())
{
// Define the area within the image where subtitles are located for a 41% improvement on speed
var contentArea = new CropRectangle(x: 189, y: 272, height: 252, width: 77);
// Add the specific region of the image to the OCR input
input.AddImage(@"D:\subtitle\image.png", contentArea);
// Perform OCR on the specified region
var result = ocr.Read(input);
// Output the extracted text to the console
Console.WriteLine(result.Text);
}// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input and specify the region of interest
using (var input = new OcrInput())
{
// Define the area within the image where subtitles are located for a 41% improvement on speed
var contentArea = new CropRectangle(x: 189, y: 272, height: 252, width: 77);
// Add the specific region of the image to the OCR input
input.AddImage(@"D:\subtitle\image.png", contentArea);
// Perform OCR on the specified region
var result = ocr.Read(input);
// Output the extracted text to the console
Console.WriteLine(result.Text);
}' Initialize IronTesseract object
Dim ocr = New IronTesseract()
' Create an OCR Input and specify the region of interest
Using input = New OcrInput()
' Define the area within the image where subtitles are located for a 41% improvement on speed
Dim contentArea = New CropRectangle(x:= 189, y:= 272, height:= 252, width:= 77)
' Add the specific region of the image to the OCR input
input.AddImage("D:\subtitle\image.png", contentArea)
' Perform OCR on the specified region
Dim result = ocr.Read(input)
' Output the extracted text to the console
Console.WriteLine(result.Text)
End Using这样可以提高41%的速度,并使我们的翻译更加具体。 在 contentArea 中,我们以 x 和 y 为单位指定了起点,然后指定了所需字幕区域的高度和宽度。
将字幕保存到字幕文本文件中
让我们将提取的字幕保存到文本文件中。
// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input with the specified image path
using (var input = new OcrInput(@"D:\subtitle\subtitle1.png"))
{
// Perform OCR on the input image to extract text
var result = ocr.Read(input);
// Save the extracted text to a specified file path
result.SaveAsTextFile(@"D:\subtitle\subtitlefile.txt");
}// Initialize IronTesseract object
var ocr = new IronTesseract();
// Create an OCR Input with the specified image path
using (var input = new OcrInput(@"D:\subtitle\subtitle1.png"))
{
// Perform OCR on the input image to extract text
var result = ocr.Read(input);
// Save the extracted text to a specified file path
result.SaveAsTextFile(@"D:\subtitle\subtitlefile.txt");
}' Initialize IronTesseract object
Dim ocr = New IronTesseract()
' Create an OCR Input with the specified image path
Using input = New OcrInput("D:\subtitle\subtitle1.png")
' Perform OCR on the input image to extract text
Dim result = ocr.Read(input)
' Save the extracted text to a specified file path
result.SaveAsTextFile("D:\subtitle\subtitlefile.txt")
End Usingresult.SaveAsTextFile 将输出路径作为参数,并将文件保存在给定的路径中。
。 较长视频的单帧,包含视频标题和视频字幕的文本区域
总结
在本教程中,我们已经学会了使用 IronOCR,并开发了一个非常简单的程序来从视频截图中读取字幕。 我们还可以指定要提取文本的区域。
IronOCR 为OpenCV提供了计算机视觉的功能。 我们已经看到,IronOCR 可以让我们从模糊或低分辨率的图像中读取文字。 该库高效、准确。 它支持 125 种以上语言,完全准确。 开发免费,生产无限制。
总之,IronOCR 提供以下服务:
- 扫描和阅读图像和扫描文件的能力
- 支持 150 多种全球语言。
- 输出为文本、结构化数据或可搜索的PDF
- 支持 .NET 6、5、Core、Standard、Framework
IronOCR是 Iron Software 套装库的一部分,可用于读写 PDF、操作 Excel 文件、从图像中读取文本和从网站中抓取内容。 以两个单独库的价格购买完整的Iron Suite。
常见问题解答
如何在 C# 中从视频文件中提取硬编码的字幕?
您可以使用 IronOCR 在 C# 中从视频文件中提取硬编码的字幕。通过 NuGet 包管理器安装库,然后使用它处理视频帧并提取文本。
使用 IronOCR 提取字幕与 Tesseract 相比有什么优势?
IronOCR 提供了 Tesseract 的升级替代方案,具有更高的准确性、易用性和对 150 多种语言的支持,非常适合从视频中提取字幕。
如何在 IronOCR 中指定字幕位置以提高处理速度?
您可以使用 System.Drawing.Rectangle 在 IronOCR 中指定字幕位置,以专注于感兴趣的区域,这可以将处理速度提高多达 41%。
提取字幕时是否可以使用 IronOCR 支持非英语语言?
是的,IronOCR 支持超过 150 种语言,使其能够准确地从多个语言的视频中提取字幕。
遵循 C# 字幕 OCR 教程的前提条件是什么?
本教程要求具有 C# 编程的基础知识,并能够使用 Visual Studio 通过 NuGet 包管理器安装 IronOCR 库。
IronOCR 如何处理低质量的视频帧?
IronOCR 包含纠正低质量扫描的功能,提高从次优视频帧中提取文本的准确性。
使用 IronOCR 提取字幕后可用哪些输出格式?
提取的字幕可以使用 IronOCR 保存为文本文件、结构化数据或可搜索的PDF。
在商业项目中使用 IronOCR 是否有费用?
IronOCR 对于开发用途是免费的,并为商业项目提供一个月的免费试用。对于持续的商业使用,需要购买许可证。
IronOCR 可以与其他库集成以增加功能吗?
是的,IronOCR 可以与其他 Iron Software 库集成,以完成如PDF操作和网页抓取任务,增强其功能。






