SOLVED - MPEG2 Play Code and Win10

Windows specific questions.
xbgtc
Posts: 211
Joined: Oct 14, 2007 5:40
Location: Australia

Re: SOLVED - MPEG2 Play Code and Win10

Postby xbgtc » Jul 06, 2020 13:57

Yeh i was surprised how much there was at ms docs and never thought it may be still updated as wasn't MCI the replacement for VFW? then with DSHOW replacing MCI i thought i was going backwards going back to MCI but looking at the docs there's so much more info and flexibility with MCI - just wish they had detailed example code too :)

Yes my old dshow code did the job ;)
I just played an old game of mine (strip poker) and now works because of the installed mpeg codec but as you say in Win10 it flickers so updated the code to use GDI and now all good but then realized the arrow keys (used mainly in the game) are VERY unresponsive (they were fine in XP) they use multikey so must be a thing with Win10 as i remember i had another problem with multikey and Win10 before and ended up using inkey instead :)

Ahh C++ nice! the closest i get to C++ is editing ACS in maps of the game Doom. When i was searching MCI before everything was C++ so you shouldn't have too much trouble doing what you want - good luck with it :)
xbgtc
Posts: 211
Joined: Oct 14, 2007 5:40
Location: Australia

Re: SOLVED - MPEG2 Play Code and Win10

Postby xbgtc » Jul 06, 2020 14:57

first page updated with new MCI code.
dodicat
Posts: 6720
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: SOLVED - MPEG2 Play Code and Win10

Postby dodicat » Jul 12, 2020 23:36

GDI code with video screen size found.
Or you can use your own gfx screen (handy for audio files).
Write down a path to a file, or if no path then the openfile window shows.

Code: Select all



#define WIN_INCLUDEALL
#include "Windows.bi"
#include "win/mmsystem.bi"
#Include once "/win/commctrl.bi"
#include "file.bi"
#define nul chr(0)
Screencontrol(103,"GDI")
Const k=.015

Declare Function SetWindowTheme Lib "UxTheme.dll" Alias "SetWindowTheme"(As Any Ptr,As zstring Ptr,As zstring Ptr) As Long

Dim Shared As String req: req="Media (.mpg) files"+NUL+"*.MPG"+NUL+"Others (.mp3,.mpeg, . . .)"+NUL+"*.MP3;*.MPEG"+NUL+"All files (*.*)"+NUL+"*.*"+NUL+NUL
Dim Shared As String message
Function map(a As Double,b As Double,_x_ As Double,c As Double,d As Double) As Double
  Return  (((d)-(c))*((_x_)-(a))/((b)-(a))+(c))
End Function

Function getfiles(filetypes As String) As String
  Dim As zstring * 2048 SELFILE
  Dim As String MYFILTER
  myfilter=filetypes
  Dim As OpenFileName SomeFile
  With SomeFile
    .lStructSize = Sizeof(OpenFileName)
    .hInstance = null
    .lpstrFilter = Strptr(MYFILTER)
    .lpstrFile = @SELFILE
    .nMaxFile = 2048
    .nMaxFileTitle = 0
    .lpstrTitle =@"Movies and songs"
    .Flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
  End With
  GetOpenFileName(@SomeFile)
  Return *SomeFile.lpstrFile
End Function

Function splice(s As String,a() As Long) As String
  Redim a(1 To 4)
  Dim As Long position=Instr(s," "),count=1
  a(1)= Val(s)
  While position>0
    count+=1
    a(count)= Val(Mid(s,position))
    position=Instr(position+1,s," ")
  Wend
  Return s
End Function

Function Getsize( file As String,Byref _Left As integer=0,Byref _Top As integer=0,Byref _Width As integer,Byref _Height As integer) As Long
  Dim As zstring * 50 mcidata
  Redim Elements() As Long
  mciSendString("open  " +Chr(34)+file+Chr(34)+ " type mpegvideo alias file1", NULL, 0, 0)
  Var MCIResult = mciSendString("Where file1 Destination max",@MCIData,50,0)
  If MCIResult = 0 Then
    splice(MCIData,elements())
    _Left = Elements(1)
    _Top = Elements(2)
    _Width = Elements(3)
    _Height = Elements(4)
    mciSendString("close file1", NULL, 0, 0)
  End If
  Return _width*_height
