Как мне преобразовать список файлов jpg в файл mjpg? - PullRequest
0 голосов
/ 23 апреля 2019

Мне нужно взять список файлов .jpg и объединить их в файл .mjpg без стороннего поставщика .dll

Код, который я имею, открывает файл для потоковой передачи и добавляет заголовок и заголовки .mjpg и изображения, но когда я открываю его в средстве просмотра, таком как VLC media player, файл не запускается.

   Private Sub Build_File(filepath As String)
    Dim oImage As Bitmap
    Dim fStream As FileStream
    Dim ms As MemoryStream
    Dim bHeader As Byte()
    Dim sFrameHeader As String
    Dim bFrameHeader As Byte()
    Dim bFrameFooter As Byte()

    Dim fileList = New DirectoryInfo(filepath).GetFiles("*.jpg").[Select](Function(o) o.Name).ToList()

    'encode the mjpg header 
    bHeader = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\nContent-Type:multipart/ x - mixed - replace;boundary=myboundary\r\n")
    'put the frame header in a string to allow for replacing the length variable
    sFrameHeader = "myboundary\r\nContent-Type:image/jpeg\r\nContent - Length:[length]\r\n\r\n"
    'encode the frame footer
    bFrameFooter = Encoding.ASCII.GetBytes("\r\n")

    'open a file for streaming
    fStream = File.Open(Path.Combine(filepath, "test.mjpg"), FileMode.Append)
    'write the header for the mjpg
    fStream.Write(bHeader, 0, bHeader.Length)
    For Each sfile As String In fileList
        'get an image
        oImage = Image.FromFile(filepath & "\" & sfile)
        ms = New MemoryStream()
        'save the image into the memory stream
        oImage.Save(ms, ImageFormat.Jpeg)
        'build the frame header by putting the stream length in
        bFrameHeader = Encoding.ASCII.GetBytes(sFrameHeader.Replace("[length]", ms.Length))
        'add the header to the stream
        fStream.Write(bFrameHeader, 0, bFrameHeader.Length)
        'add the image to the stream
        ms.WriteTo(fStream)
        'add the footer to the stream
        fStream.Write(bFrameFooter, 0, bFrameFooter.Length)
        oImage.Dispose()
    Next
    fStream.Close()
End Sub
...