質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

FFmpeg

FFmpegは、動画と音声を交換できるフリーソフトウェアです。UNIX系OSから派生した、MS-DOSから操作するコマンドラインツールです。libavcodecやlibavformat、libswscale、libavfilterなどを含みます。ライセンスは、コンパイルの際のオプションによりLGPLもしくはGPLに決定されます。対応コーデックや使用できるオプションが多く、幅広く利用されています。

Q&A

解決済

1回答

825閲覧

[Python初心者] Class 継承を利用してMediaInfoで取得した情報を表示したい。

退会済みユーザー

退会済みユーザー

総合スコア0

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

FFmpeg

FFmpegは、動画と音声を交換できるフリーソフトウェアです。UNIX系OSから派生した、MS-DOSから操作するコマンドラインツールです。libavcodecやlibavformat、libswscale、libavfilterなどを含みます。ライセンスは、コンパイルの際のオプションによりLGPLもしくはGPLに決定されます。対応コーデックや使用できるオプションが多く、幅広く利用されています。

0グッド

1クリップ

投稿2020/09/17 06:20

編集2020/09/18 07:25

Preview Frame内の白バックのText BoxにInput Pathで選択した動画ファイルのMeida Infoを表示したいのですが、他のクラスで入手したファイルパスを他のクラスに引き継ぐ方法とどうすればffmpegで入手したMedia Infoの情報を表示すればいいのかがわかりません。

イメージ説明

Python

