五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

自動翻譯播放器源碼

2023-05-30 09:24 作者:巴拉巴拉大老大  | 我要投稿



# -*- coding:utf-8 -*-

from PyQt5.Qt import pyqtSignal,QApplication,QFileDialog,QMutex,QSlider

from PyQt5.uic import loadUi

from PyQt5.QtCore import pyqtSignal,QObject,QTimer,QThread

from PyQt5.QtGui import QIcon,QPixmap,QColor

from pysubs2 import SSAFile, SSAEvent, make_time

import win32com.client as wincl

import os, platform,threading,sys,time,wave,re , pythoncom , glob

# 設置VLC庫路徑,需在import vlc之前

if len(sys.argv) > 1:

? ? os.chdir((sys.argv[0]).replace((sys.argv[0].split('\\'))[-1] , ''))

os.add_dll_directory(os.getcwd())

import vlc

ERROR = ''

qmut_1 = QMutex() # 創(chuàng)建線程鎖

class Sound(QThread): ?# 線程1

? ? def __init__(self,text):

? ? ? ? self.text = text

? ? ? ? super().__init__()


? ? def run(self):

? ? ? ? qmut_1.lock() # 加鎖

? ? ? ? pythoncom.CoInitialize()

? ? ? ? (wincl.Dispatch("SAPI.SpVoice")).Speak(self.text)

? ? ? ? pythoncom.CoUninitialize()

? ? ? ? qmut_1.unlock() # 解鎖.

class Player:

? ? '''

? ? ? ? args:設置 options

? ? '''


? ? def __init__(self, *args):

? ? ? ? def startplay():

? ? ? ? ? ? if args:

? ? ? ? ? ? ? ? instance = vlc.Instance(*args)

? ? ? ? ? ? ? ? self.media = instance.media_player_new()

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? self.media = vlc.MediaPlayer()

? ? ? ? thread = threading.Thread(target=startplay())

? ? ? ? thread.start()

? ? # 設置待播放的url地址或本地文件路徑,每次調用都會重新加載資源

? ? def set_uri(self, uri):

? ? ? ? self.media.set_mrl(uri)

? ? # 播放 成功返回0,失敗返回-1

? ? def play(self, path=None):

? ? ? ? if path:

? ? ? ? ? ? self.set_uri(path)

? ? ? ? ? ? return self.media.play()

? ? ? ? else:

? ? ? ? ? ? return self.media.play()

? ? # 暫停

? ? def pause(self):

? ? ? ? self.media.pause()

? ? # 恢復

? ? def resume(self):

? ? ? ? self.media.set_pause(0)

? ? # 停止

? ? def stop(self):

? ? ? ? self.media.stop()

? ? # 釋放資源

? ? def release(self):

? ? ? ? return self.media.release()

? ? # 是否正在播放

? ? def is_playing(self):

? ? ? ? return self.media.is_playing()

? ? # 已播放時間,返回毫秒值

? ? def get_time(self):

? ? ? ? return self.media.get_time()

? ? # 拖動指定的毫秒值處播放。成功返回0,失敗返回-1 (需要注意,只有當前多媒體格式或流媒體協(xié)議支持才會生效)

? ? def set_time(self, ms):

? ? ? ? return self.media.set_time(ms)

? ? # 音視頻總長度,返回毫秒值

? ? def get_length(self):

? ? ? ? return self.media.get_length()

? ? # 獲取當前音量(0~100)

? ? def get_volume(self):

? ? ? ? return self.media.audio_get_volume()

? ? # 設置音量(0~100)

? ? def set_volume(self, volume):

? ? ? ? return self.media.audio_set_volume(volume)

? ? # 返回當前狀態(tài):正在播放;暫停中;其他

? ? def get_state(self):

? ? ? ? state = self.media.get_state()

? ? ? ? if state == vlc.State.Playing:

? ? ? ? ? ? return 1

? ? ? ? elif state == vlc.State.Paused:

? ? ? ? ? ? return 0

? ? ? ? else:

? ? ? ? ? ? return -1

? ? # 當前播放進度情況。返回0.0~1.0之間的浮點數(shù)

? ? def get_position(self):

? ? ? ? return self.media.get_position()

? ? # 拖動當前進度,傳入0.0~1.0之間的浮點數(shù)(需要注意,只有當前多媒體格式或流媒體協(xié)議支持才會生效)

? ? def set_position(self, float_val):

? ? ? ? return self.media.set_position(float_val)

? ? # 獲取當前文件播放速率

? ? def get_rate(self):

? ? ? ? return self.media.get_rate()

? ? # 設置播放速率(如:1.2,表示加速1.2倍播放)

? ? def set_rate(self, rate):

? ? ? ? return self.media.set_rate(rate)

? ? # 設置寬高比率(如"16:9","4:3")

? ? def set_ratio(self, ratio):

? ? ? ? self.media.video_set_scale(0) ?# 必須設置為0,否則無法修改屏幕寬高

? ? ? ? self.media.video_set_aspect_ratio(ratio)

? ? # 設置窗口句柄

? ? def set_window(self, wm_id):

