2023-05-04:用go语言重写ffmpeg的scaling_video.c示例,用于实现视频缩放(Scaling)功能。

news/2024/7/10 20:14:15 标签: ffmpeg, golang, c语言

2023-05-04:用go语言重写ffmpeg的scaling_video.c示例,用于实现视频缩放(Scaling)功能。

答案2023-05-04:

这段代码实现了使用 libswscale 库进行视频缩放的功能。下面是程序的主要流程:

1.获取命令行参数,包括输出文件名和目标图像大小。

2.解析目标图像大小,生成指定大小的输出文件。

3.创建缩放上下文(scaling context)并分配输入和输出图像数据的内存空间。

4.循环生成合成图像、将输入图像转换为输出图像并将输出图像写入输出文件中,重复该操作若干次。

5.释放内存空间并关闭输出文件。

具体步骤如下:

1.获取命令行参数。首先检查命令行参数是否符合要求,如果不符合则打印使用说明并退出程序。否则,解析输出文件名和目标图像大小。

2.解析目标图像大小。调用 libavutil.AvParseVideoSize() 函数解析目标图像大小,并根据解析结果生成一个指定大小的输出文件。

3.创建缩放上下文并分配内存空间。调用 libswscale.SwsGetContext() 函数创建一个缩放上下文,并使用 libavutil.AvImageAlloc() 函数分配输入和输出图像数据的内存空间。

4.循环处理图像。在循环中,首先生成一个 YUV420P 格式的合成图像。然后,调用 libswscale.SwsScale() 函数将输入图像转换为输出图像。最后,将输出图像写入输出文件中。在本程序中,处理图像的循环次数为 100 次。

5.释放内存空间并关闭输出文件。在程序结束时,需要释放输入和输出图像数据的内存空间,并关闭输出文件。

整个程序的主要目的是演示如何使用 libswscale 库进行视频缩放。它通过调用 libswscale 库的函数 SwsGetContext()SwsScale() 实现了将一系列输入图像转换为指定大小的输出图像的功能。

代码见github/moonfdd/ffmpeg-go库。

命令如下:

go run ./examples/internalexamples/scaling_video/main.go ./out/big_buck_bunny.mp4 640*480

./lib/ffplay -f rawvideo -pix_fmt rgb24 -video_size 640x480 ./out/big_buck_bunny.mp4

golang代码如下:

package main

import (
	"fmt"
	"os"
	"unsafe"

	"github.com/moonfdd/ffmpeg-go/ffcommon"
	"github.com/moonfdd/ffmpeg-go/libavutil"
	"github.com/moonfdd/ffmpeg-go/libswscale"
)