1import tkinter as tk 2from tkinter import filedialog 3import os 4import sys 5import subprocess 6 7win = tk.Tk() 8win.title("IVT Transcode app") 9 10win.geometry("700x700") #Turn on for locked Resolution 11win.configure(background='#52514F') # DARK MODE! | ダークモード! 12icon_photo = tk.PhotoImage(file = '/Volumes/UNSUNG HERO/PYTHON/IVTC_tool/film.png') # icon file | アイコンファイル 13win.iconphoto(False, icon_photo) # Sets the icon | アイコン 14 15 16# ----Dictionaries & Variables | 辞書と変数---- 17output_format = ['.MP4', '.AVI', '.MOV', '.MXF'] 18ffmpeg_path = open(os.path.join(sys.path[0], "ffmpeg/ffmpeg"), "r") 19ffprobe_path = open(os.path.join(sys.path[0], "ffmpeg/ffprobe"), "r") 20 21## Input Settings ## 22class FirstFrame: 23 def __init__(self,master): 24 FrameOne = tk.LabelFrame(master, text="Input Settings", fg="white", bg="#52514F", padx="5", pady="5") 25 FrameOne.grid(column=2, row=2, padx='20', pady='5', sticky='nsew') 26 27 self.file_dialog_input = None 28 self.Text_Input_Path = tk.Label(FrameOne, text="Input Path ", 29 fg="white", bg="#52514F", font=('Helvetica', 10)) 30 self.Text_Input_Path.grid(column=0, row=0, sticky='w') 31 self.Field_Input_Path = tk.Entry(FrameOne, fg="black", highlightbackground="#52514F") 32 self.Field_Input_Path.grid(column=1, row=0, sticky='nsew') 33 self.Button_Input_Path = tk.Button(FrameOne, text="Open File", command=self.file_dialog_input_path, highlightbackground="#52514F", font=('Helvetica', 10)) 34 self.Button_Input_Path.grid(column=2, row=0, sticky='e') 35 36 def file_dialog_input_path(self): 37 self.file_dialog_input = tk.filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes=[('MOV file (*.mov)', '*mov'), ('AVI file (*.avi)', '*.avi'),('MP4 file (*.mp4)', '*.mp4')]) 38 self.Field_Input_Path.insert(tk.END, self.file_dialog_input) 39 print (self.file_dialog_input) 40 41 42## Output Settings ## 43class SecondFrame: 44 def __init__(self,master): 45 FrameTwo = tk.LabelFrame(master, text="Output Settings", fg="white", bg="#52514F", padx="5", pady="5") 46 FrameTwo.grid(column=2, row=3, padx='20', pady='5', sticky='nsew') 47 48 self.file_dialog_output = None 49 self.Text_Output_Path = tk.Label(FrameTwo, text="Output Path", fg="white", bg="#52514F", font=('Helvetica', 10)) 50 self.Text_Output_Path.grid(column=0, row=0, sticky='w') 51 self.Text_Output_Name = tk.Label(FrameTwo, text="Output Filename", fg="white", bg="#52514F", font=('Helvetica', 10)) 52 self.Text_Output_Name.grid(column=0, row=1, sticky='w') 53 self.Field_Output_Path = tk.Entry(FrameTwo, fg="black", highlightbackground="#52514F") 54 self.Field_Output_Path.grid(column=1, row=0, sticky='nsew') 55 self.Field_Output_Name = tk.Entry(FrameTwo, fg="black", highlightbackground="#52514F") 56 self.Field_Output_Name.grid(column=1, row=1, sticky='nsew') 57 self.Button_Output_Path = tk.Button(FrameTwo, text="Select Path", command=self.file_dialog_output_path, highlightbackground="#52514F", pady='2', font=('Helvetica', 10)) 58 self.Button_Output_Path.grid(column=2, row=0, sticky='nsew') 59 60 #Extention Option | 拡張子選択 61 self.Extension_Value = tk.StringVar() 62 self.Extension_Value.set(".MOV") # default value as MOV | デフォルトMOV 63 self.Extension_Option = tk.OptionMenu(FrameTwo, self.Extension_Value, *output_format) 64 self.Extension_Option.config(bg='#52514F', font=('Helvetica', 10), pady='2') 65 self.Extension_Option.grid(column=2, row=1, sticky='nsew') 66 67 68 69 def file_dialog_output_path(self): 70 self.file_dialog_output = tk.filedialog.askdirectory(initialdir = "/",title = "Select path") 71 self.Field_Output_Path.insert(tk.END, self.file_dialog_output) 72 print (self.file_dialog_output) 73 #TextOutputText = tk.Text(FifthFrame, height=8, width=45, bg="grey", highlightbackground='grey') 74 #TextOutputText.delete('1.0', tk.END) 75 #TextOutputText.insert(tk.END, self.file_dialog_output) 76 77## Buttoun Settings## 78class ThirdFrame: 79 def __init__(self,master): 80 FrameThree = tk.LabelFrame(master, text="Button", fg="white", bg="#52514F", pady="5", padx="5") 81 FrameThree.grid(column=3, row=2, rowspan=2, padx='20', pady='5', sticky='nsew') 82 83 #Analyze 84 self.file_dialog_input = None 85 self.ButtonOne = tk.Button(FrameThree,text="Analyze (MediaInfo)", highlightbackground="#52514F", padx=3, pady=3,) 86 self.ButtonOne.grid(column=2, row=0, sticky='nw', pady=1, padx=1) 87 #Preview 88 ButtonTwo = tk.Button(FrameThree, text="Preview", highlightbackground="#52514F", padx=3, pady=3) 89 ButtonTwo.grid(column=2, row=1, sticky='nw', pady=1, padx=1) 90 #Simple Transcode 91 Button_ffm_simple = tk.Button(FrameThree, text="Simple Transcode", highlightbackground="#52514F", padx=3, pady=3) 92 Button_ffm_simple.grid(column=2, row=2, sticky='nw', pady=1, padx=1) 93 94 95## Progress Settings ## 96class ForthFrame: 97 def __init__(self,master): 98 FrameFour = tk.LabelFrame(master, text="Progress", fg="white", bg="#52514F", padx="5", pady="5") 99 FrameFour.grid(column=2, columnspan=2, row=4, padx='20', pady='5', sticky='nsew') 100 101 #Progress | 進捗ウィンドウの設定 102 TextOutputText = tk.Text(FrameFour, height=3, width=90, bg="grey", highlightbackground='grey') 103 TextOutputText.grid(column=1, columnspan=2, row=1, sticky='nsew', pady=5, padx=5) 104 ClearButton = tk.Button(FrameFour, text="Clear", highlightbackground="#52514F", padx=1, pady=1) 105 ClearButton.grid(column=2, row=2, sticky='e', pady=1, padx=1) 106 107##Preview Window Settings ## 108class FifthFrame: 109 def __init__(self,master): 110 FrameFive = tk.LabelFrame(master, text="Preview", fg="white", bg="#52514F", padx="5", pady="5") 111 FrameFive.grid(column=2, columnspan=2, row=1, padx='20', pady='5', sticky='nsew') 112 113 Preview_Start = tk.Button(FrameFive, text="start", fg="black", bg="#52514F", font=('Helvetica', 10)) 114 Preview_Start.grid(column=1, row=4, sticky='nsew', pady=5, padx=5) 115 Preview_ad1f = tk.Button(FrameFive, text=" < ", fg="black", bg="#52514F", font=('Helvetica', 10)) 116 Preview_ad1f.grid(column=0, row=4, sticky='e', pady=5, padx=5) 117 Preview_bc1f = tk.Button(FrameFive, text=" > ", fg="black", bg="#52514F", font=('Helvetica', 10)) 118 Preview_bc1f.grid(column=2, row=4, sticky='w', pady=5, padx=5) 119 120 121 #Cavas Area | キャンバスエリア 122 canvas = tk.Canvas(FrameFive, width = 320, height = 240)#Canvasの作成 123 canvas.create_rectangle(0, 0, 320, 240, fill = 'blue')#塗りつぶし 124 canvas.place(x=0, y=0)#Canvasの配置 125 126 canvas.grid(column=0, columnspan=3, row=0, rowspan=4, pady=5, padx=5) 127 128 #Media Infomation | メディア情報 129 self.MediaInfoOut = tk.Text(FrameFive, height=20, width=40, bg="white", highlightbackground='grey') 130 self.MediaInfoOut.grid(column=3, row=1, rowspan=5, sticky='nsew', pady=5, padx=10) 131 132 def __init__(self,master): 133 # 変数を定義する 134 targetfile = file_dialog_input 135 # コマンドを定義して、変数を挿入する 136 command = 'ffprobe -i {}'.format(targetfile) 137 # コマンドを実行する 138 subprocess.run(command, shell=True) 139 140 141 142 143 144f1 = FirstFrame(win) 145f2 = SecondFrame(win) 146f3 = ThirdFrame(win) 147f4 = ForthFrame(win) 148f5 = FifthFrame(win) 149 150 151#Start the GUI | GUI開始 152win.mainloop()