? ? ? ? if platform.system() == 'Windows':

? ? ? ? ? ? self.media.set_hwnd(wm_id)

? ? ? ? else:

? ? ? ? ? ? self.media.set_xwindow(wm_id)

? ? # 注冊監(jiān)聽器

? ? def add_callback(self, event_type, callback):

? ? ? ? self.media.event_manager().event_attach(event_type, callback)

? ? ? ? print('這里是監(jiān)聽器')

? ? ? ? Stats().ui.videotime.setMaximum(self.media.get_length())

? ? ? ? Stats().ui.videotime.setValue(self.media.get_time())

? ? # 移除監(jiān)聽器

? ? def remove_callback(self, event_type, callback):

? ? ? ? self.media.event_manager().event_detach(event_type, callback)

class Stats:

? ? def __init__(self):

? ? ? ? # 從文件中加載UI定義 從 UI 定義中動態(tài) 創(chuàng)建一個相應的窗口對象 注意:里面的控件對象也成為窗口對象的屬性了 比如 self.ui.button , self.ui.textEdit

? ? ? ? self.ui = loadUi('plugins\\UI\\main.ui')

? ? ? ? self.ui.setWindowIcon(QIcon('plugins\\UI\\logo.png'))

? ? ? ? self.path = ''

? ? ? ? self.loging = ''

? ? ? ? self.Enable = True

? ? ? ? self.player = Player()

? ? ? ? self.player.set_window(self.ui.frame.winId())

? ? ? ? self.tw = timework()

? ? ? ? self.tw.videotime.connect(self.videotime)

? ? ? ? self.ui.answer.setText('朗讀已啟用')

? ? ? ? self.ui.cmd.returnPressed.connect(self.playButton)

? ? ? ? self.ui.openButton.clicked.connect(self.openFileDialog) ? ? #打開

? ? ? ? self.ui.backButton.clicked.connect(self.backButton) ? ? ? ? #后退

? ? ? ? self.ui.playButton.clicked.connect(self.playButton) ? ? ? ? #播放/暫停

? ? ? ? self.ui.flowordButton.clicked.connect(self.flowordButton) ? #前進

? ? ? ? # self.ui.stopButton.setIcon(QIcon('UI\stop.png')) ? ? ? ?#停止

? ? ? ? self.ui.stopButton.clicked.connect(self.stopButton) ? ? ? ? #停止

? ? ? ? self.ui.sound.valueChanged.connect(self.sound) ? ? ? ? ? ? ?#聲音

? ? ? ? self.ui.videotime.sliderMoved.connect(self.videotime) ? ?#拖動進度條同步刷新畫面

? ? ? ? # self.ui.videotime.sliderReleased.connect(self.videotime) ? ?#拖動進度條完畢刷新界面

? ? ? ? self.ui.frame.setStyleSheet("background-color: rgb(0, 0, 0);")#設置播放控件背景色

? ? ? ? self.ui.answer.clicked.connect(self.victostr) ?# 回答問題

? ? ? ? self.clock_time = 0

? ? ? ? self.timer = QTimer() ?# 生成定時器

? ? ? ? self.timer.start(200)

? ? ? ? self.File = sys.argv

? ? ? ? self.timetime = 0

? ? ? ? self.skip = 0

? ? ? ? self.timer.timeout.connect(self.clock) ?# 綁定計時函數(shù) self.clock

? ? def clock(self):

? ? ? ? num = self.player.get_time() ? ? ? ?#獲取播放進度

? ? ? ? if num < 0 :

? ? ? ? ? ? num = 0

? ? ? ? if num > 0:

? ? ? ? ? ? self.ui.videotime.setValue(num) ? ? #同步進度條

