转码例子
- 格式:pdf
- 大小:140.28 KB
- 文档页数:12
java转码方法一、Java转码概述Java转码是指将一个字符集编码转换为另一个字符集编码的过程。
在实际开发中,我们经常需要进行字符集编码的转换,例如将UTF-8编码的字符串转换为GBK编码的字符串。
Java提供了多种方法来进行字符集编码的转换,本文将详细介绍Java中常用的几种转码方法。
二、Java字符集在介绍Java中的转码方法之前,我们先来了解一下Java中常用的字符集。
在Java中,每个字符都有一个对应的Unicode编码。
Unicode是一种国际标准字符集,它包含了世界上所有语言所使用的字符。
但是,在实际开发中,我们通常使用其他更加常用的字符集来表示字符串。
1. ASCIIASCII是一种最早出现的字符集,它只包含英文字母、数字和少量特殊符号(如空格、制表符等),共计128个字符。
由于其简单、易于处理,因此在计算机系统中得到广泛应用。
2. ISO-8859-1ISO-8859-1是一种西欧语言所使用的字符集,它包含了所有西欧语言所使用的字母、数字和符号。
ISO-8859-1共计256个字符。
3. GBKGBK是中国国家标准GB 2312扩展出来的全能字符集,它包含了简体中文、繁体中文、日文、韩文等多种语言所使用的字符。
GBK共计21886个字符。
4. UTF-8UTF-8是一种可变长度的Unicode编码,它可以表示世界上所有的字符。
UTF-8共计1114112个字符。
三、Java转码方法在Java中,常用的转码方法有以下几种:1. String.getBytes()String.getBytes()方法可以将一个字符串按照指定的字符集编码转换为字节数组。
例如,将一个UTF-8编码的字符串转换为GBK编码的字节数组:```String str = "你好";byte[] bytes = str.getBytes("GBK");```2. new String(byte[] bytes, Charset charset)new String(byte[] bytes, Charset charset)方法可以将一个字节数组按照指定的字符集解码成字符串。
数据转换的例子以下是一些常见的数据转换的例子:1. 字符串转整数:将一个表示数字的字符串转换为整数数据类型。
例如,将字符串"123"转换为整数123。
2. 整数转字符串:将一个整数转换为字符串数据类型。
例如,将整数123转换为字符串"123"。
3. 字符串转浮点数:将一个表示浮点数的字符串转换为浮点数数据类型。
例如,将字符串"3.14"转换为浮点数3.14。
4. 浮点数转字符串:将一个浮点数转换为字符串数据类型。
例如,将浮点数3.14转换为字符串"3.14"。
5. 整数转布尔值:将一个整数转换为布尔值数据类型。
通常,非零整数会转换为True,而零整数会转换为False。
6. 布尔值转整数:将一个布尔值转换为整数数据类型。
通常,True会转换为1,False会转换为0。
7. 字符串转日期:将一个表示日期的字符串转换为日期数据类型。
例如,将字符串"2022-01-01"转换为日期数据类型。
8. 日期转字符串:将一个日期转换为字符串数据类型。
例如,将日期数据类型的2022-01-01转换为字符串"2022-01-01"。
9. 列表转字符串:将一个包含多个元素的列表转换为字符串数据类型。
例如,将列表[1, 2, 3]转换为字符串"[1, 2, 3]"。
10. 字符串转列表:将一个包含多个元素的字符串转换为列表数据类型。
例如,将字符串"[1, 2, 3]"转换为列表[1, 2, 3]。
这些都是常见的数据转换的例子,涵盖了不同的数据类型之间的转换。
具体的转换方法会根据编程语言而有所不同。
利用shell脚本进行数据格式转换和转码Shell脚本作为一种命令语言,可用于在Unix或类Unix系统上编写脚本。
通过编写Shell脚本,我们可以利用其中的命令和函数实现数据格式转换和转码的功能。
本文将介绍如何使用Shell脚本进行数据格式转换和转码的方法和实例。
一、Shell脚本简介Shell脚本是一种以文本文件形式保存的纯文本文件,其中包含了一系列Shell命令。
通过执行Shell脚本,可以将其中的命令按照顺序逐行执行,从而实现一系列功能。
二、数据格式转换在数据处理过程中,有时我们会遇到需要将数据从一种格式转换为另一种格式的情况。
使用Shell脚本可以方便地实现数据格式转换。
例如,我们有一个以逗号分隔的文本文件data.csv,其中的数据如下:```Name,Age,GenderTom,25,MaleMary,30,FemaleJohn,35,Male```现在我们需要将其转换为以Tab键分隔的格式。
可以编写一个Shell 脚本来实现这个功能,脚本内容如下:```shell#!/bin/bash# 读取每一行数据while IFS=',' read -r name age genderdo# 输出转换后的格式echo -e "$name\t$age\t$gender"done < data.csv```通过执行上述脚本,可以将data.csv中的数据转换为以Tab键分隔的格式:```Name Age GenderTom 25 MaleMary 30 FemaleJohn 35 Male```三、数据转码有时候,我们需要将数据从一种字符编码转换为另一种字符编码。
使用Shell脚本可以方便地实现数据转码。
举个例子,我们有一个以UTF-8编码的文本文件data.txt,其中包含了一些中文字符。
现在我们需要将其转换为GBK编码。
可以编写一个Shell脚本来实现这个功能,脚本内容如下:```shell#!/bin/bashiconv -f UTF-8 -t GBK data.txt > converted.txt```通过执行上述脚本,可以将data.txt的内容从UTF-8编码转换为GBK编码,并保存在converted.txt中。
1.错位—畸变—转码三个术语的由来“错位”这个词,在中文学界太常用了,但是意义极为广泛,“错位”大致意思是“错误位置”或“错开位置”。
可以是指任何用错地方,医学上关节错位、脊椎骨错位,地质上断层错位,文化上我们经常见到“传统价值错位”、“学术道德错位”、“性别角色错位”、“屌丝男与白富美的错位婚姻”,大致上指判断错误的做法,不应当处于某地位的人物,等等,看来大部分的错位真的是“错”,虽然本文将讨论比较中性的错位。
积极的错位也有可能出现,虽然不是很普遍。
例如在经济学中有所谓“错位竞争”,在地方发展中则有“错位发展”,在商品经营上有所谓“错位营销”,都有些机会主义的策略味道,大致上指的是寻找别人留下的空挡求发展生存。
消极的错位,有可能产生现有文化规范无法处理的恶性后果,可以称为意义活动中的“畸变”(mutation)。
畸变是一个文化,或一个生存系统不想见到的,只能设法避开或消除的,不让它延续或蔓延,但是如果错位是积极的,产生的后果有可能打开新的局面,成为文化演变中的转折,此事我们称为“转码”(transcoding)。
错位此词西文无对译,只能说西文无此概念譹訛。
而“转码”一词,情况几乎翻了过来,英文transcoding词义丰富,在电子技术上中文此词意义比较明白,在这个电子技术更新太快的时代,专门指电子文档从一种编码转到另一种编码,例如从模拟式录像转为数字式录像。
比喻式地使用这个意义,“转码”有时也使用在文艺学中,从一个体裁的文本转入另一个体裁,例如从小说“转码”为电影譺訛,有时候也说音乐家读乐谱时转码为内心声音。
譻訛而本文目的在于把“错位”“畸变”“转码”这三个概念应用到文化研究中。
这与前面说的各种用法相比,抽象得多,意义也宽泛得多。
既然是使用了三个其他学科比较随意使用的旧术语,本文就必须加以重新定义,免得与日常的用法混淆。
本文说的“错位”,指的是文本(或文本的某个成分),无法用文化中对该类型的文本已有的符码来做完整解释,此文化所承认的元语言,已经对此文本不(完全)适用。
upc转码示例
UPC码是一种条形码编码标准,由美国统一代码委员会制定。
以下是UPC码转码的示例:
假设我们要将一个12位的UPC码转换为EAN码,可以使用以下步骤:
将UPC码的前11位按照规定的格式转换成数字。
将转换后的数字加上一个校验位。
将转换后的数字组成一个13位的数字串。
将数字串按照规定的格式转换成EAN码的条形码。
具体来说,将UPC码的前11位按照以下规则转换成数字:
0-9的数字保持不变。
A-Z的字母转换为10-35的数字。
每个数字或字母按照从左到右的顺序转换成对应的数字。
例如,将UPC码"036006746859"转换为EAN码,步骤如下:
将UPC码的前11位"0360067468"按照规则转换成数字,得到"360067468"。
计算校验位,取前11位数字的权重分别为3、7、1、3、7、1、3、7、1、3,计算校验位的值:
(33+77+11+33+77+11+33+77+11+33)/10=258,取余数得到校验位为4。
将转换后的数字加上校验位,得到"3600674684"。
将数字串"3600674684"按照规定的格式转换成EAN码的条形码。
需要注意的是,在实际应用中,还需要对转换后的EAN码进行校验和格式检查,以确保其符合标准和规范。
stata中变量转码变量转码是数据分析中常用的一种技术,它能够将原始数据中的某一变量转换成符合研究需求的新变量。
在Stata中,我们可以通过一系列命令和函数来实现变量转码的操作,本文将介绍Stata中几种常见的变量转码方法及其应用场景。
一、将数值型变量转换为分类变量数值型变量是指取值为连续数字的变量,而分类变量则是取值为离散类别的变量。
在实际研究中,我们经常需要将数值型变量转换为分类变量,以便进行更深入的分析。
在Stata中,可以使用“recode”命令来实现这一转换。
举个例子,假设我们有一个数值型变量“age”,表示被调查者的年龄。
我们希望将年龄分为三个类别:“青年”、“中年”和“老年”。
可以使用以下命令实现转码:```recode age (min/30=1) (30/60=2) (60/max=3), generate(age_category)```上述命令中,“age”表示原始变量,“age_category”表示转码后的分类变量。
命令中的“(min/30=1)”表示将原始变量中取值在最小值到30之间的观察值转换为1,“(30/60=2)”表示将取值在30到60之间的观察值转换为2,“(60/max=3)”表示将取值在60到最大值之间的观察值转换为3。
二、将字符串型变量转换为数值型变量字符串型变量是指取值为字符或文本的变量,而数值型变量则是取值为连续数字的变量。
在某些情况下,我们需要将字符串型变量转换为数值型变量,以方便后续的数值计算和分析。
在Stata中,可以使用“encode”命令来实现这一转换。
举个例子,假设我们有一个字符串型变量“education”,表示被调查者的教育水平,包括“小学”、“初中”、“高中”、“大学本科”和“研究生”五个类别。
我们希望将其转换为数值型变量,便于计算教育水平的平均值。
可以使用以下命令实现转码:```encode education, generate(education_code)```上述命令中,“education”表示原始变量,“education_code”表示转码后的数值型变量。
lua 转码方法在 Lua 中,字符串可以使用不同的字符编码进行表示。
常见的字符编码包括 ASCII、UTF-8、UTF-16 等。
如果你需要将一个字符串从一个编码转换为另一个编码,可以使用 Lua 的 `string` 库中的 `byte` 和 `char` 函数来实现。
下面是一个示例代码,演示如何将一个 UTF-8 编码的字符串转换为 UTF-16 编码的字符串:```luafunction utf8_to_utf16(str)local len = (str)local buf = {}for i = 1, len dolocal c = (str, i)if c >= 0x80 thenlocal b2 = (str, i + 1) or 0local c2 = (b2 - 192) / 6 + 1921(buf, (c2))i = i + 1else(buf, (c))endendreturn (buf)end```在上面的代码中,我们首先使用 `` 函数获取字符串的长度。
然后,我们使用一个循环遍历字符串中的每个字符。
对于每个字符,我们使用 `` 函数获取其字节值,并根据字节值判断该字符的编码。
如果字节值大于等于 0x80,则表示该字符是一个多字节字符,我们需要获取其后续的字节值,并计算出正确的 Unicode 值。
然后,我们使用 `` 函数将 Unicode 值转换为对应的字符,并将其添加到一个缓冲区中。
最后,我们使用 `` 函数将缓冲区中的所有字符连接起来,得到最终的转换结果。
请注意,上面的代码只是一个示例,可能不适用于所有情况。
在实际应用中,你可能需要根据具体情况进行修改和调整。
汉字字符编码与转码详解,iso-8859-1的妙⽤程序中有汉字参数,经常会遇到编码转码问题,总结下:1.汉字为多字节字符,须多字节编码解码,如"测试".getBytes("GBK");这样"测试".getBytes("GBK")就变成⼀个byte数组,这时候你可以随意重新指定编码如iso-8859-1,String s1=new String("测试".getBytes("GBK"),"iso-8859-1");编为s1,这是s1就变成⼀个是iso-8859-1编码的字符串,如果你想重新转为中⽂,那么,你⽤什么字符集编码的,必须⽤什么字符集来解码,这⾥是iso-8859-1,可以这么来做String s2 = new String(s1.getBytes("ISO-8859-1"),"GBK");这样s2⼜重新变回中⽂了,所以当你打印s2时,就是“测试”。
2.⽤iso-8859-1做中间编码,注意不是开始编码和编回的编码(开始和编回的可⽤GBK或者UTF8),只做中间编码,原因:[1]iso-8859-1是单字节字符编码,[2]ANSI 编码 (如:GB2312, GBK(gbk包括了gb2312),BIG5,Shift_JIS,ISO-8859-2等等),是多字节编码(英⽂单字节,中⽂多字节),不是定长编码;[3]UNICODE ,UTF-8, UTF-7, UTF-16, UnicodeLittle, UnicodeBig,是宽字节编码(所有字符均是多字节)因此⽤iso-8859-1做中间码,会保持原有字节的秩序,不发⽣混乱;可以理解为其他的编码对iso-8859-1兼容吧。
因此,我们常常使⽤iso-8859-1做中间码来进⾏逆向操作,得到原始的“字节串”。
字符串转码UTF8转码为GBK的C语⾔程序源代码GBK和UTF8之间的转换可以使⽤两个API,⽅法是先把它们转换为中间编码Unicode,再转换为对应的编码即可。
#include <stdio.h>#include <windows.h>//GBK编码转换到UTF8编码int GBKToUTF8(unsigned char * lpGBKStr,unsigned char * lpUTF8Str,int nUTF8StrLen){wchar_t * lpUnicodeStr = NULL;int nRetLen = 0;if(!lpGBKStr) //如果GBK字符串为NULL则出错退出return 0;nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,NULL,NULL); //获取转换到Unicode编码后所需要的字符空间长度lpUnicodeStr = new WCHAR[nRetLen + 1]; //为Unicode字符串空间nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,lpUnicodeStr,nRetLen); //转换到Unicode编码if(!nRetLen) //转换失败则出错退出return 0;nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,NULL,0,NULL,NULL); //获取转换到UTF8编码后所需要的字符空间长度if(!lpUTF8Str) //输出缓冲区为空则返回转换后需要的空间⼤⼩{if(lpUnicodeStr)delete []lpUnicodeStr;return nRetLen;}if(nUTF8StrLen < nRetLen) //如果输出缓冲区长度不够则退出{if(lpUnicodeStr)delete []lpUnicodeStr;return 0;}nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,(char *)lpUTF8Str,nUTF8StrLen,NULL,NULL); //转换到UTF8编码return nRetLen;}// UTF8编码转换到GBK编码int UTF8ToGBK(unsigned char * lpUTF8Str,unsigned char * lpGBKStr,int nGBKStrLen){wchar_t * lpUnicodeStr = NULL;int nRetLen = 0;if(!lpUTF8Str) //如果UTF8字符串为NULL则出错退出return 0;nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,NULL,NULL); //获取转换到Unicode编码后所需要的字符空间长度lpUnicodeStr = new WCHAR[nRetLen + 1]; //为Unicode字符串空间nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,lpUnicodeStr,nRetLen); //转换到Unicode编码if(!nRetLen) //转换失败则出错退出return 0;nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,NULL,NULL,NULL,NULL); //获取转换到GBK编码后所需要的字符空间长度if(!lpGBKStr) //输出缓冲区为空则返回转换后需要的空间⼤⼩{if(lpUnicodeStr)delete []lpUnicodeStr;return nRetLen;}if(nGBKStrLen < nRetLen) //如果输出缓冲区长度不够则退出{if(lpUnicodeStr)delete []lpUnicodeStr;return 0;}nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,(char *)lpGBKStr,nRetLen,NULL,NULL); //转换到GBK编码return nRetLen;}//使⽤这两个函数的例⼦int main(){char cGBKStr[] = "我是中国⼈!";char * lpGBKStr = NULL;char * lpUTF8Str = NULL;FILE * fp = NULL;int nRetLen = 0;nRetLen = GBKToUTF8((unsigned char *)cGBKStr,NULL,NULL);printf("转换后的字符串需要的空间长度为:%d ",nRetLen);lpUTF8Str = new char[nRetLen + 1];nRetLen = GBKToUTF8((unsigned char *)cGBKStr,(unsigned char *)lpUTF8Str,nRetLen);if(nRetLen){printf("GBKToUTF8转换成功!");}else{printf("GBKToUTF8转换失败!");goto Ret0;}fp = fopen("C:\GBK转UTF8.txt","wb"); //保存到⽂本⽂件fwrite(lpUTF8Str,nRetLen,1,fp);fclose(fp);getchar(); //先去打开那个⽂本⽂件看看,单击记事本的“⽂件”-“另存为”菜单,在对话框中看到编码框变为了“UTF-8”说明转换成功了nRetLen = UTF8ToGBK((unsigned char *)lpUTF8Str,NULL,NULL); //再转回来printf("转换后的字符串需要的空间长度为:%d ",nRetLen);lpGBKStr = new char[nRetLen + 1];nRetLen = UTF8ToGBK((unsigned char *)lpUTF8Str,(unsigned char *)lpGBKStr,nRetLen);if(nRetLen){printf("UTF8ToGBK转换成功! ");}else{printf("UTF8ToGBK转换失败! ");goto Ret0;}fp = fopen("C:\UTF8转GBK.txt","wb"); //保存到⽂本⽂件fwrite(lpGBKStr,nRetLen,1,fp);fclose(fp);getchar(); //再去打开⽂本⽂件看看,发现编码框⼜变为了“ANSI”说明转换成功了Ret0:if(lpGBKStr)delete []lpGBKStr;if(lpUTF8Str)delete []lpUTF8Str;return 0;}。
« Groups HomePlease check the following FFmpeg code:/* now encode as many frames as possible */if (enc->frame_size > 1) {/* output resampled raw samples */av_fifo_realloc(&ost->fifo, av_fifo_size(&ost->fifo) +size_out + 1);av_fifo_write(&ost->fifo, buftmp, size_out);frame_bytes =3D enc->frame_size * 2 * enc->channels;while (av_fifo_read(&ost->fifo, audio_buf, frame_bytes) =3D=3D 0) {AVPacket pkt;av_init_packet(&pkt);//FIXME pass ost->sync_opts as AVFrame.pts inavcodec_encode_audio()ret =3D avcodec_encode_audio(enc, audio_out, audio_out_size, (short *)audio_buf);audio_size +=3D ret;pkt.stream_index=3D ost->index;pkt.data=3D audio_out;pkt.size=3D ret;if(enc->coded_frame && enc->coded_frame->pts !=3DAV_NOPTS_VALUE)pkt.pts=3D av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);pkt.flags |=3D PKT_FLAG_KEY;write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);ost->sync_opts +=3D enc->frame_size;}}On Apr 23, 6:36 pm, Flame <flameh...@> wrote:=CA=D3=C6=B5=B6=BC=D7=AA=A3=A9=B5=C4=D4=B4=C2=EB=A3=BF> =D0=BB=D0=BB=A3=AC=CE=D2=CF=EB=B2=CE=BF=BC=D2=BB=CF=C2=A3=AC=CE=D2=D4=F5= =C3=B4=D0=B4=B6=BC=B2=BB=B6=D4=A3=AC=D6=BB=C4=DC=D7=AA=CA=D3=C6=B5=A3=AC=BA= =C3=B2=BB=C8=DD=D2=D7=B0=C9=D2=F4=C6=B5=A3=AC=BC=D3=BD=F8=C8=A5=C1=CB=A3=AC= =BD=E1=B9=FB=D7=AA=B3=F6=C0=B4=B2=BB=C4=DC=B2=A5=B7=C5=A3=A1>> =CE=D2=B5=C4=A3=BA> //=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D>> #include <time.h>> #include <stdio.h>> #include <stdlib.h>> #include <string.h>> #include <math.h>>> #ifdef __cplusplus> extern "C"> {> #endif> #include "common.h"> #include "avcodec.h"> #include "avformat.h">> #ifdef __cplusplus}>> #endif>> #define M_PI 3.14159265358979323846>> #ifdef HAVE_AV_CONFIG_H> #undef HAVE_AV_CONFIG_H> #endif>> #define INBUF_SIZE 4096>> int main(int argc, char **argv)> {> char *filename =3D argv[1];//"f:\\test.flv";>> AVFormatContext *pFormatCtxDec,*pFormatCtxEnc;> AVCodecContext> *pCodecCtxDec,*pCodecCtxDec_audio,*pCodecCtxEnc,*pCodecCtxEnc_audio;> AVCodec *pCodecDec,*pCodecDec_audio,*pCodecEnc,*pCodecEnc_audio;> AVFrame *pFrameDec,*pFrameDec_audio,*pFrameEnc,*pFrameEnc_audio;> AVOutputFormat *pOutputFormat;> AVStream *video_st,*audio_st;> unsigned int i, videoStream=3D-1,audioStream=3D-1;> int outbuf_size,outbuf_size_audio;> uint8_t *outbuf,*outbuf_audio,*ptr;> AVPacket packet,packet_audio;> int frameFinished,frameFinished_audio,frames=3D0,frames_audio=3D0;> int out_size,out_size_audio;> double audio_pts, video_pts;>> int len;> static short *samples=3DNULL;> static unsigned int samples_size=3D0;> // Register all formats and codecs> av_register_all();>> // Open video file> if(av_open_input_file(&pFormatCtxDec, filename, NULL, 0, NULL)!=3D0)> return ; // Couldn't open file>> // Retrieve stream information> if(av_find_stream_info(pFormatCtxDec)<0)> return ; // Couldn't find stream information>> // Find the first video stream> videoStream=3D-1;> for(i=3D0; i<pFormatCtxDec->nb_streams; i++) {> if(pFormatCtxDec->streams[i]->codec->codec_type=3D=3DCODEC_TYPE_VIDEO) > {> videoStream=3Di;> }> if(pFormatCtxDec->streams[i]->codec->codec_type=3D=3DCODEC_TYPE_AUDIO) > {> }}>> if(videoStream=3D=3D-1)> return ; // Didn't find a video stream>> if(audioStream=3D=3D-1)> {> printf("No Audio!");> return ;> }> else> {> printf("Audio=3D%d",audioStream);> }> // Get a pointer to the codec context for the video stream> pCodecCtxDec=3DpFormatCtxDec->streams[videoStream]->codec;>> pCodecCtxDec_audio=3DpFormatCtxDec->streams[audioStream]->codec;> // Find the decoder for the video stream> pCodecDec=3Davcodec_find_decoder(pCodecCtxDec->codec_id);> if(pCodecDec=3D=3DNULL)> return ; // Codec not found> pCodecDec_audio=3Davcodec_find_decoder(pCodecCtxDec_audio->codec_id); >> if(pCodecDec_audio=3D=3DNULL)> return ; // Codec not found>> // Inform the codec that we can handle truncated bitstreams -- i.e., > // bitstreams where frame boundaries can fall in the middle of packets > //if(pCodecDec->capabilities & CODEC_CAP_TRUNCATED)> // pCodecCtxDec->flags|=3DCODEC_FLAG_TRUNCATED;>> // Open codec> if(avcodec_open(pCodecCtxDec, pCodecDec)<0)> return ; // Could not open codec>> // Open audio codec> if(avcodec_open(pCodecCtxDec_audio, pCodecDec_audio)<0)> return ; // Could not open codec> // Allocate video frame> pFrameDec =3D avcodec_alloc_frame();> if(pFrameDec =3D=3D NULL)> return ;>> // Allocate audio frame> pFrameDec_audio =3D avcodec_alloc_frame();> if(pFrameDec_audio =3D=3D NULL)> return ;>> // auto detect the output format from the name. default is mpeg.> pOutputFormat =3D guess_format(NULL, outfilename, NULL);> if (pOutputFormat =3D=3D NULL)> return;> // allocate the output media context> pFormatCtxEnc =3D av_alloc_format_context();> if (pFormatCtxEnc =3D=3D NULL)> return;>> pFormatCtxEnc->oformat =3D pOutputFormat;> sprintf(pFormatCtxEnc->filename, "%s", outfilename);> video_st =3D av_new_stream(pFormatCtxEnc, 0); // 0 for video> audio_st =3D av_new_stream(pFormatCtxEnc, 1); // 1 for audio>> pCodecCtxEnc =3D video_st->codec;> pCodecCtxEnc_audio =3D avcodec_alloc_context();> pCodecCtxEnc_audio =3D audio_st->codec;>> pCodecCtxEnc->codec_id =3D pOutputFormat->video_codec;> pCodecCtxEnc_audio->codec_id =3D pOutputFormat->audio_codec;>> pCodecCtxEnc->codec_type =3D CODEC_TYPE_VIDEO;> pCodecCtxEnc_audio->codec_type =3D CODEC_TYPE_AUDIO;> // put sample parameters> pCodecCtxEnc->bit_rate =3D 3000000;> pCodecCtxEnc_audio->bit_rate =3D pCodecCtxDec_audio->bit_rate ;> pCodecCtxEnc_audio->sample_rate =3D pCodecCtxDec_audio->sample_rate; > pCodecCtxEnc_audio->channels=3D2;> // resolution must be a multiple of two> pCodecCtxEnc->height =3D pCodecCtxDec->height;> // frames per second> pCodecCtxEnc->time_base.den =3D 25;> pCodecCtxEnc->time_base.num =3D 1;> pCodecCtxEnc->pix_fmt =3D PIX_FMT_YUV420P;> pCodecCtxEnc->gop_size =3D 12; /* emit one intra frame every ten frames> */> if (pCodecCtxEnc->codec_id =3D=3D CODEC_ID_MPEG1VIDEO){> /* needed to avoid using macroblocks in which some coeffs> overflow> this doesnt happen with normal video, it just happens here> as the> motion of the chroma plane doesnt match the luma plane */> pCodecCtxEnc->mb_decision=3D2;> }> // some formats want stream headers to be seperate> if(!strcmp(pFormatCtxEnc->oformat->name, "mp4") || !> strcmp(pFormatCtxEnc->oformat->name, "mov") || !strcmp(pFormatCtxEnc->ofor= mat->name, "3gp"))>> pCodecCtxEnc->flags |=3D CODEC_FLAG_GLOBAL_HEADER;>> // set the output parameters (must be done even if no parameters).> if (av_set_parameters(pFormatCtxEnc, NULL) < 0) {> return;>> }>> // find the video encoder> pCodecEnc =3D avcodec_find_encoder(pCodecCtxEnc->codec_id);> if (pCodecEnc =3D=3D NULL)> return;>> // find the audio encoder> pCodecEnc_audio =3D avcodec_find_encoder(pCodecCtxEnc_audio->codec_id);> if (pCodecEnc_audio =3D=3D NULL)> return;> // open it> if (avcodec_open(pCodecCtxEnc, pCodecEnc) < 0) {>> // open audio> if (avcodec_open(pCodecCtxEnc_audio, pCodecEnc_audio) < 0) {> return;}>> if (!(pFormatCtxEnc->oformat->flags & AVFMT_RAWPICTURE)) {> /* allocate output buffer */> /* XXX: API change will be done */> outbuf_size =3D 500000;> outbuf_size_audio=3D10000;> outbuf =3D av_malloc(outbuf_size);> outbuf_audio=3Dav_malloc(outbuf_size_audio);> }>> pFrameEnc=3D avcodec_alloc_frame();> pFrameEnc_audio=3D avcodec_alloc_frame();> // open the output file, if needed> if (!(pOutputFormat->flags & AVFMT_NOFILE)) {> if (url_fopen(&pFormatCtxEnc->pb, outfilename, URL_WRONLY) < > 0) {> fprintf(stderr, "Could not open '%s'\n", filename);> return;> }> }>> // write the stream header, if any> av_write_header(pFormatCtxEnc);> // Read frames and save frames to disk> while(av_read_frame(pFormatCtxDec, &packet)>=3D0)> {> // Is this a packet from the video stream?> if(packet.stream_index=3D=3DvideoStream)> {> // Decode video frame> avcodec_decode_video(pCodecCtxDec, pFrameDec, &frameFinished,> packet.data, packet.size);>> // Did we get a video frame?> if(frameFinished)> frames++;>> pFrameEnc->data[0] =3D pFrameDec->data[0];> pFrameEnc->data[1] =3D pFrameDec->data[1];> pFrameEnc->data[2] =3D pFrameDec->data[2];> pFrameEnc->linesize[0] =3D pFrameDec->linesize[0];> pFrameEnc->linesize[1] =3D pFrameDec->linesize[1];> pFrameEnc->linesize[2] =3D pFrameDec->linesize[2];> if (pFormatCtxEnc->oformat->flags & AVFMT_RAWPICTURE) {> /* raw video case. The API will change slightly in the near > futur for that */> AVPacket pkt;> av_init_packet(&pkt);>> pkt.flags |=3D PKT_FLAG_KEY;> pkt.stream_index=3D video_st->index;> pkt.data=3D (uint8_t *)pFrameEnc;> pkt.size=3D sizeof(AVPicture);>> av_write_frame(pFormatCtxEnc, &pkt);> } else {> // encode the image> out_size =3D avcodec_encode_video(pCodecCtxEnc, outbuf,> outbuf_size, pFrameEnc);> // if zero size, it means the image was buffered> if (out_size !=3D 0) {> AVPacket pkt;> av_init_packet(&pkt);>> pkt.pts=3D pCodecCtxEnc->coded_frame->pts;> if(pCodecCtxEnc->coded_frame->key_frame)> pkt.flags |=3D PKT_FLAG_KEY;> pkt.stream_index=3D video_st->index;> pkt.data=3D outbuf;> pkt.size=3D out_size;>> // write the compressed frame in the media file> av_write_frame(pFormatCtxEnc, &pkt);> }> }> }> else if(packet.stream_index=3D=3DaudioStream)> {int ret=3D0;> AVPacket pkt;> len=3Dpacket.size;> ptr=3Dpacket.data;>> while(len>0)> {> outbuf_audio=3DNULL;> out_size_audio=3D0;> if(&packet)> samples=3D(short> *)av_fast_realloc(samples,&samples_size,FFMAX(packet.size*sizeof(*samples)= ,-AVCODEC_MAX_AUDIO_FRAME_SIZE));> out_size_audio=3Dsamples_size;>> ret=3Davcodec_decode_audio(pCodecCtxDec_audio,samples,&out_size_audio,ptr,= len-);//> =C8=F4=CE=AA=D2=F4=C6=B5=B0=FC=A3=AC=BD=E2=C2=EB=B8=C3=D2=F4=C6=B5=B0=FC> if(ret<0)> {> printf("while decode audio failure\n");> exit(0);> }> fflush(stdout);> ptr+=3Dret;> len-=3Dret;> if(out_size_audio<=3D0)> continue;> outbuf_audio=3D(uint8_t *)samples;>> av_init_packet(&pkt);> pkt.size=3D avcodec_encode_audio(pCodecCtxEnc_audio,> outbuf_audio, out_size_audio, (short int*)outbuf_audio);> pkt.pts=3D av_rescale_q(pCodecCtxEnc_audio->coded_frame->pts, = pCodecCtxEnc_audio->time_base, audio_st->time_base);>> pkt.stream_index=3D audioStream;> pkt.data=3D outbuf_audio;>> #if 1> if (av_write_frame(pFormatCtxEnc, &pkt) !=3D 0)> {> fprintf(stderr, "Error while writing audio frame\n"); > exit(1);> }> #endif> }> }> // Free the packet that was allocated by av_read_frame> av_free_packet(&packet);}>> // get the delayed frames> for(; out_size; i++) {> out_size =3D avcodec_encode_video(pCodecCtxEnc, outbuf, outbuf_size, > NULL);> if (out_size !=3D 0) {> AVPacket pkt;> av_init_packet(&pkt);>> pkt.pts=3D pCodecCtxEnc->coded_frame->pts;> if(pCodecCtxEnc->coded_frame->key_frame)> pkt.flags |=3D PKT_FLAG_KEY;> pkt.stream_index=3D video_st->index;> pkt.data=3D outbuf;> pkt.size=3D out_size;>> // write the compressed frame in the media file> av_write_frame(pFormatCtxEnc, &pkt);> }}>> // Close the codec> avcodec_close(pCodecCtxDec);> avcodec_close(pCodecCtxEnc);> // Free the YUV frame> av_free(pFrameDec);> av_free(pFrameEnc);。