AVI erstellen mit Malisch's AVI-Lib

    • VB.NET

    Es gibt 14 Antworten in diesem Thema. Der letzte Beitrag () ist von StarGate01.

      AVI erstellen mit Malisch's AVI-Lib

      Hey Leute,

      hier würd ich euch gerne zeigen, wie ihr einen einfachen (!) AVI-Film mit VB.net und Malisch's AVI-Lib ertsellen könnt.
      Zuallererst werden die beiden dll's dafür bennötigt, die könnt ihr euch hier herunterladen.

      Entpackt die Dll's, erstellt ein neues Projekt und fügt dem Projekt anschließend die Verweise hinzu.

      Der Code, um aus einer Image-List (einer Lsite voller Bilder) ein AVI zu erstellen (Funktion):

      VB.NET-Quellcode

      1. Private Sub CreateAVI()
      2. If IO.File.Exists("temp.avi") = True Then
      3. IO.File.Delete("temp.avi")
      4. End If
      5. Dim Imgs As New List(Of Image)
      6. Imgs.Add(Image.FromFile("bild1.png"))
      7. Imgs.Add(Image.FromFile("bild2.png"))
      8. Imgs.Add(Image.FromFile("bild3.png"))
      9. avicreator.CreateAVIFile(Imgs.ToArray(), 5 --- DIE FRAMERATE ---- , SystemInformation.VirtualScreen.Width --- BREITE DES VIDEOS, SystemInformation.VirtualScreen.Height --- HÖHE DES VIDEOS, "temp.avi")
      10. avicreator.setmessage("Das Video wurde erstellt.")
      11. 'optional starten wir das Video
      12. Process.Start("temp.avi")
      13. End Sub


      Es nicht möglcih Sound hinzuzufügen, wenn man ein Bild aber längermachen wilslt, stellst du die Frammerate auf eins und wiederholst das jeweilige Bild so oft ;).

      Habt Spaß damit!

      Mfg
      Epic
      Ich brauche Hilfe. Nachdem ich das Video erstellt habe, kommt ein Fenster, bei dem ich die Komprimierung des Videos auswähle soll. Wenn ich dort etwas auswähle und auf OK drücke, stürzt mein Programm ab. Danke schon mal im Voraus für die Antworten
      Hallo!

      Da ich mich gerade auch mit dem Thema beschäftigt habe, kann ich euch sagen, dass ihr mit der Lib von Malisch kein Video in der Länge einer Stunde erstellen könnt da er eine List(of Image) bzw. ein Image Array benutzt.
      Wenn man jetzt versucht mehr als 1000 Bilder in ein Image-Array zu laden wir euer Arbeitsspeicher irgendwann voll werden und das Programm hängt sich auf =P.

      Lösungsvorschlag:

      Benutzt die avilib die auch Malisch verwendet hat um den Avi Film zu machen oder geht noch weiter zurück und schaut euch mal den Code an adp-gmbh.ch/csharp/avi/write_avi.html. Natürlich müsste dieser Code dann Konvertiert oder umgeschrieben werden.

      Mfg
      Hudson Smith

      PS: Der Fehler mit der Komprimierung wird auch bei der avilib kommen. Ich selber habe da auch noch keine Lösung gefunden.
      Hi,
      ich hatte nichts anderes zu tun und habe den Code in Vb.net übersetzt:
      Viel Spaß damit!

      Spoiler anzeigen

      VB.NET-Quellcode

      1. 'Code von [url]http://www.adp-gmbh.ch/csharp/avi/write_avi.html[/url]
      2. Imports System.Runtime.InteropServices
      3. Imports System.Drawing
      4. Imports System.Drawing.Imaging
      5. Public Class AviWriter
      6. <StructLayout(LayoutKind.Sequential, Pack:=1)> _
      7. Private Structure AVISTREAMINFOW
      8. Public fccType As UInt32, fccHandler As UInt32, dwFlags As UInt32, dwCaps As UInt32
      9. Public wPriority As UInt16, wLanguage As UInt16
      10. Public dwScale As UInt32, dwRate As UInt32, dwStart As UInt32, dwLength As UInt32, dwInitialFrames As UInt32, dwSuggestedBufferSize As UInt32, _
      11. dwQuality As UInt32, dwSampleSize As UInt32, rect_left As UInt32, rect_top As UInt32, rect_right As UInt32, rect_bottom As UInt32, _
      12. dwEditCount As UInt32, dwFormatChangeCount As UInt32
      13. Public szName0 As UInt16, szName1 As UInt16, szName2 As UInt16, szName3 As UInt16, szName4 As UInt16, szName5 As UInt16, _
      14. szName6 As UInt16, szName7 As UInt16, szName8 As UInt16, szName9 As UInt16, szName10 As UInt16, szName11 As UInt16, _
      15. szName12 As UInt16, szName13 As UInt16, szName14 As UInt16, szName15 As UInt16, szName16 As UInt16, szName17 As UInt16, _
      16. szName18 As UInt16, szName19 As UInt16, szName20 As UInt16, szName21 As UInt16, szName22 As UInt16, szName23 As UInt16, _
      17. szName24 As UInt16, szName25 As UInt16, szName26 As UInt16, szName27 As UInt16, szName28 As UInt16, szName29 As UInt16, _
      18. szName30 As UInt16, szName31 As UInt16, szName32 As UInt16, szName33 As UInt16, szName34 As UInt16, szName35 As UInt16, _
      19. szName36 As UInt16, szName37 As UInt16, szName38 As UInt16, szName39 As UInt16, szName40 As UInt16, szName41 As UInt16, _
      20. szName42 As UInt16, szName43 As UInt16, szName44 As UInt16, szName45 As UInt16, szName46 As UInt16, szName47 As UInt16, _
      21. szName48 As UInt16, szName49 As UInt16, szName50 As UInt16, szName51 As UInt16, szName52 As UInt16, szName53 As UInt16, _
      22. szName54 As UInt16, szName55 As UInt16, szName56 As UInt16, szName57 As UInt16, szName58 As UInt16, szName59 As UInt16, _
      23. szName60 As UInt16, szName61 As UInt16, szName62 As UInt16, szName63 As UInt16
      24. End Structure
      25. ' vfw.h
      26. <StructLayout(LayoutKind.Sequential, Pack:=1)> _
      27. Private Structure AVICOMPRESSOPTIONS
      28. Public fccType As UInt32
      29. Public fccHandler As UInt32
      30. Public dwKeyFrameEvery As UInt32
      31. ' only used with AVICOMRPESSF_KEYFRAMES
      32. Public dwQuality As UInt32
      33. Public dwBytesPerSecond As UInt32
      34. ' only used with AVICOMPRESSF_DATARATE
      35. Public dwFlags As UInt32
      36. Public lpFormat As IntPtr
      37. Public cbFormat As UInt32
      38. Public lpParms As IntPtr
      39. Public cbParms As UInt32
      40. Public dwInterleaveEvery As UInt32
      41. End Structure
      42. <StructLayout(LayoutKind.Sequential, Pack:=1)> _
      43. Public Structure BITMAPINFOHEADER
      44. Public biSize As UInt32
      45. Public biWidth As Int32
      46. Public biHeight As Int32
      47. Public biPlanes As Int16
      48. Public biBitCount As Int16
      49. Public biCompression As UInt32
      50. Public biSizeImage As UInt32
      51. Public biXPelsPerMeter As Int32
      52. Public biYPelsPerMeter As Int32
      53. Public biClrUsed As UInt32
      54. Public biClrImportant As UInt32
      55. End Structure
      56. Public Class AviException
      57. Inherits ApplicationException
      58. Public Sub New(ByVal s As String)
      59. MyBase.New(s)
      60. End Sub
      61. Public Sub New(ByVal s As String, ByVal hr As Int32)
      62. MyBase.New(s)
      63. If hr = AVIERR_BADPARAM Then
      64. err_msg = "AVIERR_BADPARAM"
      65. Else
      66. err_msg = "unknown"
      67. End If
      68. End Sub
      69. Public Function ErrMsg() As String
      70. Return err_msg
      71. End Function
      72. Private Const AVIERR_BADPARAM As Int32 = -2147205018
      73. Private err_msg As String
      74. End Class
      75. Public Function Open(ByVal fileName As String, ByVal frameRate As UInt32, ByVal width As Integer, ByVal height As Integer) As Bitmap
      76. frameRate_ = frameRate
      77. width_ = CType(width, UInt32)
      78. height_ = CType(height, UInt32)
      79. bmp_ = New Bitmap(width, height, PixelFormat.Format24bppRgb)
      80. Dim bmpDat As BitmapData = bmp_.LockBits(New Rectangle(0, 0, width, height), ImageLockMode.[ReadOnly], PixelFormat.Format24bppRgb)
      81. stride_ = CType(bmpDat.Stride, UInt32)
      82. bmp_.UnlockBits(bmpDat)
      83. AVIFileInit()
      84. ' OF_WRITE | OF_CREATE (winbase.h)
      85. Dim hr As Integer = AVIFileOpenW(pfile_, fileName, 4097, 0)
      86. If hr <> 0 Then
      87. Throw New AviException("error for AVIFileOpenW")
      88. End If
      89. CreateStream()
      90. SetOptions()
      91. Return bmp_
      92. End Function
      93. Public Sub AddFrame()
      94. Dim bmpDat As BitmapData = bmp_.LockBits(New Rectangle(0, 0, CInt(width_), CInt(height_)), ImageLockMode.[ReadOnly], PixelFormat.Format24bppRgb)
      95. ' pointer to data
      96. ' 16 = AVIIF_KEYFRAMe
      97. Dim hr As Integer = AVIStreamWrite(psCompressed_, count_, 1, bmpDat.Scan0, CType(stride_ * height_, Int32), 0, _
      98. 0, 0)
      99. If hr <> 0 Then
      100. Throw New AviException("AVIStreamWrite")
      101. End If
      102. bmp_.UnlockBits(bmpDat)
      103. count_ += 1
      104. End Sub
      105. Public Sub Close()
      106. AVIStreamRelease(ps_)
      107. AVIStreamRelease(psCompressed_)
      108. AVIFileRelease(pfile_)
      109. AVIFileExit()
      110. End Sub
      111. Private Sub CreateStream()
      112. Dim strhdr As New AVISTREAMINFOW()
      113. strhdr.fccType = fccType_
      114. strhdr.fccHandler = fccHandler_
      115. strhdr.dwFlags = 0
      116. strhdr.dwCaps = 0
      117. strhdr.wPriority = 0
      118. strhdr.wLanguage = 0
      119. strhdr.dwScale = 1
      120. strhdr.dwRate = frameRate_
      121. ' Frames per Second
      122. strhdr.dwStart = 0
      123. strhdr.dwLength = 0
      124. strhdr.dwInitialFrames = 0
      125. strhdr.dwSuggestedBufferSize = height_ * stride_
      126. strhdr.dwQuality = &HFFFFFFFFUI
      127. '-1; // Use default
      128. strhdr.dwSampleSize = 0
      129. strhdr.rect_top = 0
      130. strhdr.rect_left = 0
      131. strhdr.rect_bottom = height_
      132. strhdr.rect_right = width_
      133. strhdr.dwEditCount = 0
      134. strhdr.dwFormatChangeCount = 0
      135. strhdr.szName0 = 0
      136. strhdr.szName1 = 0
      137. Dim hr As Integer = AVIFileCreateStream(pfile_, ps_, strhdr)
      138. If hr <> 0 Then
      139. Throw New AviException("AVIFileCreateStream")
      140. End If
      141. End Sub
      142. Private Sub SetOptions()
      143. Dim opts As New AVICOMPRESSOPTIONS()
      144. opts.fccType = 0
      145. 'fccType_;
      146. opts.fccHandler = 0
      147. 'fccHandler_;
      148. opts.dwKeyFrameEvery = 0
      149. opts.dwQuality = 0
      150. ' 0 .. 10000
      151. opts.dwFlags = 0
      152. ' AVICOMRPESSF_KEYFRAMES = 4
      153. opts.dwBytesPerSecond = 0
      154. opts.lpFormat = 0
      155. 'new IntPtr(0);
      156. opts.cbFormat = 0
      157. opts.lpParms = 0
      158. 'new IntPtr(0);
      159. opts.cbParms = 0
      160. opts.dwInterleaveEvery = 0
      161. Dim p As Pointer(Of AVICOMPRESSOPTIONS) = New Pointer(Of AviWriter.AVICOMPRESSOPTIONS)(opts)
      162. Dim pp As Pointer(Of Pointer(Of AVICOMPRESSOPTIONS)) = New Pointer(Of AviWriter.AVICOMPRESSOPTIONS*)(p)
      163. Dim x As IntPtr = ps_
      164. Dim ptr_ps As New Pointer(Of IntPtr)(x)
      165. AVISaveOptions(0, 0, 1, ptr_ps, pp)
      166. ' TODO: AVISaveOptionsFree(...)
      167. Dim hr As Integer = AVIMakeCompressedStream(psCompressed_, ps_, opts, 0)
      168. If hr <> 0 Then
      169. Throw New AviException("AVIMakeCompressedStream")
      170. End If
      171. Dim bi As New BITMAPINFOHEADER()
      172. bi.biSize = 40
      173. bi.biWidth = CType(width_, Int32)
      174. bi.biHeight = CType(height_, Int32)
      175. bi.biPlanes = 1
      176. bi.biBitCount = 24
      177. bi.biCompression = 0
      178. ' 0 = BI_RGB
      179. bi.biSizeImage = stride_ * height_
      180. bi.biXPelsPerMeter = 0
      181. bi.biYPelsPerMeter = 0
      182. bi.biClrUsed = 0
      183. bi.biClrImportant = 0
      184. hr = AVIStreamSetFormat(psCompressed_, 0, bi, 40)
      185. If hr <> 0 Then
      186. Throw New AviException("AVIStreamSetFormat", hr)
      187. End If
      188. End Sub
      189. <DllImport("avifil32.dll")> _
      190. Private Shared Sub AVIFileInit()
      191. End Sub
      192. <DllImport("avifil32.dll")> _
      193. Private Shared Function AVIFileOpenW(ByRef ptr_pfile As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal fileName As String, ByVal flags As Integer, ByVal dummy As Integer) As Integer
      194. End Function
      195. <DllImport("avifil32.dll")> _
      196. Private Shared Function AVIFileCreateStream(ByVal ptr_pfile As Integer, ByRef ptr_ptr_avi As IntPtr, ByRef ptr_streaminfo As AVISTREAMINFOW) As Integer
      197. End Function
      198. <DllImport("avifil32.dll")> _
      199. Private Shared Function AVIMakeCompressedStream(ByRef ppsCompressed As IntPtr, ByVal aviStream As IntPtr, ByRef ao As AVICOMPRESSOPTIONS, ByVal dummy As Integer) As Integer
      200. End Function
      201. <DllImport("avifil32.dll")> _
      202. Private Shared Function AVIStreamSetFormat(ByVal aviStream As IntPtr, ByVal lPos As Int32, ByRef lpFormat As BITMAPINFOHEADER, ByVal cbFormat As Int32) As Integer
      203. End Function
      204. <DllImport("avifil32.dll")> _
      205. Private Shared Function AVISaveOptions(ByVal hwnd As Integer, ByVal flags As UInt32, ByVal nStreams As Integer, ByVal ptr_ptr_avi As Pointer(Of IntPtr), ByVal ao As Pointer(Of Pointer(Of AVICOMPRESSOPTIONS))) As Integer
      206. End Function
      207. <DllImport("avifil32.dll")> _
      208. Private Shared Function AVIStreamWrite(ByVal aviStream As IntPtr, ByVal lStart As Int32, ByVal lSamples As Int32, ByVal lpBuffer As IntPtr, ByVal cbBuffer As Int32, ByVal dwFlags As Int32, _
      209. ByVal dummy1 As Int32, ByVal dummy2 As Int32) As Integer
      210. End Function
      211. <DllImport("avifil32.dll")> _
      212. Private Shared Function AVIStreamRelease(ByVal aviStream As IntPtr) As Integer
      213. End Function
      214. <DllImport("avifil32.dll")> _
      215. Private Shared Function AVIFileRelease(ByVal pfile As Integer) As Integer
      216. End Function
      217. <DllImport("avifil32.dll")> _
      218. Private Shared Sub AVIFileExit()
      219. End Sub
      220. Private pfile_ As Integer = 0
      221. Private ps_ As New IntPtr(0)
      222. Private psCompressed_ As New IntPtr(0)
      223. Private frameRate_ As UInt32 = 0
      224. Private count_ As Integer = 0
      225. Private width_ As UInt32 = 0
      226. Private stride_ As UInt32 = 0
      227. Private height_ As UInt32 = 0
      228. Private fccType_ As UInt32 = 1935960438
      229. ' vids
      230. Private fccHandler_ As UInt32 = 808810089
      231. ' IV50
      232. '1145656899; // CVID
      233. Private bmp_ As Bitmap
      234. End Class



      MfG,
      Gamemaster22

      Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „mikeb69“ () aus folgendem Grund: VB-Code-Tags eingefügt