| 1 | """Print details of a media file that pyglet can open (requires FFmpeg). |
| 2 | |
| 3 | Usage:: |
| 4 | |
| 5 | media_info.py <filename> |
| 6 | |
| 7 | """ |
| 8 | import sys |
| 9 | import pyglet |
| 10 | |
| 11 | |
| 12 | def print_ffmpeg_info(): |
| 13 | from pyglet.media import have_ffmpeg |
| 14 | |
| 15 | if have_ffmpeg(): |
| 16 | from pyglet.media.codecs import ffmpeg |
| 17 | print(f'Using FFmpeg version {ffmpeg.get_version()}') |
| 18 | else: |
| 19 | print('FFmpeg not available') |
| 20 | print('https://www.ffmpeg.org/download.html\n') |
| 21 | |
| 22 | |
| 23 | def print_source_info(source): |
| 24 | if source.info: |
| 25 | if source.info.title: |
| 26 | print(f'Title: {source.info.title}') |
| 27 | if source.info.album: |
| 28 | print(f'Album: {source.info.album}') |
| 29 | if source.info.author: |
| 30 | print(f'Author: {source.info.author}') |
| 31 | if source.info.year: |
| 32 | print(f'Year: {source.info.year}') |
| 33 | if source.info.track: |
| 34 | print(f'Track: {source.info.track}') |
| 35 | if source.info.genre: |
| 36 | print(f'Genre: {source.info.genre}') |
| 37 | if source.info.copyright: |
| 38 | print(f'Copyright: {source.info.copyright}') |
| 39 | if source.info.comment: |
| 40 | print(f'Comment: {source.info.comment}') |
| 41 | |
| 42 | if source.audio_format: |
| 43 | af = source.audio_format |
| 44 | print(f'Audio: {af.channels} channel(s), {af.sample_size} bits, {af.sample_rate:.02f} Hz') |
| 45 | |
| 46 | if source.video_format: |
| 47 | vf = source.video_format |
| 48 | if vf.frame_rate: |
| 49 | frame_rate = f'{vf.frame_rate:.02f}' |
| 50 | else: |
| 51 | frame_rate = 'unknown' |
| 52 | if vf.sample_aspect >= 1: |
| 53 | display_width = vf.sample_aspect * vf.width |
| 54 | display_height = vf.height |
| 55 | else: |
| 56 | display_width = vf.width |
| 57 | display_height = vf.sample_aspect / vf.height |
| 58 | print(f'Video: {vf.width}x{vf.height} at aspect {vf.sample_aspect!r} (displays at {display_width}x{display_height}), {frame_rate} fps') |
| 59 | |
| 60 | hours = int(source.duration / 3600) |
| 61 | minutes = int(source.duration / 60) % 60 |
| 62 | seconds = int(source.duration) % 60 |
| 63 | milliseconds = int(source.duration * 1000) % 1000 |
| 64 | print(f'Duration: {hours}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}') |
| 65 | |
| 66 | |
| 67 | if __name__ == '__main__': |
| 68 | if len(sys.argv) != 2: |
| 69 | print(__doc__) |
| 70 | print_ffmpeg_info() |
| 71 | sys.exit(1) |
| 72 | |
| 73 | print_ffmpeg_info() |
| 74 | |
| 75 | filename = sys.argv[1] |
| 76 | try: |
| 77 | source = pyglet.media.load(filename, streaming=True) |
| 78 | print_source_info(source) |
| 79 | del source |
| 80 | except pyglet.media.codecs.MediaException: |
| 81 | print(f'Codec not available to open: {filename}') |