次は、超簡単DVD Playerプログラム。


車載用のDVDを時々作成するんだが、再生できるか確認したくても、いつもどのソフトでDVD が再生できたか、忘れて探し回るはめになっている。

再生できるかどうか確認するためだけの、超簡単DVD Playerの作成に、チャレンジしてみた。


作成できるかどうかの問い合わせを含めて、5回のやりとりで、

自分の求める超簡単DVD Playerができた。


まあ、自分のパソコンはDVDドライブが K:ドライブなので、それに対応できるようにするために、5回もやりとりしたんだがw

(何でK:ドライブになってるかなんて、完全に忘れてるw)


興味がある方は、

Pythonをダウンロードしてインストール
VS Codeをダウンロードしてインストール

メモ帳で、
install python-vlc.bat
などのファイル名で、

pip install python-vlc
pause

の内容のバッチファイルを作成して、実行

VS Codeで
DVDPlayer002.py
などのファイル名で、次のコードのプログラムを作成して、動作させてみてください。


import tkinter as tk
from tkinter import ttk
import ctypes
import vlc


# ==========================================
# DVD Player
# ==========================================

class DVDPlayer:

    def __init__(self, root):
        self.root = root
        self.root.title("DVD Player")
        self.root.geometry("900x600")
        self.root.configure(bg="black")

        # VLC
        self.instance = vlc.Instance("--no-video-title-show")
        self.player = self.instance.media_player_new()

        # --------------------------------------
        # 映像表示部分
        # --------------------------------------
        self.video_frame = tk.Frame(root, bg="black")
        self.video_frame.pack(fill="both", expand=True)

        self.root.update()

        # WindowsのウィンドウハンドルをVLCへ渡す
        self.player.set_hwnd(self.video_frame.winfo_id())

        # --------------------------------------
        # 操作パネル
        # --------------------------------------
        control_frame = tk.Frame(
            root,
            bg="#303030",
            height=100
        )
        control_frame.pack(fill="x", side="bottom")

        # DVDドライブ
        tk.Label(
            control_frame,
            text="DVDドライブ",
            bg="#303030",
            fg="white"
        ).pack(side="left", padx=(10, 5))

        self.drive_combo = ttk.Combobox(
            control_frame,
            width=8,
            state="readonly"
        )
        self.drive_combo.pack(side="left", padx=5)

        # DVDドライブを自動検出
        drives = self.detect_dvd_drives()

        if drives:
            self.drive_combo["values"] = drives
            self.drive_combo.current(0)
        else:
            self.drive_combo["values"] = ["DVDドライブなし"]
            self.drive_combo.current(0)

        # --------------------------------------
        # 再生ボタン
        # --------------------------------------
        self.play_button = tk.Button(
            control_frame,
            text="▶ 再生",
            width=10,
            command=self.play
        )
        self.play_button.pack(side="left", padx=5, pady=15)

        # --------------------------------------
        # 一時停止ボタン
        # --------------------------------------
        self.pause_button = tk.Button(
            control_frame,
            text="Ⅱ 一時停止",
            width=10,
            command=self.pause
        )
        self.pause_button.pack(side="left", padx=5, pady=15)

        # --------------------------------------
        # 停止ボタン
        # --------------------------------------
        self.stop_button = tk.Button(
            control_frame,
            text="■ 停止",
            width=10,
            command=self.stop
        )
        self.stop_button.pack(side="left", padx=5, pady=15)

        # --------------------------------------
        # 音量
        # --------------------------------------
        tk.Label(
            control_frame,
            text="音量",
            bg="#303030",
            fg="white"
        ).pack(side="left", padx=(15, 5))

        self.volume = tk.Scale(
            control_frame,
            from_=0,
            to=100,
            orient="horizontal",
            command=self.change_volume,
            bg="#303030",
            fg="white",
            highlightthickness=0
        )
        self.volume.set(50)
        self.volume.pack(side="left")

        # --------------------------------------
        # 終了ボタン
        # --------------------------------------
        self.exit_button = tk.Button(
            control_frame,
            text="終了",
            width=10,
            command=self.close
        )
        self.exit_button.pack(
            side="right",
            padx=10,
            pady=15
        )

        # 初期音量
        self.player.audio_set_volume(80)

        # ウィンドウ終了処理
        self.root.protocol(
            "WM_DELETE_WINDOW",
            self.close
        )

    # ==========================================
    # DVD / CDドライブを自動検出
    # ==========================================

    def detect_dvd_drives(self):

        drives = []

        # Windows API
        DRIVE_CDROM = 5

        for i in range(26):

            drive_letter = chr(ord("A") + i)
            drive_path = drive_letter + ":\\"

            drive_type = ctypes.windll.kernel32.GetDriveTypeW(
                drive_path
            )

            if drive_type == DRIVE_CDROM:
                drives.append(drive_letter + ":")

        return drives

    # ==========================================
    # DVD再生
    # ==========================================

    def play(self):

        if not self.drive_combo["values"]:
            return

        drive = self.drive_combo.get()

        if not drive.endswith(":"):
            return

        # 現在の再生を停止
        self.player.stop()

        # DVDを指定
        media = self.instance.media_new(
            "dvd:///" + drive + "/"
        )

        self.player.set_media(media)

        # 再生
        self.player.play()

    # ==========================================
    # 一時停止
    # ==========================================

    def pause(self):
        self.player.pause()

    # ==========================================
    # 停止
    # ==========================================

    def stop(self):
        self.player.stop()

    # ==========================================
    # 音量変更
    # ==========================================

    def change_volume(self, value):

        try:
            volume = int(float(value))
            self.player.audio_set_volume(volume)
        except:
            pass

    # ==========================================
    # 終了
    # ==========================================

    def close(self):

        try:
            self.player.stop()
        except:
            pass

        self.root.destroy()


# ==========================================
# 起動
# ==========================================

root = tk.Tk()

app = DVDPlayer(root)

root.mainloop()


DVDドライブを自動で探して、ドライブ指定できるようになっています。


VS Codeからの起動で、

市販のMusic DVD 1枚でしか動作確認してませんw

VS Code無しで起動できるようにはしてませんw

他のパソコンで、動作するかもわかりませんw


このプログラムはAI(ChatGPT)を利用して作成したものです。 

どなたでも自由にコピー・改変・再配布していただいて構いません。

 【免責事項】

 本プログラムの利用によって生じた、いかなる損害やトラブルについても開発者は一切の責任を負いません。ご自身の責任においてご利用ください。


コメント

このブログの人気の投稿