Menu

How to get the video file's width and height?

2009-09-09
2012-10-29
  • Rohan Puranik

    Rohan Puranik - 2009-09-09

    Hi
    I am using DirectshowLib v1.5 and DESCombinelib in my application (in VB.Net) to render video files in AVI format. I have used output's width and height as 640 and 480 respectively. But it seems that if input video's resolution is different than this, the output video's quality is poor. So what i want to do is to determine the input video file's width and height so that i can supply it to the object of Descombine and render the output with the same resolution. But i dont know how to retrieve video file's information (i.e. its Height, Width, Frame rate, bit rate etc.). So how can i programatically determine these parameters (if they are available) of a video file irrespective of its format. Any help in this matter is highly appreciated....
    Thank you

     
    • Peter_B

      Peter_B - 2009-09-09

      I do it this way in my app - using the Sample Grabber.

      ''' <summary> Read and store the properties </summary>
      Private Sub SaveSizeInfo(ByVal sampGrabber As ISampleGrabber)
      Dim hr As Integer

              ' Get the media type from the SampleGrabber
              Dim media As New AMMediaType()
              Try
                  hr = sampGrabber.GetConnectedMediaType(media)
                  DsError.ThrowExceptionForHR(hr)
              Catch ex As Exception
                  Fejl(&quot;Fejl ved GetConnectedMediaType&quot; + vbCrLf + ex.StackTrace)
              End Try
      
              If (media.formatType &lt;&gt; FormatType.VideoInfo) OrElse (media.formatPtr = IntPtr.Zero) Then
                  Throw New NotSupportedException(&quot;Ukendt videotype&quot;)
              End If
      
              ' Grab the size info
              Dim videoInfoHeader As VideoInfoHeader = Nothing
              Try
                  videoInfoHeader = CType(Marshal.PtrToStructure(media.formatPtr, GetType(VideoInfoHeader)), VideoInfoHeader)
              Catch ex As Exception
                  Fejl(&quot;Fejl under 'videoInfoHeadern'&quot; + vbCrLf + _
                              ex.StackTrace)
              End Try
      
              Try
                  m_videoWidth = videoInfoHeader.BmiHeader.Width
                  m_videoHeight = videoInfoHeader.BmiHeader.Height
                  VideoBredde = m_videoWidth
                  VideoHøjde = m_videoHeight
              Catch ex As Exception
                  Fejl(&quot;Fejl i SaveSizeInfo - højde + bredde&quot; + vbCrLf _
      
                         + ex.StackTrace)
              End Try
      
              'Billedhastighed - framerate
              Try
                  If videoInfoHeader.AvgTimePerFrame &gt; 0 Then
                      BilHast = CSng(10000000 / videoInfoHeader.AvgTimePerFrame)
                      'BilHast = CSng(Math.Round(10000000 / videoInfoHeader.AvgTimePerFrame))
                  Else
                      Call FindBilledhastighed(VideoFil)
                  End If
              Catch ex As Exception
                  BilHast = 25
                  MsgBox(&quot;Billedhastighed sat til 25&quot;)
              End Try
      
              'm_stride = CInt(m_videoWidth * (videoInfoHeader.BmiHeader.BitCount / 8))
              Try
                  m_stride = CInt(videoInfoHeader.BmiHeader.ImageSize / m_videoHeight)
              Catch ex As Exception
                  Fejl(&quot;Fejl ved m_Stride.&quot; + vbCrLf _
      
                         + ex.StackTrace)
              End Try
      
              DsUtils.FreeAMMediaType(media)
              media = Nothing
          End Sub
      

      Peter

       
    • Rohan Puranik

      Rohan Puranik - 2009-09-10

      Thank you very much Peter for your prompt reply.
      I got some idea through this code snippet about my requirement. However after looking this code i have got some further query
      >> To call this subroutine one requires the object of ISampleGrabber. So what methods of this interface needs to be called to create it? What other objects will be required to create this "sampGrabber "? And how should i give my input video files path whose height and width is desired? Please reply...
      Thank you

       
    • Rohan Puranik

      Rohan Puranik - 2009-09-10

      Hi Peter
      Thanks again for your reply.
      I tried the code from the link of the zip file that you gave me. Also i have successfully implemented the code in my application. But there's one catch in the code of DXTest:
      >> Whenever i try to retrieve the details of a WMV file the line of code in the subroutine SetupGraph
      hr = m_FilterGraph.Connect(iPinOutSource, iPinInFilter);
      gives the error: "No combination of intermediate filters could be found to make the connection."
      value of hr is -2147220969
      and further code execution stops.
      It's happening with WMV file specifically (as i used the code so far. i am not sure about all the formats). With AVI it's working fine and DIVX file also not offering any issue. Is it due to the filters and grabbers whose name are HARDCODED in the functions?
      Could you please look into it for me?
      Thank you

       
      • Peter_B

        Peter_B - 2009-09-10

        This is due to the fact that the video stream in wmv files isn't 0 (zero).
        I solved the probelem, and it works, but the solution could probably be more elegant.
        You can find it here:
        https://sourceforge.net/forum/message.php?msg_id=5531759

        Peter

         
    • Rohan Puranik

      Rohan Puranik - 2009-09-11

      Thank you Peter
      That is working nice for me. I have rendered AVI files using WMV as input but they get rendered with large file size (say 3 MB WMV is creating 27 MB AVI).I am using "Microsoft Media Video 9 " as default compressor. Do you know any other good and widely available codec which i can use in my application and get files of relatively smaller size. Also i want to compress files using divx codec. But vista is offering issues. Also some codec packs are overriding my filters in the application for example if i install ffdshow on my system it overrides my default compressor (dsd.getdeviceofcatgory) and render the video with its (unknown to me) own codec . Do you know any good codec which could do the job for me . XVID codec is also welcome.

       
      • Peter_B

        Peter_B - 2009-09-11

        I haven't used DESCombine, so I don't know anything about this.
        I use the lib for a subtitle editor where I get the frames (sample time) from the Sample Grabber.
        But I get mpeg-1 files from the TV-station and I convert them to Xvid avi-files with VirtualDub
        before I use them for creating subtitles. Nice quality and good compression.
        90 minutes of video results in a file size of around 900 MB with my settings for Xvid.

        Happy coding and have a nice weekend

        Peter

         
  • Rohan Puranik

    Rohan Puranik - 2009-09-17

    Hi Peter
    I need a codec which could compress the large AVI and WMV files without
    degrading their quality much. I wonder if you could suggest me the appropriate
    person/forum who has worked with DESCombinelib and DirectShowlib.
    Thank you

     
  • Peter_B

    Peter_B - 2009-09-18

    I'm afraid I don't know anything about DESCombinelib and compression but I
    would use Xvid.
    I used to use Microsoft MP4.2 with VirtualDub to make avi files but for some
    reason
    the sample grabber doesn't work with black frames. The sample time doesn't
    change from one frame to the next
    but this is not the case with Xvid.
    I use a target bitrate of 900 kbps, and I set 'max consecutive BVOPs' to 1
    because it makes it easy to seek through the file using
    MediaPosition.put_CurrentPosition.

    Peter

     

Log in to post a comment.