| The following code create a 128 Kbps WMA file from your a MP3 file and if the bit rate of the MP3 file is greater than 128 Kbps then a recompressed 128 Kbps MP3 file is created at the same time.
using
(WmaStream str
=
new
WmaStream(
"
File.mp3
"
,
new
WaveFormat(
44100
,
16
,
2
))![]()
...
{ byte[] buffer = new byte[str.SampleSize*2]; IWMProfile profile = null; //See System Profiles in the WMF SDK documentation for more detail Guid WMProfile_V80_128StereoAudio = new Guid("{407B9450-8BDC-4ee5-88B8-6F527BD941F2}"); WM.ProfileManager.LoadProfileByID(ref WMProfile_V80_128StereoAudio, out profile); AudioWriter[] writers = new AudioWriter[2]; try![]() ...{ writers[0] = new WmaWriter(new FileStream("File.wma", FileMode.Create), str.Format, profile); if ( OrigBitRate > 128000 )![]() ...{ writers[1] = new Mp3Writer(new FileStream("File-128.mp3", FileMode.Create), str.Format, new Yeti.Lame.BE_CONFIG(str.Format, 128)); } int read; while ( (read = str.Read(buffer, 0, buffer.Length)) > 0)![]() ...{ foreach( AudioWriter writer in writers )![]() ...{ if (writer != null) writer.Write(buffer, 0, read); } } } catch (Exception e)![]() ...{ //Handle Exceptions } finally![]() ...{ foreach( AudioWriter writer in writers )![]() ...{ if (writer != null)![]() ...{ try![]() ...{ writer.Close(); } catch![]() ...{ } } } } }
|
|
/**/
/// <summary> /// Write an image from an ASF video file to a jpeg file. /// </summary> /// <param name="AsfVideoFileName">Video file name</param> /// <param name="ImageTime">Time position in 100-nanosecond units of the image to capture</param> /// <param name="JpgFileName">File name of resulting image</param>
public
static
void
WMVImageToJpg(
string
AsfVideoFileName,
ulong
ImageTime,
string
JpgFileName)![]()
...
{ IWMSyncReader Reader; uint VideoOuput = uint.MaxValue; uint VideoStream = uint.MaxValue; uint OutputCount; WM_MEDIA_TYPE mtype = new WM_MEDIA_TYPE(); WMVIDEOINFOHEADER InfoHeader = new WMVIDEOINFOHEADER(); Reader = WM.CreateSyncReader(WMT_RIGHTS.WMT_RIGHT_NO_DRM); Reader.Open(AsfVideoFileName); Reader.GetOutputCount(out OutputCount); ushort[] StreamNumbers = new ushort[OutputCount]; WMT_STREAM_SELECTION[] StreamSelections = new WMT_STREAM_SELECTION[OutputCount]; //Look for the video stream within the ASF file for (uint i = 0; i < OutputCount; i++)![]() ...{ IWMOutputMediaProps Props = null; Guid mt; ushort StreamNumber;![]() Reader.GetOutputProps(i, out Props); Reader.GetStreamNumberForOutput(i, out StreamNumber); StreamNumbers[i] = StreamNumber; StreamSelections[i] = WMT_STREAM_SELECTION.WMT_OFF; Props.GetType(out mt); if ( mt == MediaTypes.WMMEDIATYPE_Video )![]() ...{ uint FormatCount; VideoOuput = i; StreamSelections[i] = WMT_STREAM_SELECTION.WMT_ON; Reader.GetOutputFormatCount(i, out FormatCount); uint BufferSize = (uint)(Marshal.SizeOf(typeof(WM_MEDIA_TYPE)) + Marshal.SizeOf(typeof(WMVIDEOINFOHEADER))); IntPtr buffer = Marshal.AllocCoTaskMem((int)BufferSize); try![]() ...{ //Look for the first uncompressed RGB output format for (uint j = 0; j < FormatCount; j++)![]() ...{ uint Size = 0; Reader.GetOutputFormat(i, j, out Props); Props.GetMediaType(IntPtr.Zero, ref Size); if ( Size > BufferSize )![]() ...{ BufferSize = Size; Marshal.FreeCoTaskMem(buffer); buffer = Marshal.AllocCoTaskMem((int)BufferSize); } Props.GetMediaType(buffer, ref Size); mtype = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE)); if ( mtype.formattype == MediaTypes.WMFORMAT_VideoInfo ) if ( (mtype.subtype == MediaTypes.WMMEDIASUBTYPE_RGB555) || (mtype.subtype == MediaTypes.WMMEDIASUBTYPE_RGB24) || (mtype.subtype == MediaTypes.WMMEDIASUBTYPE_RGB32) )![]() ...{ VideoStream = StreamNumber; InfoHeader = (WMVIDEOINFOHEADER)Marshal.PtrToStructure(mtype.pbFormat, typeof(WMVIDEOINFOHEADER)); Reader.SetOutputProps(i, Props); break; } } } finally![]() ...{ Marshal.FreeCoTaskMem(buffer); } } } if ( VideoOuput == uint.MaxValue)![]() ...{ //No video stream found throw new ArgumentException(string.Format("No video stream found in: {0}", AsfVideoFileName), "AsfVideoFileName"); } if ( VideoStream != uint.MaxValue )![]() ...{ INSSBuffer Sample = null; IntPtr SampleBuff; ulong SampleTime, Duration; uint Flags, OutputNum; ushort StreamNum; Bitmap bmap; PixelFormat pixelfmt = PixelFormat.DontCare; Reader.SetStreamsSelected((ushort)OutputCount, StreamNumbers, StreamSelections); Reader.SetRange(ImageTime, 0); Reader.GetNextSample( (ushort)VideoStream, out Sample, out SampleTime, out Duration, out Flags, out OutputNum, out StreamNum ); //Use GetBufferAndLength instead if you want to do aditional error check Sample.GetBuffer(out SampleBuff); if ( mtype.subtype == MediaTypes.WMMEDIASUBTYPE_RGB555 )![]() ...{ pixelfmt = PixelFormat.Format16bppRgb555; } else if ( mtype.subtype == MediaTypes.WMMEDIASUBTYPE_RGB24 ) ![]() ...{ pixelfmt = PixelFormat.Format24bppRgb; } else if( mtype.subtype == MediaTypes.WMMEDIASUBTYPE_RGB32) ![]() ...{ pixelfmt = PixelFormat.Format32bppRgb; } int stride = InfoHeader.bmiHeader.biWidth * InfoHeader.bmiHeader.biPlanes * InfoHeader.bmiHeader.biBitCount / 8; bmap = new Bitmap(InfoHeader.bmiHeader.biWidth, InfoHeader.bmiHeader.biHeight, stride, pixelfmt, SampleBuff); bmap.RotateFlip(RotateFlipType.RotateNoneFlipY); bmap.Save(JpgFileName, ImageFormat.Jpeg); bmap.Dispose(); } else //No RGB output format found throw new ArgumentException(string.Format("No valid uncompressed video format found in: {0}", AsfVideoFileName), "AsfVideoFileName"); }
The previous function receives time in 100-nanosecond units. To use time in HH:MM:SS you can use a function like this one:
|
| Demo source project: Avi2Wmv.zip, updated: June 2, 2004 Article sources: ManWMF.zip, updated : June 2, 2004 (the demo project needs this version, the version at http://www.thecodeproject.com/ManWMF.asp is not up to date) I've developed a project named Avi2Wmv inspired in the WMF sample UncompAVIToWMV, you can check that sample to better understand the code that I show here. Is a reduce version, I didn't include all the features of the UncompAVIToWMV sample The main classes in this sample are:
The following code shows how to use tryYou can find a similar code in the demo project. The UI is not the best of the demo, here the steps to follow: select the AVI file, optionally select different output ASF file, select the WM profile to use and finally click Convert. |
| |
I have written the following function and it works for me. I don’t use uncompressed samples at all and if you don’t need to edit the streams but just copy I think that the best idea would be to use only compressed samples. There are some issues that commented in the code.
The following code use the previous function and the function HMSmS2WMTime to copy the last part of a video file to another starting at 5th minute. ulong StartTime = HMSmS2WMTime(0, 5, 0, 0);
long Duration = 0;//Zero means until the end of the file
CopyWMV("SomeFile.wmv", "Result.wmv", StartTime, Duration);
|
Media Format SDK Translation
最新推荐文章于 2024-11-27 02:08:45 发布
本文提供使用 Windows Media Format (WMF) SDK 和 C# 进行多媒体文件处理的示例代码,包括从 ASF 文件捕获帧并保存为图片、将 MP3 文件转换为 WMA 格式、从 AVI 文件创建 WMV 文件,以及对 ASF 文件进行快速编辑。



}
}
1315

被折叠的 条评论
为什么被折叠?