? ? ? ? ? ? self.ui.minlcd.display(str(num//60000+100)[1:]) ? ? ? ? #顯示分鐘數(shù)

? ? ? ? ? ? self.ui.slcd.display(':' + str(num//1000%60+100)[1:]) ? #顯示秒數(shù)

? ? ? ? ? ? self.playsubs(num)

? ? def playsubs(self , num):

? ? ? ? for i in self.subs:

? ? ? ? ? ? if (i.start) <= num <= (i.end):

? ? ? ? ? ? ? ? sub = i.text.replace(r'\N' , '\n')

? ? ? ? ? ? ? ? # if not self.is_chinese(sub):

? ? ? ? ? ? ? ? # ? ? sub = self.translate(sub)

? ? ? ? ? ? ? ? if '\n' in sub:

? ? ? ? ? ? ? ? ? ? sub = sub.split('\n')[0]

? ? ? ? ? ? ? ? if self.Enable and self.loging != sub:

? ? ? ? ? ? ? ? ? ? print(f'進度:{str(num)} ? ?開始時間:{str(i.start)} ?結束時間:{str(i.end)} ? ?字幕:{sub}')


? ? ? ? ? ? ? ? ? ? self.ui.echoanswer.setText(sub)

? ? ? ? ? ? ? ? ? ? self.loging = sub

? ? ? ? ? ? ? ? ? ? self.thread_1 = Sound(sub) ?# 創(chuàng)建線程

? ? ? ? ? ? ? ? ? ? self.thread_1.start() ?# 開始線程

? ? def victostr(self):

? ? ? ? if self.Enable:

? ? ? ? ? ? self.ui.answer.setText('朗讀已禁用')

? ? ? ? ? ? self.Enable = False

? ? ? ? ? ? self.ui.echoanswer.setText('')

? ? ? ? else:

? ? ? ? ? ? self.ui.answer.setText('朗讀已啟用')

? ? ? ? ? ? self.Enable = True

? ? def backButton(self): ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #后退

? ? ? ? self.player.set_time(self.player.get_time() - 5000)

? ? ? ? self.ui.videotime.setValue(self.player.get_time())

? ? def playButton(self): ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #播放/暫停

? ? ? ? if self.path:

? ? ? ? ? ? if self.player.is_playing() ?== 1 :

? ? ? ? ? ? ? ? self.player.pause()

? ? ? ? ? ? ? ? self.ui.playButton.setText('播放')

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? self.player.resume()

? ? ? ? ? ? ? ? self.ui.playButton.setText('暫停')

? ? ? ? ? ? self.ui.videotime.setValue(self.player.get_time())

? ? ? ? else:

? ? ? ? ? ? self.openFileDialog()

? ? def flowordButton(self): ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#前進

? ? ? ? self.player.set_time(self.player.get_time() +5000)

? ? ? ? self.ui.videotime.setValue(self.player.get_time())

? ? def stopButton(self): ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #停止ian

? ? ? ? self.player.stop()

? ? ? ? self.ui.videotime.setValue(self.player.get_time())

? ? def sound(self):

? ? ? ? self.player.set_volume(self.ui.sound.value())

? ? def videotime(self):

? ? ? ? self.ui.cmd.setText('')

? ? ? ? self.player.set_time(self.ui.videotime.value())

? ? def initialization(self): #初始化,等待接收文件

? ? ? ? pass

? ? def openFile(self):#類型關聯(lián)打開文件

? ? ? ? self.getsubs(self.File[1])

? ? def timehandle(self,timestr):

? ? ? ? if ':' in timestr:

? ? ? ? ? ? stoptime =timestr.split(':')

? ? ? ? ? ? second = float((stoptime)[0]) * 60 + float((stoptime)[1]) ?# 獲得暫停時間

? ? ? ? else:

? ? ? ? ? ? second = float(timestr)

? ? ? ? return second

? ? def openFileDialog(self):#打開文件

? ? ? ? # 生成文件對話框對象

? ? ? ? dialog = QFileDialog()

? ? ? ? # 設置文件過濾器,這里是任何文件,包括目錄噢

? ? ? ? dialog.setFileMode(QFileDialog.AnyFile)

? ? ? ? # 設置顯示文件的模式,這里是詳細模式

? ? ? ? dialog.setViewMode(QFileDialog.Detail)

? ? ? ? if dialog.exec_():

? ? ? ? ? ? fileNames = dialog.selectedFiles()

? ? ? ? ? ? self.getsubs(fileNames[0])

? ? def getsubs(self , path = ''):

? ? ? ? self.path = path

? ? ? ? self.ui.setWindowTitle(path)#將路徑顯示到標題欄上

? ? ? ? self.player.play(path)

? ? ? ? self.ui.playButton.setText('暫停')

? ? ? ? time.sleep(0.1)

? ? ? ? self.ui.sound.setValue(self.player.get_volume())

? ? ? ? self.ui.videotime.setMaximum(self.player.get_length())

? ? ? ? self.ui.videotime.setValue(self.player.get_time())

? ? ? ? P = os.path.dirname(path)

? ? ? ? if subpath := glob.glob(f'{P}/*.srt'):

? ? ? ? ? ? self.subs = SSAFile.load(subpath[0])

? ? ? ? elif subpath := glob.glob(f'{P}/*.ass'):

? ? ? ? ? ? self.subs = SSAFile.load(subpath[0])

? ? ? ? elif subpath := glob.glob(f'{P}/*.ssa'):

? ? ? ? ? ? self.subs = SSAFile.load(subpath[0])

class timework(QObject):

? ? videotime = pyqtSignal(QSlider,int)

app = QApplication([])

stats = Stats()

stats.ui.show()

stats.initialization()

sys.exit(app.exec_())


自動翻譯播放器源碼的評論 (共 條)

分享到微博請遵守國家法律
剑川县| 城步| 长宁区| 洛宁县| 民乐县| 南江县| 永平县| 曲靖市| 桂阳县| 赣榆县| 诏安县| 巴马| 桐梓县| 新乡县| 汤原县| 龙海市| 宝坻区| 剑川县| 海伦市| 三都| 睢宁县| 礼泉县| 禹州市| 武胜县| 渭源县| 美姑县| 开平市| 泉州市| 芜湖县| 扬中市| 江永县| 宝应县| 浪卡子县| 繁峙县| 渭源县| 庄浪县| 海林市| 洛隆县| 边坝县| 翁源县| 始兴县|