func main0() (ret ffcommon.FInt) {
	var src_data, dst_data [4]*ffcommon.FUint8T
	var src_linesize, dst_linesize [4]ffcommon.FInt
	var src_w ffcommon.FInt = 320
	var src_h ffcommon.FInt = 240
	var dst_w ffcommon.FInt
	var dst_h ffcommon.FInt
	var src_pix_fmt libavutil.AVPixelFormat = libavutil.AV_PIX_FMT_YUV420P
	var dst_pix_fmt libavutil.AVPixelFormat = libavutil.AV_PIX_FMT_RGB24
	var dst_size string
	var dst_filename string
	var dst_file *os.File
	var dst_bufsize ffcommon.FInt
	var sws_ctx *libswscale.SwsContext
	var i ffcommon.FInt

	if len(os.Args) != 3 {
		fmt.Printf("Usage: %s output_file output_size\nAPI example program to show how to scale an image with libswscale.\nThis program generates a series of pictures, rescales them to the given output_size and saves them to an output file named output_file\n.\n", os.Args[0])
		os.Exit(1)
	}
	dst_filename = os.Args[1]
	dst_size = os.Args[2]

	if libavutil.AvParseVideoSize(&dst_w, &dst_h, dst_size) < 0 {
		fmt.Printf("Invalid size '%s', must be in the form WxH or a valid size abbreviation\n",
			dst_size)
		os.Exit(1)
	}

	dst_file, _ = os.Create(dst_filename)
	if dst_file == nil {
		fmt.Printf("Could not open destination file %s\n", dst_filename)
		os.Exit(1)
	}

	/* create scaling context */
	sws_ctx = libswscale.SwsGetContext(src_w, src_h, src_pix_fmt,
		dst_w, dst_h, dst_pix_fmt,
		libswscale.SWS_BILINEAR, nil, nil, nil)
	if sws_ctx == nil {
		fmt.Printf(
			"Impossible to create scale context for the conversion fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
			libavutil.AvGetPixFmtName(src_pix_fmt), src_w, src_h,
			libavutil.AvGetPixFmtName(dst_pix_fmt), dst_w, dst_h)
		ret = -libavutil.EINVAL
		goto end
	}

	/* allocate source and destination image buffers */
	ret = libavutil.AvImageAlloc(&src_data, &src_linesize,
		src_w, src_h, src_pix_fmt, 16)
	if ret < 0 {
		fmt.Printf("Could not allocate source image\n")
		goto end
	}

	/* buffer is going to be written to rawvideo file, no alignment */
	ret = libavutil.AvImageAlloc(&dst_data, &dst_linesize,
		dst_w, dst_h, dst_pix_fmt, 1)
	if ret < 0 {
		fmt.Printf("Could not allocate destination image\n")
		goto end
	}
	dst_bufsize = ret

	for i = 0; i < 100; i++ {
		// /* generate synthetic video */
		fill_yuv_image(&src_data, &src_linesize, src_w, src_h, i)

		/* convert to destination format */
		sws_ctx.SwsScale((**byte)(unsafe.Pointer(&src_data)),
			(*int32)(unsafe.Pointer(&src_linesize)), 0, uint32(src_h), (**byte)(unsafe.Pointer(&dst_data)), (*int32)(unsafe.Pointer(&dst_linesize)))

		// /* write scaled image to file */
		dst_file.Write(ffcommon.ByteSliceFromByteP(dst_data[0], int(dst_bufsize)))
	}

	fmt.Printf("Scaling succeeded. Play the output file with the command:\nffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
		libavutil.AvGetPixFmtName(dst_pix_fmt), dst_w, dst_h, dst_filename)

end:
	dst_file.Close()
	libavutil.AvFreep(uintptr(unsafe.Pointer(&src_data[0])))
	libavutil.AvFreep(uintptr(unsafe.Pointer(&dst_data[0])))
	sws_ctx.SwsFreeContext()
	if ret < 0 {
		return 1
	} else {
		return 0
	}
}

func fill_yuv_image(data *[4]*ffcommon.FUint8T, linesize *[4]ffcommon.FInt, width, height, frame_index ffcommon.FInt) {
	var x, y ffcommon.FInt

	/* Y */
	for y = 0; y < height; y++ {
		for x = 0; x < width; x++ {
			//data[0][y*linesize[0]+x] = x + y + frame_index*3
			*(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(data[0])) + uintptr(y*linesize[0]+x))) = byte((x + y + frame_index*3) % 256)
		}
	}

	/* Cb and Cr */
	for y = 0; y < height/2; y++ {
		for x = 0; x < width/2; x++ {
			// data[1][y * linesize[1] + x] = 128 + y + frame_index * 2;
			// data[2][y * linesize[2] + x] = 64 + x + frame_index * 5;
			*(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(data[1])) + uintptr(y*linesize[1]+x))) = byte((128 + y + frame_index*2) % 256)
			*(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(data[2])) + uintptr(y*linesize[2]+x))) = byte((64 + x + frame_index*5) % 256)
		}
	}
}

