java如何生成avi格式的文件??? 从摄像头的数据端口能抓到视频的数据,抓下来的数据保存在文本文件中,请问这些二进制的数据怎么才能生成avi格式的视频?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 补充一下,保存在txt中的这些数据是可以用vlc-0.9.4的播放器播的,因为这个播放器可以播那些没有头的文件,请问如何弄才能把这些数据保存成avi的格式的,使得别的播放器也可以播? open-open.com找个组件吧.你要自己编码这个格式也是可以的,不过非常麻烦.我读过c语言的avi文件库文件头就够折腾了,呵呵! http://msdn.microsoft.com/en-us/library/ms779636.aspx编码格式参考. 下面是cs的代码,什么采样率,声道...很麻烦的using System;using System.IO;namespace WaveLib{ /// <summary> /// WAVE 文件流 /// </summary> public class WaveStream : Stream, IDisposable { //private System.IO.MemoryStream waveStream ; private Stream m_Stream; /// <summary> /// 当前位置 /// </summary> private long m_DataPos; /// <summary> /// 长度 /// </summary> private long m_Length; //格式 private WaveFormat m_Format; /// <summary> /// 该文件的格式(即 WAVE 文件的格式) /// </summary> public WaveFormat Format { get { return m_Format; } } /// <summary> /// 获取WAVE 文件的 Chunk 的值 /// </summary> /// <param name="reader"></param> /// <returns></returns> private string ReadChunk(BinaryReader reader) { byte[] ch = new byte[4]; reader.Read(ch, 0, ch.Length); return System.Text.Encoding.ASCII.GetString(ch); } /// <summary> /// 解析 WAVE 文件的 Chunk /// </summary> private void ReadHeader() { BinaryReader Reader = new BinaryReader(m_Stream); //分析 RIFF WAVE Chunk 的 ID if(ReadChunk(Reader) != "RIFF") throw new Exception("Invalid file format"); //跳过 RIFF WAVE Chunk 的 Size Reader.ReadInt32(); // File length minus first 8 bytes of RIFF description, we don't use it //分析 RIFF WAVE Chunk 的 Type if (ReadChunk(Reader) != "WAVE") throw new Exception("Invalid file format"); //分析 Format Chunk 的 ID if (ReadChunk(Reader) != "fmt ") throw new Exception("Invalid file format"); //分析 Format Chunk 的 Size,数值为16或18,18则最后又附加信息 int len = Reader.ReadInt32(); if (len < 16) // bad format chunk length throw new Exception("Invalid file format"); m_Format = new WaveFormat(22050, 16, 2); // initialize to any format //分析 Format Chunk 的 FormatTag; 编码方式,一般为0x0001 m_Format.wFormatTag = Reader.ReadInt16(); //分析 Format Chunk 的 Channels; 声道数目,1--单声道;2--双声道 m_Format.nChannels = Reader.ReadInt16(); //分析 Format Chunk 的 SamplesPerSec,采样频率 m_Format.nSamplesPerSec = Reader.ReadInt32(); //分析 Format Chunk 的 AvgBytesPerSec,每秒所需字节数 m_Format.nAvgBytesPerSec = Reader.ReadInt32(); //分析 Format Chunk 的 BlockAlign,数据块对齐单位(每个采样需要的字节数) m_Format.nBlockAlign = Reader.ReadInt16(); //分析 Format Chunk 的 BitsPerSample,每个采样需要的bit数 m_Format.wBitsPerSample = Reader.ReadInt16(); //跳过两个字节的附加信息 // advance in the stream to skip the wave format block len -= 16; // minimum format size while (len > 0) { Reader.ReadByte(); len--; } //跳过 Fact Chunk,定位到 Data Chunk 的起始位置 // assume the data chunk is aligned while(m_Stream.Position < m_Stream.Length && ReadChunk(Reader) != "data") ; if (m_Stream.Position >= m_Stream.Length) throw new Exception("Invalid file format"); //分析 Data Chunk 的 Size m_Length = Reader.ReadInt32(); //保存 Data 在 m_Stream 中的位置 m_DataPos = m_Stream.Position; Position = 0; } public WaveStream(string fileName) : this(new FileStream(fileName, FileMode.Open)) { } public WaveStream(Stream S) { m_Stream = S; ReadHeader(); } ~WaveStream() { Dispose(); } public void Dispose() { if (m_Stream != null) m_Stream.Close(); GC.SuppressFinalize(this); } public override bool CanRead { get { return true; } } public override bool CanSeek { get { return true; } } public override bool CanWrite { get { return false; } } public override long Length { get { return m_Length; } } public override long Position { get { return m_Stream.Position - m_DataPos; } set { Seek(value, SeekOrigin.Begin); } } public override void Close() { Dispose(); } public override void Flush() { } public override void SetLength(long len) { throw new InvalidOperationException(); } public override long Seek(long pos, SeekOrigin o) { switch(o) { case SeekOrigin.Begin: m_Stream.Position = pos + m_DataPos; break; case SeekOrigin.Current: m_Stream.Seek(pos, SeekOrigin.Current); break; case SeekOrigin.End: m_Stream.Position = m_DataPos + m_Length - pos; break; } return this.Position; } public override int Read(byte[] buf, int ofs, int count) { int toread = (int)Math.Min(count, m_Length - Position); return m_Stream.Read(buf, ofs, toread); } public override void Write(byte[] buf, int ofs, int count) { throw new InvalidOperationException(); } }} 恩,最重要的就是头的格式,弄了个avi的视频用 UltraEdit看了二进制的数据,不过既然能用别的播放器播,是不是代表数据是不用压缩什么的? java里的静态变量是放在了堆内存还是栈内存? suse linux 下,执行 Runtime.getRuntime().exec(cmd) 不成功 java 开发的c/s 数据库程序中 建立存储过程问题 如何取出日期所在周的第一天(周一)和最后一天(周日)的日期 看书看的累死了。来散点分,顺便问些心得 一道初学者的问题。 菜鸟问题,各位大侠举手之劳,在线等待。 Applet 求救? 如何使用JBuilder做电子地图?(Applet) 求大神指教我这个问题出在哪里 请问怎样在一个类中叠代LIST的内容?3Q. 关于接口的问题询问
找个组件吧.你要自己编码这个格式也是可以的,不过非常麻烦.我读过c语言的avi文件库文件头就够折腾了,呵呵!
编码格式参考.
using System;
using System.IO;namespace WaveLib
{
/// <summary>
/// WAVE 文件流
/// </summary>
public class WaveStream : Stream, IDisposable
{
//private System.IO.MemoryStream waveStream ;
private Stream m_Stream;
/// <summary>
/// 当前位置
/// </summary>
private long m_DataPos;
/// <summary>
/// 长度
/// </summary>
private long m_Length; //格式
private WaveFormat m_Format; /// <summary>
/// 该文件的格式(即 WAVE 文件的格式)
/// </summary>
public WaveFormat Format
{
get { return m_Format; }
}
/// <summary>
/// 获取WAVE 文件的 Chunk 的值
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
private string ReadChunk(BinaryReader reader)
{
byte[] ch = new byte[4];
reader.Read(ch, 0, ch.Length);
return System.Text.Encoding.ASCII.GetString(ch);
} /// <summary>
/// 解析 WAVE 文件的 Chunk
/// </summary>
private void ReadHeader()
{
BinaryReader Reader = new BinaryReader(m_Stream);
//分析 RIFF WAVE Chunk 的 ID
if(ReadChunk(Reader) != "RIFF")
throw new Exception("Invalid file format"); //跳过 RIFF WAVE Chunk 的 Size
Reader.ReadInt32(); // File length minus first 8 bytes of RIFF description, we don't use it //分析 RIFF WAVE Chunk 的 Type
if (ReadChunk(Reader) != "WAVE")
throw new Exception("Invalid file format"); //分析 Format Chunk 的 ID
if (ReadChunk(Reader) != "fmt ")
throw new Exception("Invalid file format"); //分析 Format Chunk 的 Size,数值为16或18,18则最后又附加信息
int len = Reader.ReadInt32();
if (len < 16) // bad format chunk length
throw new Exception("Invalid file format"); m_Format = new WaveFormat(22050, 16, 2); // initialize to any format
//分析 Format Chunk 的 FormatTag; 编码方式,一般为0x0001
m_Format.wFormatTag = Reader.ReadInt16();
//分析 Format Chunk 的 Channels; 声道数目,1--单声道;2--双声道
m_Format.nChannels = Reader.ReadInt16();
//分析 Format Chunk 的 SamplesPerSec,采样频率
m_Format.nSamplesPerSec = Reader.ReadInt32();
//分析 Format Chunk 的 AvgBytesPerSec,每秒所需字节数
m_Format.nAvgBytesPerSec = Reader.ReadInt32();
//分析 Format Chunk 的 BlockAlign,数据块对齐单位(每个采样需要的字节数)
m_Format.nBlockAlign = Reader.ReadInt16();
//分析 Format Chunk 的 BitsPerSample,每个采样需要的bit数
m_Format.wBitsPerSample = Reader.ReadInt16(); //跳过两个字节的附加信息
// advance in the stream to skip the wave format block
len -= 16; // minimum format size
while (len > 0)
{
Reader.ReadByte();
len--;
} //跳过 Fact Chunk,定位到 Data Chunk 的起始位置
// assume the data chunk is aligned
while(m_Stream.Position < m_Stream.Length && ReadChunk(Reader) != "data")
; if (m_Stream.Position >= m_Stream.Length)
throw new Exception("Invalid file format"); //分析 Data Chunk 的 Size
m_Length = Reader.ReadInt32();
//保存 Data 在 m_Stream 中的位置
m_DataPos = m_Stream.Position; Position = 0;
}
public WaveStream(string fileName) : this(new FileStream(fileName, FileMode.Open))
{
}
public WaveStream(Stream S)
{
m_Stream = S;
ReadHeader();
}
~WaveStream()
{
Dispose();
}
public void Dispose()
{
if (m_Stream != null)
m_Stream.Close();
GC.SuppressFinalize(this);
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return false; }
}
public override long Length
{
get { return m_Length; }
}
public override long Position
{
get { return m_Stream.Position - m_DataPos; }
set { Seek(value, SeekOrigin.Begin); }
}
public override void Close()
{
Dispose();
}
public override void Flush()
{
}
public override void SetLength(long len)
{
throw new InvalidOperationException();
}
public override long Seek(long pos, SeekOrigin o)
{
switch(o)
{
case SeekOrigin.Begin:
m_Stream.Position = pos + m_DataPos;
break;
case SeekOrigin.Current:
m_Stream.Seek(pos, SeekOrigin.Current);
break;
case SeekOrigin.End:
m_Stream.Position = m_DataPos + m_Length - pos;
break;
}
return this.Position;
}
public override int Read(byte[] buf, int ofs, int count)
{
int toread = (int)Math.Min(count, m_Length - Position);
return m_Stream.Read(buf, ofs, toread);
}
public override void Write(byte[] buf, int ofs, int count)
{
throw new InvalidOperationException();
}
}
}