End Function

Function gethandle As hwnd 
  Static As Any Ptr win
  Screencontrol 2,Cast(Integer,win)
  Return win
End Function

Sub play(file As String="")
  If file="" Then file=getfiles(req)
  Dim As Integer x,y
  Screeninfo x,y
  If Screenptr =0 Then
    If Getsize(file,,,x,y) Then Screenres x,y
  End If
  Dim As Any Ptr  p=gethandle
  SetWindowTheme(p," "," ")
  Dim As zstring * 20 ans,length,position
  Windowtitle Mid(file,1+Instrrev(file,Any"\/"))+ "       p = pause, r = resume, s = restart, q = quit"
  mciSendString("open  " +Chr(34)+file+Chr(34)+ " type mpegvideo alias file1", NULL, 0, 0)
  mciSendString("window file1 handle " & p, 0, 0, 0)
  mcisendstring("put file1 destination at "+Str(Int(k*x))+" "+ Str(Int(k*y))+" "+ Str(Int(x-2*k*x))+" "+ Str(Int(y-8-2*k*y))+" ",0,0,0)
  mciSendString("play file1", NULL, 0,0)
  mciSendString("status file1 length",@length, 20,0)
  Dim As Long Lngth,pst
  Lngth=Vallng(length)
  Dim As Double t=Timer
  Dim As String key
  Do
    key=Inkey
    Select Case key
    Case "p"
      mciSendString("pause file1", NULL, 0,0)
    Case "r"
      mciSendString("play file1", NULL, 0,0)
    Case "s"
      mciSendString("play file1 from 0", NULL, 0,0)
    Case "q"
      mciSendString("close file1", NULL, 0, 0):End
    End Select
   
    mciSendString("status file1 position ",@position, 20,0)
    pst=Vallng(position)
    Var xpos=map(0,1,pst/Lngth,0,x)
    Line(0,y-4)-(xpos,y-4)
   
    mciSendString("status file1 mode ",@ans,20,0)
    message=ans
    If ans="stopped" Then Exit Sub
    Sleep 1
    If key=Chr(27) Then mciSendString("close file1", NULL, 0, 0):End
  Loop
End Sub

Sub done Destructor
  mciSendString("close file1", NULL, 0, 0)
End Sub

'===============================================

'screen 20                     'optional screen otherwise mci screen
dim as string filepath=""     'optional path otherwise openfile window
play(filepath)
Print message                  'optional end message
Sleep

 
xbgtc
Posts: 211
Joined: Oct 14, 2007 5:40
Location: Australia

Re: SOLVED - MPEG2 Play Code and Win10

Postby xbgtc » Jul 17, 2020 11:50

Very Good Dodicat!

I have been wasting a lot of time trying to get seamless video play from vid to vid eg. if you play a short clip then play the next one your viewing area goes black for a second then the next vid starts - this is because you have to close the vid first before you can play another so the vid area goes black and because it takes too much time before the next vid displays it stays black for 1 sec or so - annoying!

I have about 125 clips in my game so i thought i'd try joining them into 1 large file and use:

mciSendString("set vid time format frames",Null,0,0)
mciSendString("play vid from "+start+" to "+finish,NULL,0,0)

this fixes the blacking out problem obviously but it does not play some clips properly eg. you get a still frame for about 2secs sometimes or you get the sound but the video is still for the first second weird stuff. Other bits are fine but just too many of these things happening so i gave it away.

Then i tried to "GET" the last frame of each vid then "PUT' it there but if you try using GET in the video area the program just ends (no error code) and it can't even copy the frame anyway so gave up on that also.

I've gone back to my original way and just used an image blitted to the vid area after a vid has played and when the vid closed you see the pic no probs but the problem with this is that if your pic is different than the last frame of the vid just played the transition looks rubbish and the only way would be to have a pic for every last frame of the vids you have (125) and at 931KB a pic that's just too much not to mention the work of getting the images + coding!