func main() {

	os.Setenv("Path", os.Getenv("Path")+";./lib")
	ffcommon.SetAvutilPath("./lib/avutil-56.dll")
	ffcommon.SetAvcodecPath("./lib/avcodec-58.dll")
	ffcommon.SetAvdevicePath("./lib/avdevice-58.dll")
	ffcommon.SetAvfilterPath("./lib/avfilter-56.dll")
	ffcommon.SetAvformatPath("./lib/avformat-58.dll")
	ffcommon.SetAvpostprocPath("./lib/postproc-55.dll")
	ffcommon.SetAvswresamplePath("./lib/swresample-3.dll")
	ffcommon.SetAvswscalePath("./lib/swscale-5.dll")

	genDir := "./out"
	_, err := os.Stat(genDir)
	if err != nil {
		if os.IsNotExist(err) {
			os.Mkdir(genDir, 0777) //  Everyone can read write and execute
		}
	}

	main0()
}

在这里插入图片描述


http://www.niftyadmin.cn/n/288352.html

相关文章

[笔记]Python计算机视觉编程《一》 基本的图像操作和处理

文章目录 前言环境搭建 计算机视觉简介Python和NumPy第一章 基本的图像操作和处理1.1 PIL&#xff1a;Python图像处理类库1.1.1 转换图像格式1.1.2 创建缩略图1.1.3 复制和粘贴图像区域1.1.4 调整尺寸和旋转 1.2 Matplotlib1.2.1 绘制图像、点和线 前言 今天&#xff0c;图…

Linux——理解文件系统和动静态库

一、理解文件系统 使用命令查看信息 1&#xff0c;使用ls -l查看文件属性和文件内容 2&#xff0c;stat文件名查看更多信息 3&#xff0c;inode Linux中的文件分为文件属性和文件内容。文件属性又称为元信息。保存在inode结构中&#xff0c;inode是一个文件属性的集合。一个文…

如何在Windows上搭建NFS服务器实现开发板与Windows之间的文件共享

目录 1 安装nfs.exe 2 mounting 172.31.8.183:/f/nfs on /mnt/nfs failed: No such file or directory 3 mounting 172.31.8.183:/d/nfs on /mnt/nfs failed: Permission denied 1 安装nfs.exe 某项目中需要把程序放到Linux开发板中测试&#xff0c;刚开始使用tftp命令下载…

6---N字形变化

将一个给定字符串 s 根据给定的行数 numRows &#xff0c;以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 "PAYPALISHIRING" 行数为 3 时&#xff0c;排列如下&#xff1a; P A H N A P L S I I G Y I R 之后&#xff0c;你的输出需要从左往右逐…

第十四届蓝桥杯Python B组省赛复盘

第十四届蓝桥杯Python B组省赛复盘 文章目录 第十四届蓝桥杯Python B组省赛复盘试题 A: 2023【问题描述】&#xff08;5 分&#xff09;【思路】 试题 B: 硬币兑换【问题描述】【思路】 试题 C: 松散子序列【问题描述】【输入格式】【输出格式】【样例输入】【样例输出】【评测…

【C++】类与对象(2)

【C】类与对象&#xff08;2&#xff09; 作者&#xff1a;爱写代码的刚子 时间&#xff1a;2023.5.4 本篇博客有关构造函数、析构函数、拷贝构造的知识&#xff0c;由于本篇博客可能比较详细&#xff0c;还剩一些内容没介绍&#xff0c;所以我将剩余的知识放在下一篇博客。 目…

使用ControlNet控制Stable-Diffusion出图人物的姿势

概述 在Stable-Diffusion&#xff08;以下简称SD&#xff09;出图中&#xff0c;我们往往需要对出图人物的姿势进行控制&#xff0c;这里我使用一个比较简单上手的方法&#xff0c;通过ControlNet可以很方便地对画面风格&#xff0c;人物姿势进行控制&#xff0c;从而生成更加…

巧用语言模型——让准确率再涨一点点!

还记得在去年&#xff0c;我们曾经发过一篇文章介绍 icefall 中的语言模型使用方法&#xff1a;升点小技巧之—在icefall中巧用语言模型。如今半年过去了&#xff0c;k2 团队又有了一些新进展。今天来给大家做一个小小的总结&#xff0c;再给大家的模型涨涨点&#xff08;又又又…