Problem w/ Screenshots from MPG

Just starting out? Need help? Post your questions and find answers here.
Searhin
User
User
Posts: 41
Joined: Mon May 26, 2003 10:53 am
Location: Germany

Problem w/ Screenshots from MPG

Post by Searhin »

Hi,

I wrote a little movie player for grabbing stills from mpgs. But with my code i get only messed up pics. Any idea, anyone ??

The image saving part is this:

Code: Select all

              If OpenWindowedScreen(hWinMain,screen_x,screen_y,screen_width,screen_height,0,0,0)
                string = Left(GetFilePart(movie),Len(GetFilePart(movie))-Len(GetExtensionPart(movie))-1)+"__frame"+Str(frame)+".jpg"
                GrabSprite(#sprite,screen_x + #screen_frame,screen_y + #screen_frame,screen_width -2 * #screen_frame,screen_height - 2 * #screen_frame,0)
                SaveSprite(#sprite,"C:\temp\zz\screenshots\"+string,#PB_ImagePlugIn_JPEG)
                SetGadgetText(#info,"frame "+Str(frame)+" grabbed")
                SetGadgetState(#combo,frame)
                CloseScreen()
              Else
                MessageRequester("Error","Could not open WindowedScreen",0|#MB_IconError)
              EndIf
              StopDrawing()


and the complete code is below:

Code: Select all


UseJPEGImageEncoder()

#win        = 0
#button     = 0
#info       = 1
#combo      = 2
#pause      = 3
#resume     = 4
#title      = 5
#screenshot = 0
#movie      = 0
#sprite     = 0

Procedure myCallback(WindowID,Message,wParam,lParam)

  myCallback_result.l = #PB_ProcessPureBasicEvents
  private_index.l = 0
  
  Select Message
  
    Case #WM_CTLCOLORSTATIC
      SetBkMode_(wParam,#TRANSPARENT)
      
      If lParam = GadgetID(#title)
        SetTextColor_(wParam,$C66939)
        myCallBack_result = CreateSolidBrush_($000000)
      EndIf
      
    Case #WM_CTLCOLOREDIT
      SetBkMode_(wParam,#TRANSPARENT)
      
      If lParam = GadgetID(#info)
        SetTextColor_(wParam,$FFFFFF)
        myCallback_result = CreateSolidBrush_($000000)
      EndIf
  
  EndSelect
  
  ProcedureReturn myCallback_result
  
EndProcedure

string .s = ""
event  .l = 0
pause  .b = 1

width  = GetSystemMetrics_(#SM_CXSCREEN)
height = GetSystemMetrics_(#SM_CYSCREEN) - GetSystemMetrics_(#SM_CYFRAME)

movie.s = OpenFileRequester("Select a Movie","C:\","Movie Files|*.avi;*.mpg;|All Files|*.*",1)

If movie <> ""  And  InitSprite()  And  InitMovie()  And  LoadMovie(#movie,movie)
  total_frames.l = MovieLength()
  MovieAudio(0,0)  ; mute

hWinMain.l = OpenWindow(#win,0,0,width,height,#PB_Window_BorderLess,"")
If hWinMain
  ;*** set window background color      
  hBrushWinMain.l = CreateSolidBrush_($000000)
  SetClassLong_(hWinMain,#GCL_HBRBACKGROUND,hBrushWinMain)
  InvalidateRect_(hWinMain,#NULL,#TRUE)
  
  SetWindowCallback(@myCallback())

  AddKeyboardShortcut(#win,#PB_Shortcut_Escape,0)

    CreateGadgetList(WindowID(#win))
    AdvancedGadgetEvents(#TRUE)
    
    SetGadgetFont(#PB_Default,LoadFont(0,"Arial",16)) 
    TextGadget(#title,Int((width-520)/2)+100,100,320,30,GetFilePart(movie),#PB_Text_Center)
    
    SetGadgetFont(#PB_Default,LoadFont(0,"Arial",12))
    ButtonGadget  (#button,Int((width-520)/2)+200,20,120,25,"Grab Image")
    
    ButtonGadget  (#pause,Int((width-520)/2) ,height-120,124,25,"Pause / Resume")
    ComboBoxGadget(#combo,Int((width-520)/2)+200,height-120,116,60)
    StringGadget  (#info,Int((width-520)/2)+400,height-120,160,30,"(infobox)",#PB_String_BorderLess|#PB_Text_Right)
      
      AddGadgetItem(#combo,0,"select frame")
      For i = 1 To total_frames
        AddGadgetItem(#combo,i,Str(i))
      Next i
      SetGadgetState(#combo,0)
    
    While WindowEvent(): Wend
    
      ;*** scale movie:
      If MovieWidth() <= 0 And MovieHeight() <= 0: End: Else
        #border = 300   ; width of border on left and right side (pixels)
        movie_width = width - 2 * #border
        scaling_factor.f = movie_width / MovieWidth()
        y_pos.l  = Int(0.5 * (height - Int(MovieHeight() * scaling_factor))) - 50
        ResizeMovie(#border,y_pos,Int(MovieWidth() * scaling_factor),Int(MovieHeight() * scaling_factor))
        PlayMovie(#movie,WindowID(#win))
      EndIf
      
      tickstart = GetTickCount_()
      Repeat 

        If MovieStatus() > 0
          frame = MovieStatus()
        Else
          frame = Val(GetGadgetItemText(#combo,GetGadgetState(#combo),0))
        EndIf
        
        If GetTickCount_() - tickstart > 1000  And  MovieStatus() <> -1   ; update each second
          SetGadgetText(#info,"frame "+Str(frame)+" ("+Str(frame - old_frame)+" fps)")
          SetGadgetState(#combo,frame)
          tickstart = GetTickCount_()
          old_frame = frame
        ElseIf MovieStatus() = -1
          tickstart = GetTickCount_()
        EndIf
        
        event = WindowEvent()
          If event = #PB_Event_Gadget  And  EventGadgetID() = #button
            If EventType() = #PB_EventType_LeftClick  And  StartDrawing(WindowOutput())
              #screen_frame = -2  ; crop image area
              screen_x = #border - #screen_frame
              screen_y = y_pos - #screen_frame
              screen_width  = Int(MovieWidth()  * scaling_factor) + 2 * #screen_frame
              screen_height = Int(MovieHeight() * scaling_factor) + 2 * #screen_frame
              
              If OpenWindowedScreen(hWinMain,screen_x,screen_y,screen_width,screen_height,0,0,0)
                string = Left(GetFilePart(movie),Len(GetFilePart(movie))-Len(GetExtensionPart(movie))-1)+"__frame"+Str(frame)+".jpg"
                GrabSprite(#sprite,screen_x + #screen_frame,screen_y + #screen_frame,screen_width -2 * #screen_frame,screen_height - 2 * #screen_frame,0)
                SaveSprite(#sprite,"C:\temp\zz\screenshots\"+string,#PB_ImagePlugIn_JPEG)
                SetGadgetText(#info,"frame "+Str(frame)+" grabbed")
                SetGadgetState(#combo,frame)
                CloseScreen()
              Else
                MessageRequester("Error","Could not open WindowedScreen",0|#MB_IconError)
              EndIf
              StopDrawing()
            EndIf
            
          ElseIf event = #PB_Event_Gadget  And  EventGadgetID() = #combo
            frame = Val(GetGadgetItemText(#combo,GetGadgetState(#combo),0))
            If frame > 0
              frame = Val(GetGadgetItemText(#combo,GetGadgetState(#combo),0))
              SetGadgetText(#info,"frame "+Str(frame)+" selected")
              ResumeMovie()
              MovieSeek(frame)
              PauseMovie()
            EndIf
            
          ElseIf event = #PB_Event_Gadget  And  EventGadgetID() = #pause  And  pause = 1
            If EventType() = #PB_EventType_LeftClick
              PauseMovie()
              SetGadgetState(#combo,frame)
              ActivateGadget(#combo)
              pause = 2
            EndIf
            
          ElseIf event = #PB_Event_Gadget  And  EventGadgetID() = #pause  And  pause = 2
            If EventType() = #PB_EventType_LeftClick
              ResumeMovie()
              frame = Val(GetGadgetItemText(#combo,GetGadgetState(#combo),0))
              MovieSeek(frame + 1)
              pause = 1
            EndIf
            
          ElseIf event = #PB_Event_Menu  And  EventMenuID() = 0
            End
            
          EndIf
        
        Sleep_(0)
      Until  frame >= total_frames - 1
      FreeMovie(#movie)
      
EndIf: EndIf
End
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Re: Problem w/ Screenshots from MPG

Post by Tranquil »

Im not able to try your code here at work, but it might be something to do with the direct x Overlay function of your videocard. The Video is directly written to the graphic buffer and that can be the reason that you can not grab it. HyperSnap supports DirectX screenshots and using that tool it works fine.

Mike
Tranquil
Post Reply