【发布时间】:2020-10-28 15:52:07
【问题描述】:
我有一个使用 Youtube_dlc 的小型 PyQt5 GUI。我想将一些打印语句放入外部日志文件中进行测试。 但是,在某个点之后(在我将视频元数据存储到变量中之后),我的程序不再将打印语句保存到文件中。只有当我在我的应用程序上按 X 并关闭它时,它才会一次保存所有行。 但是,当我不将标准输出重定向到文件时,它会像平常一样打印。
这是我的代码:
import youtube_dlc
from audioclipextractor import AudioClipExtractor
from PyQt5 import QtWidgets
import Output
import sys
import os
import datetime
ffmpegpath = "ffmpeg.exe"
sys.stdout = open("test.txt", "w")
open("test.txt", "w").truncate(0)
class youtubeDownloader(QtWidgets.QMainWindow, Output.Ui_MainWindow):
def __init__(self, parent=None):
super(youtubeDownloader, self).__init__(parent)
self.setupUi(self)
self.button_download.clicked.connect(lambda: self.add_youtubeaudio(url=self.textbox_url.toPlainText(), start=self.textbox_start.toPlainText(), end=self.textbox_end.toPlainText(), file=self.textbox_filename.toPlainText()))
def add_youtubeaudio(self, url, start=None, end=None, file=None):
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dlc.YoutubeDL(ydl_opts) as ydl:
print("{} # Downloading Youtube video {}".format(datetime.datetime.now(),url))
ydl.download([url])
print("{} # Downloading Youtube video {} complete".format(datetime.datetime.now(),url))
self.textBrowser_Output.setPlainText("{} wurde heruntergeladen".format(file))
print("{} # Downloading Metadata {}".format(datetime.datetime.now(), url))
video_info = (ydl.extract_info(url))['id']
print("{} # Downloading Metadata {} complete".format(datetime.datetime.now(), url))
for x in os.listdir(os.curdir):
if str(video_info)+".mp3" in x:
videokeep = x
print("{} # Checking altarnative arguments".format(datetime.datetime.now()))
if start and end:
print("{} # Cutting video {} from {} to {}".format(datetime.datetime.now(),url,start,end))
ext = AudioClipExtractor(str(videokeep), ffmpegpath)
specs = str(start) + " " + str(end)
ext.extract_clips(specs)
print("{} # Cutting video {} from {} to {} complete".format(datetime.datetime.now(),url, start, end))
try:
print("{} # Renaming file to {}".format(datetime.datetime.now(),file))
os.rename('clip1.mp3',str(file)+".mp3")
print("{} # Renaming file to {} complete".format(datetime.datetime.now(),file))
self.textBrowser_Output.setPlainText("{} wurde in clip geschnitten".format(file))
except:
print("{} # Output file {} already exists".format(datetime.datetime.now(),file))
self.textBrowser_Output.setPlainText("{} existiert bereits".format(file))
else:
try:
print("{} # Renaming file to {}".format(datetime.datetime.now(),file))
os.rename(videokeep, str(file)+".mp3")
print("{} # Renaming file to {} complete".format(datetime.datetime.now(),file))
except:
print("{} # Output file {} already exists".format(datetime.datetime.now(),file))
if self.checkBox_deletevid.isChecked():
try:
os.remove(videokeep)
except:
pass
def main():
app = QtWidgets.QApplication(sys.argv)
form = youtubeDownloader()
form.show()
app.exec_()
if __name__ == '__main__':
main()
(https://github.com/FlyingThunder/YoutubeDownloader 的完整代表)
像这样运行它并使用默认输入将其保存到文件中:
[youtube] C0DPdy98e4c: Downloading webpage
2020-10-28 16:33:05.376083#Downloading Youtube video https://www.youtube.com/watch?v=C0DPdy98e4c
[download] Destination: TEST VIDEO-C0DPdy98e4c.m4a
[download] 0.4% of 274.02KiB at 167.00KiB/s ETA 00:01
[download] 1.1% of 274.02KiB at 501.01KiB/s ETA 00:00
[download] 2.6% of 274.02KiB at 1.14MiB/s ETA 00:00
[download] 5.5% of 274.02KiB at 2.10MiB/s ETA 00:00
[download] 11.3% of 274.02KiB at 1.38MiB/s ETA 00:00
[download] 23.0% of 274.02KiB at 1.67MiB/s ETA 00:00
[download] 46.3% of 274.02KiB at 2.22MiB/s ETA 00:00
[download] 93.1% of 274.02KiB at 3.08MiB/s ETA 00:00
[download] 100.0% of 274.02KiB at 3.27MiB/s ETA 00:00
[download] 100% of 274.02KiB in 00:00
[ffmpeg] Correcting container in "TEST VIDEO-C0DPdy98e4c.m4a"
[ffmpeg] Destination: TEST VIDEO-C0DPdy98e4c.mp3
Deleting original file TEST VIDEO-C0DPdy98e4c.m4a (pass -k to keep)
[youtube] C0DPdy98e4c: Downloading webpage
2020-10-28 16:33:07.722789#Downloading Youtube video https://www.youtube.com/watch?v=C0DPdy98e4c complete
2020-10-28 16:33:07.722789#Downloading Metadata https://www.youtube.com/watch?v=C0DPdy98e4c
[download] Destination: TEST VIDEO-C0DPdy98e4c.m4a
[download] 0.4% of 274.02KiB at 143.29KiB/s ETA 00:01
[download] 1.1% of 274.02KiB at 429.88KiB/s ETA 00:00
[download] 2.6% of 274.02KiB at 1003.04KiB/s ETA 00:00
[download] 5.5% of 274.02KiB at 1.84MiB/s ETA 00:00
[download] 11.3% of 274.02KiB at 878.15KiB/s ETA 00:00
[download] 23.0% of 274.02KiB at 1.24MiB/s ETA 00:00
[download] 46.3% of 274.02KiB at 1.76MiB/s ETA 00:00
[download] 93.1% of 274.02KiB at 2.64MiB/s ETA 00:00
[download] 100.0% of 274.02KiB at 2.75MiB/s ETA 00:00
[download] 100% of 274.02KiB in 00:00
[ffmpeg] Correcting container in "TEST VIDEO-C0DPdy98e4c.m4a"
[ffmpeg] Destination: TEST VIDEO-C0DPdy98e4c.mp3
Deleting original file TEST VIDEO-C0DPdy98e4c.m4a (pass -k to keep)
当注释掉sys.stdout 行时,我在控制台中得到了这个:
2020-10-28 16:46:57.879111 # Downloading Youtube video https://www.youtube.com/watch?v=C0DPdy98e4c
[youtube] C0DPdy98e4c: Downloading webpage
[download] Destination: TEST VIDEO-C0DPdy98e4c.m4a
[download] 100% of 274.02KiB in 00:00
[ffmpeg] Correcting container in "TEST VIDEO-C0DPdy98e4c.m4a"
[ffmpeg] Destination: TEST VIDEO-C0DPdy98e4c.mp3
Deleting original file TEST VIDEO-C0DPdy98e4c.m4a (pass -k to keep)
2020-10-28 16:47:00.811446 # Downloading Youtube video https://www.youtube.com/watch?v=C0DPdy98e4c complete
2020-10-28 16:47:00.811446 # Downloading Metadata https://www.youtube.com/watch?v=C0DPdy98e4c
[youtube] C0DPdy98e4c: Downloading webpage
[download] Destination: TEST VIDEO-C0DPdy98e4c.m4a
[download] 100% of 274.02KiB in 00:00
[ffmpeg] Correcting container in "TEST VIDEO-C0DPdy98e4c.m4a"
[ffmpeg] Destination: TEST VIDEO-C0DPdy98e4c.mp3
Deleting original file TEST VIDEO-C0DPdy98e4c.m4a (pass -k to keep)
2020-10-28 16:47:02.566841 # Downloading Metadata https://www.youtube.com/watch?v=C0DPdy98e4c complete
2020-10-28 16:47:02.566841 # Checking altarnative arguments
2020-10-28 16:47:02.566841 # Cutting video https://www.youtube.com/watch?v=C0DPdy98e4c from 5 to 7
2020-10-28 16:47:02.715639 # Cutting video https://www.youtube.com/watch?v=C0DPdy98e4c from 5 to 7 complete
2020-10-28 16:47:02.715639 # Renaming file to test
2020-10-28 16:47:02.717630 # Renaming file to test complete
为什么我不使用sys.stdout时,最后几条打印语句没有立即保存到文件中,而是正常打印出来?
【问题讨论】:
标签: python stdout youtube-dl