yuv編碼成h264格式寫成文件

yuv編碼成h264格式寫成文件
(使用ffmpeg 編碼yuv420p編碼成h264格式)

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>#include <libavcodec/avcodec.h>
#include <libavutil/time.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>static int encode(AVCodecContext* enc_ctx, AVFrame* frame, AVPacket* pkt, FILE* outfile)
{int ret;//發送一幀進行編碼ret = avcodec_send_frame(enc_ctx, frame);if(ret < 0){fprintf(stderr, "avcodec_send_frame() failed!\n");return -1;}while (ret >= 0){//獲取編碼后的數據ret = avcodec_receive_packet(enc_ctx, pkt);if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF){return 0;}else if(ret < 0){fprintf(stderr, "avcodec_receive_packet() failed!\n");return -1;}//寫入文件fwrite(pkt->data, 1, pkt->size, outfile);}}int main()
{printf("Hello video encoder!\n");char* in_yuv_file = "test_yuv420p_1280x720.yuv";char* out_h264_file = "test_420p_1280x720.h264";FILE* infile = NULL;FILE* outfile = NULL;const char* codec_name = "libx264";const AVCodec* codec = NULL;AVCodecContext* codec_ctx = NULL;AVFrame* frame = NULL;AVPacket* pkt = NULL;int ret = 0;//查找指定的編碼器codec = avcodec_find_encoder_by_name(codec_name);if(!codec){fprintf(stderr, "avcodec_find_encoder_by_name() failed!\n");return 0;}//分配編碼器上下文codec_ctx = avcodec_alloc_context3(codec);if(!codec_ctx){fprintf(stderr, "avcodec_alloc_context3() failed!\n");return 0;}//設置分辨率codec_ctx->width = 1280;codec_ctx->height = 720;//設置time_baseAVRational time_base = {1, 25};AVRational framerate = {25, 1};codec_ctx->time_base = time_base;codec_ctx->framerate = framerate;//設置I幀間隔(GOP size)codec_ctx->gop_size = 25;//planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)//YYYY....UU....VV....codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;//設置一些參數//這些參數可能會相互影響的,preset設置就有可能會影響到profileif(codec->id == AV_CODEC_ID_H264){//h264的參數// baseline profile:基本畫質。支持I/P 幀,只支持無交錯(Progressive)和CAVLC;//extended profile:進階畫質。支持I/P/B/SP/SI 幀,只支持無交錯(Progressive)和CAVLC;//main profile:主流畫質。提供I/P/B 幀,支持無交錯(Progressive)和交錯(Interlaced),也支持//CAVLC 和CABAC 的支持;//high profile:高級畫質。在main Profile 的基礎上增加了8x8內部預測、自定義量化、 無損視頻編碼//和更多的YUV 格式;ret = av_opt_set(codec_ctx->priv_data, "profile", "main", 0);if(ret != 0){sprintf(stderr,"av_opt_set() profile = main failed!\n");}//x264編碼器下的參數//編碼速度和壓縮率之間做出1個權衡//ultrafast//superfast//veryfast//faster//fast//medium – default preset//slow//slower//veryslow//placeboret = av_opt_set(codec_ctx->priv_data, "preset", "medium", 0);if(ret != 0){sprintf(stderr, "av_opt_set() preset = medium failed!\n");}//x264編碼器下的參數//film:電影類型,對視頻的質量非常嚴格時使用該選項//animation:動畫片,壓縮的視頻是動畫片時使用該選項//grain:顆粒物很重,該選項適用于顆粒感很重的視頻//stillimage:靜態圖像,該選項主要用于靜止畫面比較多的視頻//psnr:提高psnr,該選項編碼出來的視頻psnr比較高//ssim:提高ssim,該選項編碼出來的視頻ssim比較高//fastdecode:快速解碼,該選項有利于快速解碼//zerolatency:零延遲,該選項主要用于視頻直播ret = av_opt_set(codec_ctx->priv_data, "tune", "zerolatency", 0);if(ret != 0){sprintf(stderr, "av_opt_set() tune = zerolatency failed!\n");}}//碼率codec_ctx->bit_rate = 3000000;//將codec_ctx 和codec關聯ret = avcodec_open2(codec_ctx, codec, NULL);if(ret < 0){fprintf(stderr, "avcodec_open2() failed!\n");return 0;}//打開輸入文件 和 輸出文件infile = fopen(in_yuv_file, "rb");if(!infile){fprintf(stderr, "fopen() in_yuv_file failed!\n");return 0;}outfile = fopen(out_h264_file, "wb");if(!outfile){fprintf(stderr, "fopen() out_h264_file failed!\n");return 0;}//分配AVPacketpkt = av_packet_alloc();if(!pkt){fprintf(stderr, "av_packet_alloc() failed!\n");return 0;}//分配AVFrameframe = av_frame_alloc();if(!frame){fprintf(stderr, "av_frame_alloc() failed!\n");return 0;}frame->width = codec_ctx->width;frame->height = codec_ctx->height;frame->format = codec_ctx->pix_fmt;//計算出一幀數據的大小 像素格式 * 寬 * 高int frame_bytes = av_image_get_buffer_size(frame->format, frame->width,frame->height, 1);printf("frame_bytes = %d\n", frame_bytes);uint8_t* yuv_buf = (uint8_t*)malloc(frame_bytes);if(!yuv_buf){printf("yuv_buf malloc() failed!\n");return 0;}int64_t pts = 0;while (1){//從文件讀一幀數據memset(yuv_buf, 0, frame_bytes);size_t read_bytes = fread(yuv_buf, 1, frame_bytes, infile);if(read_bytes <= 0){fprintf(stderr, "fread end!\n");break;}//根據設置的參數將yuv數據填充到frame->data , frame->linesizeint fill_size = av_image_fill_arrays(frame->data, frame->linesize, yuv_buf,frame->format, frame->width, frame->height, 1);if(fill_size != frame_bytes){fprintf(stderr, "av_image_fill_arrays failed!\n");break;}pts += 40;//設置ptsframe->pts = pts;ret = encode(codec_ctx, frame, pkt, outfile);if(ret < 0){fprintf(stderr, "encode failed!\n");break;}}//沖刷編碼器encode(codec_ctx, NULL, pkt, outfile);fclose(infile);fclose(outfile);if(yuv_buf){free(yuv_buf);}av_frame_free(&frame);av_packet_free(&pkt);avcodec_free_context(&codec_ctx);printf("video encoder end!\n");return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379089.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379089.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379089.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c++ stl隊列初始化_聲明,初始化和訪問向量| C ++ STL

c stl隊列初始化Here, we have to declare, initialize and access a vector in C STL. 在這里&#xff0c;我們必須聲明&#xff0c;初始化和訪問C STL中的向量。 向量聲明 (Vector declaration) Syntax: 句法&#xff1a; vector<data_type> vector_name;Since, vec…

ADO.NET與SQL Server數據庫的交互

7.3.1 使用SqlConnection對象連接數據庫 例如&#xff1a;建立與SQL Server數據庫的連接。 string connstring"Data Sourceservername;uidusername;pwdpassword;Initial Catalogdbname";SqlConnection connnew SqlConnection(connstring);conn.Open(); 例如&#xf…

nsis 修改exe執行權限

通過修改注冊表的方式&#xff0c;修改exe的執行權限。&#xff0c;以下例子是使用管理員運行。 ;添加admin權限 SectionWriteRegStr HKCU "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" "$INSTDIR\spp.exe" "RUNASADMIN&qu…

linux ftp日志_linux學習筆記(一)——Linux分區和目錄結構

linux學習筆記&#xff08;一&#xff09;——Linux分區和目錄結構安裝Linux時&#xff0c;手動掛載分區的情況下&#xff0c;/ 和 swap 是必須要掛載的&#xff0c;其他/home、/boot 等可以根據需要自行掛載。一般來說&#xff0c;簡單的話&#xff0c;建議掛載三個分區&#…

C#通過VS連接MySQL數據庫實現增刪改查基本操作

創建一個數據庫wsq 里面有一張beyondyanyu表 表里面有id(int)、names(varchar)、count(int)、passwords(varchar) 數據可以自己添 1、導入MySQL引用&#xff0c;你需要從官網或者其他地方下載&#xff0c;私聊我也可以 using MySql.Data.MySqlClient; 2、創建MySqlConnection對…

使用ffmpeg的filter處理yuv數據包括split filter(分流)、crop filter(裁剪)、vflip filter(垂直向上的翻轉)、overlay filter(合成)

使用ffmpeg的filter處理yuv數據包括split filter(分流)、crop filter(裁剪)、vflip filter(垂直向上的翻轉)、overlay filter(合成) #include <stdio.h>#include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavfilter/avfil…

vc++ 6.0 堆棧_在C ++中使用鏈接列表實現堆棧

vc 6.0 堆棧To implement a stack using a linked list, basically we need to implement the push() and pop() operations of a stack using linked list. 要使用鏈接列表實現堆棧 &#xff0c;基本上&#xff0c;我們需要使用鏈接列表實現堆棧的push()和pop()操作。 Exampl…

煙雨小書店

煙雨小書店演示視頻 源碼

協議地址結構_TCP/IP 協議 講解

計算機網絡體系結構分層太厲害了&#xff0c;終于有人能把TCP/IP 協議講的明明白白了計算機網絡體系結構分層不難看出&#xff0c;TCP/IP 與 OSI 在分層模塊上稍有區別。OSI 參考模型注重“通信協議必要的功能是什么”&#xff0c;而 TCP/IP 則更強調“在計算機上實現協議應該開…

ffmpeg進行混音,將兩路音頻pcm數據合成一路輸出

ffmpeg進行混音&#xff0c;將兩路音頻pcm數據合成一路輸出 audiomixer.h #ifndef AUDIOMIXER_H #define AUDIOMIXER_H#include <map> #include <mutex> #include <cstdio> #include <cstdint> #include <string> #include <memory>exter…

python sep函數_Python中帶有print()函數的sep參數

python sep函數sep parameter stands for separator, it uses with the print() function to specify the separator between the arguments. sep參數代表分隔符&#xff0c;它與print()函數一起使用以指定參數之間的分隔符。 The default value is space i.e. if we dont us…

關于 MySQL 主從復制的配置(轉)

來源&#xff1a;http://www.oschina.net/bbs/thread/10388設置Mysql的主從設置很重要&#xff0c;有如下幾點用處&#xff1a;1 做備份機器&#xff0c;一旦主服務器崩潰&#xff0c;可以直接啟用從服務器作為主服務器2 可以直接鎖定從服務器的表只讀&#xff0c;然后做備份數…

Silverlight 同域WCF免跨域文件

在sl3使用wcf時常常會因為sl中調用了不同域的wcf服務而導至調用服務失敗&#xff0c;記得在很久以前sl當是只支持同域的訪問&#xff0c;那么讓我有一個想法&#xff0c;就是在sl引用時可以動態地取得當前sl所在的域&#xff0c;而wcf服務也必須同時部署到這個域下邊&#xff0…

使用ffmpeg 的 filter 給圖片添加水印

使用ffmpeg 的 filter 給圖片添加水印。 main.c #include <stdio.h>#include <libavfilter/avfilter.h> #include <libavfilter/buffersrc.h> #include <libavfilter/buffersink.h> #include <libavformat/avformat.h> #include <libavcodec…

程序崩潰 分析工具_程序分析工具| 軟件工程

程序崩潰 分析工具A program analysis tool implies an automatic tool that takes the source code or the executable code of a program as information and produces reports with respect to a few significant attributes of the program, for example, its size, multif…

28335接兩個spi設備_IIC和SPI如此流行,誰才是嵌入式工程師的必備工具?

IICvs SPI現今&#xff0c;在低端數字通信應用領域&#xff0c;我們隨處可見 IIC (Inter-Integrated Circuit) 和 SPI (Serial Peripheral Interface)的身影。原因是這兩種通信協議非常適合近距離低速芯片間通信。Philips(for IIC)和 Motorola(for SPI) 出于不同背景和市場需求…

線性表15|魔術師發牌問題和拉丁方陣 - 數據結構和算法20

線性表15 : 魔術師發牌問題和拉丁方陣 讓編程改變世界 Change the world by program 題外話 今天小甲魚看到到微博有朋友在問&#xff0c;這個《數據結構和算法》系列課程有木有JAVA版本的&#xff1f; 因為這個問題之前也有一些朋友問過&#xff0c;所以咱在這里統一說下哈…

[ZT]Three ways to tell if a .NET Assembly is Strongly Named (or has Strong Name)

Here are several convenient ways to tell whether a .NET assembly is strongly named. (English language note: I assume the form “strongly named” is preferred over “strong named” since that’s the form used in the output of the sn.exe tool shown immediat…