The best way and least work would be the capturing of the last frame then blitting it for a seamless look in between playing clips. Is there a way of achieving this?

Failing that i would just have to use a few pics (4 needed) and just put up with the stupid looking transition between vids :(
xbgtc
Posts: 211
Joined: Oct 14, 2007 5:40
Location: Australia

Re: SOLVED - MPEG2 Play Code and Win10

Postby xbgtc » Aug 05, 2020 11:12

Well well well

finished coding my game in WinFBE then compiled it then installed it.

went to play it and low and behold back to square one - audio but no vid!!!!!!!!!

UNBELIEVABLE!

think i'll forget about video and freebasic altogether as it obviously has problems, i mean in FBIde as soon as you saved the basic then loaded it you lost the video (great stuff that!) then use WinFBE and no probs with a saved file and just when you thought it was safe to go back into the water and BANG! your exe does not show vid again - just amazing!
dodicat
Posts: 6720
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: SOLVED - MPEG2 Play Code and Win10

Postby dodicat » Aug 05, 2020 11:56

OK.
I shall experiment with using a screen from a .dll for video.
dodicat
Posts: 6720
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: SOLVED - MPEG2 Play Code and Win10

Postby dodicat » Aug 06, 2020 14:37

Here is screen.bas.
Compile with -lib to get a static library (or -dll to get a dynamic library).
I would suggest try a static first.

Code: Select all

'screen.bas
'compile -lib or -dll

Sub setres(xpos As Long,ypos As Long,xres As Long,yres As Long,bits As Long=32) Export
    Screencontrol(103,"GDI")
    Screenres xres,yres,bits
    Screencontrol 100,xpos,ypos
End Sub


Function handle As Any Ptr  Export
    Static As Any Ptr win
    Screencontrol 2,Cast(Integer,win)
    Return win
End Function

Sub screen_info(Byref x As Integer, Byref y As Integer) Export
    Screeninfo(x,y)
End Sub

Function screenpointer() As Any Ptr Export
    Return Screenptr
End Function

Sub title(s As String) Export
    Windowtitle s
End Sub

Sub putimage(x As Long,y As Long,i As Any Ptr) Export
    Put(x,y),i,Pset
End Sub

Function createimage(x As Long,y As Long,clr As Ulong) As Any Ptr Export
    Static As Any Ptr i
    i= Imagecreate(x,y,clr)
    Return i
End Function

Sub drawline(x1 As Long,y1 As Long,x2 As Long,y2 As Long) Export
    Line(x1,y1)-(x2,y2)
End Sub

Function in_key() As zstring Ptr Export
    Static As zstring * 10 s
    s=Inkey
    Return @s[0]
End Function

 


Here is the video player using the external screen from the library.

Code: Select all


#define WIN_INCLUDEALL
#include "Windows.bi"
#include "win/mmsystem.bi"
#Include once "/win/commctrl.bi"
#include "file.bi"
#define nul chr(0)
#inclib "screen"

declare sub setres(xpos as long,ypos as long,xres as long,yres as long,bits as long=32)
declare Function handle As any ptr
declare sub screen_info(byref x as integer, byref y as integer)
declare function screenpointer() as any ptr
declare sub title(s as string)
declare sub putimage(x as long,y as long,i as any ptr)
declare function createimage(x as long,y as long,clr as ulong) as any ptr
declare sub drawline(x1 as long,y1 as long,x2 as long,y2 as long)
declare function in_key() as zstring ptr

Const k=.015

Declare Function SetWindowTheme Lib "UxTheme.dll" Alias "SetWindowTheme"(As Any Ptr,As zstring Ptr,As zstring Ptr) As Long

Dim Shared As String req: req="Media (.mpg) files"+NUL+"*.MPG"+NUL+"Others (.mp3,.mpeg, . . .)"+NUL+"*.MP3;*.MPEG"+NUL+"All files (*.*)"+NUL+"*.*"+NUL+NUL
Dim Shared As String message
Function map(a As Double,b As Double,_x_ As Double,c As Double,d As Double) As Double
  Return  (((d)-(c))*((_x_)-(a))/((b)-(a))+(c))
End Function

Function getfiles(filetypes As String) As String
  Dim As zstring * 2048 SELFILE
  Dim As String MYFILTER
  myfilter=filetypes
  Dim As OpenFileName SomeFile
  With SomeFile
    .lStructSize = Sizeof(OpenFileName)
    .hInstance = null
    .lpstrFilter = Strptr(MYFILTER)
    .lpstrFile = @SELFILE
    .nMaxFile = 2048
    .nMaxFileTitle = 0
    .lpstrTitle =@"Movies and songs"
    .Flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
  End With
  GetOpenFileName(@SomeFile)
  Return *SomeFile.lpstrFile
End Function

Function splice(s As String,a() As Long) As String
  Redim a(1 To 4)
  Dim As Long position=Instr(s," "),count=1
  a(1)= Val(s)
  While position>0
    count+=1
    a(count)= Val(Mid(s,position))
    position=Instr(position+1,s," ")
  Wend
  Return s
End Function

Function Getsize( file As String,Byref _Left As integer=0,Byref _Top As integer=0,Byref _Width As integer,Byref _Height As integer) As Long
  Dim As zstring * 50 mcidata
  Redim Elements() As Long
  mciSendString("open  " +Chr(34)+file+Chr(34)+ " type mpegvideo alias file1", NULL, 0, 0)
  Var MCIResult = mciSendString("Where file1 Destination max",@MCIData,50,0)
  If MCIResult = 0 Then
    splice(MCIData,elements())
    _Left = Elements(1)
    _Top = Elements(2)
    _Width = Elements(3)
    _Height = Elements(4)
    mciSendString("close file1", NULL, 0, 0)
  End If
  Return _width*_height
End Function


Sub play(file As String="")
  If file="" Then file=getfiles(req)
  Dim As Integer x,y
 
  screen_info(x,y)
  if screenpointer=0 then
    If Getsize(file,,,x,y) Then setres(50,50,x,y)
  End If
 
  Dim As Any Ptr  p=handle
  SetWindowTheme(p," "," ")
  Dim As zstring * 20 ans,length,position
  title Mid(file,1+Instrrev(file,Any"\/"))+ "       p = pause, r = resume, s = restart, q = quit"
 
  mciSendString("open  " +Chr(34)+file+Chr(34)+ " type mpegvideo alias file1", NULL, 0, 0)
  mciSendString("window file1 handle " & p, 0, 0, 0)
  mcisendstring("put file1 destination at "+Str(Int(k*x))+" "+ Str(Int(k*y))+" "+ Str(Int(x-2*k*x))+" "+ Str(Int(y-8-2*k*y))+" ",0,0,0)
  mciSendString("play file1", NULL, 0,0)
  mciSendString("status file1 length",@length, 20,0)
  Dim As Long Lngth,pst
  Lngth=Vallng(length)

  Dim As zString ptr key
  Do
    key=In_key
    Select Case *key
    Case "p"
      mciSendString("pause file1", NULL, 0,0)
    Case "r"
      mciSendString("play file1", NULL, 0,0)
    Case "s"
      mciSendString("play file1 from 0", NULL, 0,0)
    Case "q"
      mciSendString("close file1", NULL, 0, 0):End
    End Select
   
    mciSendString("status file1 position ",@position, 20,0)
    pst=Vallng(position)
    Var xpos=map(0,1,pst/Lngth,0,x)
   
    drawline(0,y-4,xpos,y-4)
   
    mciSendString("status file1 mode ",@ans,20,0)
    message=ans
    If ans="stopped" Then Exit Sub
    Sleep 1
    If *key=Chr(27) Then mciSendString("close file1", NULL, 0, 0):End
  Loop
End Sub

Sub done Destructor
  mciSendString("close file1", NULL, 0, 0)
End Sub

'=================  optional bit  ===========================
dim as string ans
print "do you want to set a screensize y/n"
print "y = yes,  n = no I shall use the default screen "
ans=input(1)
if lcase(ans)="y" then
    dim as string msg
    dim as long x,y
    dim as integer xres,yres
    screeninfo xres,yres  'desktop
    lbl:
    print msg
    print "maximum resolutions set to ";str(xres);",";str(yres)
    input "Enter your resolutions seperated by a comma e.g. 800,600  ",x,y
    if x=0 or y=0 then msg="please redo": goto lbl
    msg=""
    if x>xres or y>yres then msg= "Too big for monitor":goto lbl
    setres(0,0,x,y)' 'optional screen otherwise mci screen
end if
'================================  ====================
''''setres(0,0,800,600) 'optional screen, could be any other resolution, if you delete the optional bit

dim as string filepath=""    ' ' path optional if you delete the optional bit
play(filepath)
if message="stopped" then print "END"                  'optional end message
Sleep

 


Please remember the video code and library in the same folder.

This is a brand new computer, win 10, and I have no other codecs installed.
The video code runs OK.
xbgtc
Posts: 211
Joined: Oct 14, 2007 5:40
Location: Australia

Re: SOLVED - MPEG2 Play Code and Win10

Postby xbgtc » Aug 07, 2020 1:55

thanks for your time and effort dodicat!

i have copied your code and was just going to go through it when i thought i'd try something :O

I copied your previous code (from page 3) and compiled that in WinFBE and that worked FINE!

so i looked at my code and thought i'd better make these #INCLUDES and #DEFINES the same JUST IN CASE

my program #INCLUDES that by the way works fine and you see the video when the basic is run in WinFBE

Code: Select all

#include once "poker.bi"
'#include once "win/dshow.bi"


and yours with my poker.bi added too of course

Code: Select all

#define WIN_INCLUDEALL
#include "Windows.bi"
#include "win/mmsystem.bi"
#Include once "/win/commctrl.bi"
#include "file.bi"
#define nul chr(0)
#include once "poker.bi


and compiled my game and the bloody thing WORKS!

Why when run as a basic it works but when compiled it didn't is a mystery and in dshow.bi it 'includes' all those other includes and i did check each time by running the program and checking if vid worked BUT of course i never compiled it thinking there was no need as if it runs fine in source code it should be fine once compiled and this may explain the weird behavior in FBIde (where once the .bas saved vid wouldn't show anymore). I will have to go through those defines and see what i was missing and discard what's not needed.

Anyway, just because you've gone to all that trouble i will go through the code (not sure what is does exactly and it worked fine) but at least it will be a good example of how to create and use a .dll - thanks again dodicat :)

EDIT:
This is getting ridiculous.
I ran the exe in the WinFBE dir and vid shows no prob (even without the above includes so that was not the problem).

I then moved the exe to the game dir (where i installed the game and was testing) and ran the exe and bang! sound but no video shows.

so somehow, for some reason, the exe does not like to be moved from the dir where it was made. i remember i had a problem once before in WIn10 with trying to run a compiled exe when moved from the created dir hmmm.
badidea
Posts: 2150
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: SOLVED - MPEG2 Play Code and Win10

Postby badidea » Aug 07, 2020 8:48

xbgtc wrote:Why when run as a basic it works but when compiled it didn't is a mystery and in dshow.bi it 'includes' all those other includes and i did check each time by running the program and checking if vid worked BUT of course i never compiled it thinking there was no need as if it runs fine in source code it should be fine once compiled and this may explain the weird behavior in FBIde (where once the .bas saved vid wouldn't show anymore). I will have to go through those defines and see what i was missing and discard what's not needed.

Freebasic is not an interpreter. You cannot run 'as basic'. You cannot run source code. You always have to compile before you can run code. A development tool like FBIde or WinFBE may obscure these steps.

So if you run the executable in the development folder, it works properly?
And if you copy the executable and all files in the same directory to a different location it does not work?

Note the dodicats remark: "Compile with -lib to get a static library (or -dll to get a dynamic library)" can be important.

dodicat wrote:#Include once "/win/commctrl.bi"

The first "/" should be omited I think?
xbgtc
Posts: 211
Joined: Oct 14, 2007 5:40
Location: Australia

Re: SOLVED - MPEG2 Play Code and Win10

Postby xbgtc » Aug 07, 2020 9:31

badidea wrote:Freebasic is not an interpreter. You cannot run 'as basic'. You cannot run source code. You always have to compile before you can run code. A development tool like FBIde or WinFBE may obscure these steps.
OIC i thought it was run as a basic file haha - would explain why i always saw a TEMPENC.exe or whatever name WinFBE gave it.
badidea wrote:So if you run the executable in the development folder, it works properly?
Yep
badidea wrote:And if you copy the executable and all files in the same directory to a different location it does not work?
That's right - well everything works but the original problem of this thread comes back (vid plays and has audio but no video).
badidea wrote:Note the dodicats remark: "Compile with -lib to get a static library (or -dll to get a dynamic library)" can be important.?
Naa not using his latest code but my own game code that i now know works in both WinFBE and FBIde but the exe must be in those directories so all i have to do is figure out why. I'm sure it's a Win10 thing as i've seen a lot of weird things after coming to Win10 from WinXP eg. just now i edited my program .cfg file in the c:\windows dir and after saving it i copied it to another dir and when i opened it the edit wasn't there - just weird!
dodicat
Posts: 6720
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: SOLVED - MPEG2 Play Code and Win10

Postby dodicat » Aug 07, 2020 10:21

Avoid the Program Files folder in win 10, both for installing Freebasic or running compiled .exe files.
Also in Win 10 I have to make exceptions (folders and files) for the anti virus, both my older win 10 with Avira and now with AVG.
I have fbidetemp.exe as an exception and I now only use one desktop folder for everything, which I have flagged as an exception.
As far as floating .exe files around, the desktop folder and folders in the desktop seem to pose no problems.
xbgtc
Posts: 211
Joined: Oct 14, 2007 5:40
Location: Australia

Re: SOLVED - MPEG2 Play Code and Win10

Postby xbgtc » Aug 07, 2020 10:54

Yes dodicat i saw that when i first installed FB and now all my FB installations are directly below C eg. C:\FBfolder

If by exceptions you mean that AVG or Avira excludes them from stuff eg. run scan then yep i have done all that but in Defender.

I can't believe i have these weird problems all the time - i mean vid shows fine in the FB directory but once you move the exe away from the directory the vid won't show. there's nothing wrong with the exe as you can move it back and all ok again but must be something! and to do with video as everything else works fine.
dodicat
Posts: 6720
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: SOLVED - MPEG2 Play Code and Win10

Postby dodicat » Aug 07, 2020 11:15

Are you getting a clean path to the videos?
I read that mciSendString has problems with spaces in a path.
xbgtc
Posts: 211
Joined: Oct 14, 2007 5:40
Location: Australia

Re: SOLVED - MPEG2 Play Code and Win10

Postby xbgtc » Aug 07, 2020 12:39

must be as the video actually plays (you can hear the audio) just no video.

i have been stuffing around trying this and that but no joy. it seems like a codec problem again. if i make the program full screen then it actually works but then the video is brighter than the interweaved pics but anyway i want it windowed in this game.
xbgtc
Posts: 211
Joined: Oct 14, 2007 5:40
Location: Australia

Re: SOLVED - MPEG2 Play Code and Win10

Postby xbgtc » Aug 08, 2020 2:52

SOLVED!!

OMG finally solved this little BS problem.

First i tried installing WinFBE to the game dir eg. c:\games\poker and compiled the program and ran the exe but same deal.

Then i thought i'll just copy the whole Poker dir to my USB drive root dir then ran poker.exe and it worked!

So then i just thought i'd rename the c:\games\poker dir to c:\games\spoker AND It WORKED!

then i remembered i had a poker type in my program and maybe that has something to do with it so went and changed all that and no diff.
then i renamed poker.bi to spoker.bi but also no diff.
then i changed poker.bas to spoker.bas compiled and that worked!
then i renamed spoker.exe to poker.exe and no vid!

So all it was is that you can't have a exe with the same name as the dir - bloody Win10 :)

Return to “Windows”

Who is online

Users browsing this forum: Baidu [Spider] and 3 guests