Class FirstFrameで入手したInput_Pathを

def file_dialog_input_path(self): self.file_dialog_input = tk.filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes=[('MOV file (*.mov)', '*mov'), ('AVI file (*.avi)', '*.avi'),('MP4 file (*.mp4)', '*.mp4')]) self.Field_Input_Path.insert(tk.END, self.file_dialog_input) print (self.file_dialog_input)

Class FifthFrameのtargetfileを変数として使いたい。

def __init__(self,master): # 変数を定義する targetfile = file_dialog_input # コマンドを定義して、変数を挿入する command = 'ffprobe -i {}'.format(targetfile) # コマンドを実行する subprocess.run(command, shell=True)

Class 継承をする際のコードの書き方がいまいちわからず困っております。

どなたかご教示いただけると幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

FirstFrameはグローバル変数の f1 に作成されているので、
同じファイル内であれば f1.file_dialog_input もしくは
f1.Field_Input_Path.get() でアクセス可能です。

ですが、コードを動かすために他にも問題点があるので、
動作確認する為には他の部分の修正も必要です。

  • FifthFrame に __init__ が複数実装されている。
  • FirstFrame で、選択されたファイルは file_dialog_input に保持しているが、

 入力欄との同期が取れていない。
手入力があった場合、ファイルが選択されずキャンセルされた場合、
意図しない挙動になります。

  • subprocess 利用はスレッドで利用しないと、

 外部プログラム実行が長引くとGUIが応答なしになってしまいます。

  • ffprobe_path / ffmpeg_path のファイルの中身を読み込んでいる。

 subprocessで呼び出す実行ファイルのpathを指定したいのではありませんか?

# ~略~ # FifthFrame の2つ目の__init__は一旦削除 f1 = FirstFrame(win) f2 = SecondFrame(win) f3 = ThirdFrame(win) f4 = ForthFrame(win) f5 = FifthFrame(win) def analyze_command(): input_path = f1.Field_Input_Path.get() # TODO: subprocess の使い出力結果を得る f5.MediaInfoOut.insert(tk.END, input_path) f3.ButtonOne.config(command=analyze_command) win.mainloop()

これで、ファイル名の所得は出来るはずなので、
以下の項目を一つづつ順番に調べて見て下さい。

  • subprocess で実行したプログラムの出力結果を得る。
  • GUIが停止しないように、スレッドでsubprocessを呼び出す。
  • サブスレッドからGUIを更新する。

投稿2020/09/19 02:18

teamikl

総合スコア8664

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問