2023-05-18 21:15:27 +08:00
import warnings
2022-04-08 23:07:41 +08:00
from pyglet . util import CodecRegistry , Decoder , Encoder
2021-04-16 23:21:06 +08:00
from . base import *
import pyglet
_debug = pyglet . options [ ' debug_media ' ]
2022-04-08 23:07:41 +08:00
registry = CodecRegistry ( )
add_decoders = registry . add_decoders
add_encoders = registry . add_encoders
get_decoders = registry . get_decoders
get_encoders = registry . get_encoders
2021-04-16 23:21:06 +08:00
class MediaDecoder ( Decoder ) :
2022-04-08 23:07:41 +08:00
def decode ( self , filename , file , streaming ) :
2021-04-16 23:21:06 +08:00
""" Read the given file object and return an instance of `Source`
or ` StreamingSource ` .
2022-04-08 23:07:41 +08:00
Throws DecodeException if there is an error . ` filename `
2021-04-16 23:21:06 +08:00
can be a file type hint .
"""
raise NotImplementedError ( )
class MediaEncoder ( Encoder ) :
2022-04-08 23:07:41 +08:00
def encode ( self , source , filename , file ) :
2021-04-16 23:21:06 +08:00
""" Encode the given source to the given file. `filename`
provides a hint to the file format desired . options are
encoder - specific , and unknown options should be ignored or
issue warnings .
"""
raise NotImplementedError ( )
2022-04-08 23:07:41 +08:00
def add_default_codecs ( ) :
2021-04-16 23:21:06 +08:00
# Add all bundled codecs. These should be listed in order of
# preference. This is called automatically by pyglet.media.
try :
from . import wave
2022-04-08 23:07:41 +08:00
registry . add_decoders ( wave )
registry . add_encoders ( wave )
2021-04-16 23:21:06 +08:00
except ImportError :
pass
if pyglet . compat_platform . startswith ( ' linux ' ) :
try :
from . import gstreamer
2022-04-08 23:07:41 +08:00
registry . add_decoders ( gstreamer )
2021-04-16 23:21:06 +08:00
except ImportError :
pass
try :
if pyglet . compat_platform in ( ' win32 ' , ' cygwin ' ) :
from pyglet . libs . win32 . constants import WINDOWS_VISTA_OR_GREATER
if WINDOWS_VISTA_OR_GREATER : # Supports Vista and above.
from . import wmf
2022-04-08 23:07:41 +08:00
registry . add_decoders ( wmf )
2021-04-16 23:21:06 +08:00
except ImportError :
pass
try :
if have_ffmpeg ( ) :
from . import ffmpeg
2022-04-08 23:07:41 +08:00
registry . add_decoders ( ffmpeg )
2021-04-16 23:21:06 +08:00
except ImportError :
pass
2021-09-23 06:34:23 +08:00
try :
from . import pyogg
2022-04-08 23:07:41 +08:00
registry . add_decoders ( pyogg )
2021-09-23 06:34:23 +08:00
except ImportError :
pass
2021-04-16 23:21:06 +08:00
2023-02-07 11:27:15 +08:00
if pyglet . compat_platform . startswith ( " darwin " ) :
try :
from . import coreaudio
registry . add_decoders ( coreaudio )
except ImportError :
pass
2021-12-26 23:06:03 +08:00
2021-04-16 23:21:06 +08:00
def have_ffmpeg ( ) :
""" Check if FFmpeg library is available.
Returns :
bool : True if FFmpeg is found .
. . versionadded : : 1.4
"""
try :
from . import ffmpeg_lib
if _debug :
2023-05-18 21:15:27 +08:00
print ( ' FFmpeg available, using to load media files. ' )
found = False
for release_versions , build_versions in ffmpeg_lib . release_versions . items ( ) :
if ffmpeg_lib . compat . versions == build_versions :
if _debug :
print ( f ' FFMpeg Release Version: { release_versions } . Versions Loaded: { ffmpeg_lib . compat . versions } ' )
found = True
break
if not found :
warnings . warn ( f ' FFmpeg release version not found. This may be a new or untested release. Unknown behavior may occur. Versions Loaded: { ffmpeg_lib . compat . versions } ' )
2021-04-16 23:21:06 +08:00
return True
2023-05-18 21:15:27 +08:00
except ( ImportError , FileNotFoundError , AttributeError ) as err :
2021-04-16 23:21:06 +08:00
if _debug :
2023-05-18 21:15:27 +08:00
print ( f ' FFmpeg not available: { err } ' )
2021-04-16 23:21:06 +08:00
return False