Skip to content

Commit d8cb9d8

Browse files
authored
Version 4.9.2 (#335)
* Fixing #334 machine now sleeps during encoding (thanks to Don Gafford) * Fixing After Conversion command running after every encoding (thanks to Don Gafford)
1 parent 1071726 commit d8cb9d8

File tree

6 files changed

+75
-64
lines changed

6 files changed

+75
-64
lines changed

CHANGES

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Version 4.9.2
4+
5+
* Fixing #334 machine now sleeps during encoding (thanks to Don Gafford)
6+
* Fixing After Conversion command running after every encoding (thanks to Don Gafford)
7+
38
## Version 4.9.1
49

510
* Fixing QSV AVC command builder not working (thanks to Marco Ravich)

fastflix/conversion_worker.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import logging
33
from pathlib import Path
44
from queue import Empty
5-
from typing import Optional
6-
from multiprocessing import Lock
75

86
import reusables
97
from appdirs import user_data_dir
@@ -12,48 +10,16 @@
1210
from fastflix.command_runner import BackgroundRunner
1311
from fastflix.language import t
1412
from fastflix.shared import file_date
15-
from fastflix.models.video import Video
16-
from fastflix.ff_queue import save_queue
1713

1814

1915
logger = logging.getLogger("fastflix-core")
2016

2117
log_path = Path(user_data_dir("FastFlix", appauthor=False, roaming=True)) / "logs"
22-
after_done_path = Path(user_data_dir("FastFlix", appauthor=False, roaming=True)) / "after_done_logs"
23-
24-
CONTINUOUS = 0x80000000
25-
SYSTEM_REQUIRED = 0x00000001
26-
27-
28-
def prevent_sleep_mode():
29-
"""https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx"""
30-
if reusables.win_based:
31-
import ctypes
32-
33-
try:
34-
ctypes.windll.kernel32.SetThreadExecutionState(CONTINUOUS | SYSTEM_REQUIRED)
35-
except Exception:
36-
logger.exception("Could not prevent system from possibly going to sleep during conversion")
37-
else:
38-
logger.debug("System has been asked to not sleep")
39-
40-
41-
def allow_sleep_mode():
42-
if reusables.win_based:
43-
import ctypes
44-
45-
try:
46-
ctypes.windll.kernel32.SetThreadExecutionState(CONTINUOUS)
47-
except Exception:
48-
logger.exception("Could not allow system to resume sleep mode")
49-
else:
50-
logger.debug("System has been allowed to enter sleep mode again")
5118

5219

5320
@reusables.log_exception(log="fastflix-core")
5421
def queue_worker(gui_proc, worker_queue, status_queue, log_queue):
5522
runner = BackgroundRunner(log_queue=log_queue)
56-
after_done_command = ""
5723
gui_died = False
5824
currently_encoding = False
5925
video_uuid = None
@@ -73,7 +39,6 @@ def start_command():
7339
encoding="utf-8",
7440
)
7541
logger.addHandler(new_file_handler)
76-
prevent_sleep_mode()
7742
currently_encoding = True
7843
runner.start_exec(
7944
command,
@@ -84,7 +49,6 @@ def start_command():
8449
if currently_encoding and not runner.is_alive():
8550
reusables.remove_file_handlers(logger)
8651
log_queue.put("STOP_TIMER")
87-
allow_sleep_mode()
8852
currently_encoding = False
8953

9054
if runner.error_detected:
@@ -96,13 +60,6 @@ def start_command():
9660
continue
9761

9862
status_queue.put(("complete", video_uuid, command_uuid))
99-
if after_done_command:
100-
logger.info(f"{t('Running after done command:')} {after_done_command}")
101-
try:
102-
runner.start_exec(after_done_command, str(after_done_path))
103-
except Exception:
104-
logger.exception("Error occurred while running after done command")
105-
continue
10663
if gui_died:
10764
return
10865

@@ -120,7 +77,6 @@ def start_command():
12077
continue
12178
except KeyboardInterrupt:
12279
status_queue.put(("exit",))
123-
allow_sleep_mode()
12480
return
12581
else:
12682
if request[0] == "execute":
@@ -131,17 +87,8 @@ def start_command():
13187
logger.debug(t("Cancel has been requested, killing encoding"))
13288
runner.kill()
13389
currently_encoding = False
134-
allow_sleep_mode()
13590
status_queue.put(("cancelled", video_uuid, command_uuid))
13691
log_queue.put("STOP_TIMER")
137-
video = None
138-
139-
if request[0] == "set after done":
140-
after_done_command = request[1]
141-
if after_done_command:
142-
logger.debug(f'{t("Setting after done command to:")} {after_done_command}')
143-
else:
144-
logger.debug(t("Removing after done command"))
14592

14693
if request[0] == "pause encode":
14794
logger.debug(t("Command worker received request to pause current encode"))

fastflix/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
__version__ = "4.9.1"
3+
__version__ = "4.9.2"
44
__author__ = "Chris Griffith"

fastflix/widgets/main.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
get_text_color,
4747
)
4848
from fastflix.shared import error_message, message, time_to_number, yes_no_message, clean_file_string
49-
from fastflix.windows_tools import show_windows_notification
49+
from fastflix.windows_tools import show_windows_notification, prevent_sleep_mode, allow_sleep_mode
5050
from fastflix.widgets.background_tasks import ThumbnailCreator
5151
from fastflix.widgets.progress_bar import ProgressBar, Task
5252
from fastflix.widgets.video_options import VideoOptions
@@ -1825,6 +1825,7 @@ def encode_video(self):
18251825
logger.debug(t("Starting conversion process"))
18261826

18271827
self.app.fastflix.currently_encoding = True
1828+
prevent_sleep_mode()
18281829
self.set_convert_button(False)
18291830
self.send_video_request_to_worker_queue(video_to_send)
18301831
self.disable_all()
@@ -1870,6 +1871,7 @@ def add_to_queue(self):
18701871
# @reusables.log_exception("fastflix", show_traceback=False)
18711872
def conversion_complete(self, success: bool):
18721873
self.paused = False
1874+
allow_sleep_mode()
18731875
self.set_convert_button()
18741876

18751877
if not success:
@@ -1890,6 +1892,7 @@ def conversion_complete(self, success: bool):
18901892
def conversion_cancelled(self, video: Video):
18911893
self.app.fastflix.worker_queue.put(Request("cancel"))
18921894
self.app.fastflix.currently_encoding = False
1895+
allow_sleep_mode()
18931896
self.set_convert_button()
18941897

18951898
exists = video.video_settings.output_path.exists()
@@ -1952,14 +1955,18 @@ def status_update(self, status_response):
19521955
video_to_send: Optional[Video] = None
19531956
errored = False
19541957
same_video = False
1958+
19551959
for video in self.app.fastflix.conversion_list:
19561960
if response.video_uuid == video.uuid:
19571961
video.status.running = False
1962+
19581963
if response.status == "cancelled":
19591964
video.status.cancelled = True
19601965
self.app.fastflix.currently_encoding = False
1966+
allow_sleep_mode()
19611967
self.video_options.update_queue()
19621968
return
1969+
19631970
if response.status == "complete":
19641971
video.status.current_command += 1
19651972
if len(video.video_settings.conversion_commands) > video.status.current_command:
@@ -1968,15 +1975,15 @@ def status_update(self, status_response):
19681975
break
19691976
else:
19701977
video.status.complete = True
1978+
19711979
if response.status == "error":
19721980
video.status.error = True
19731981
errored = True
19741982
break
19751983

19761984
if errored and not self.video_options.queue.ignore_errors.isChecked():
1977-
self.app.fastflix.currently_encoding = False
19781985
self.conversion_complete(success=False)
1979-
self.video_options.update_queue()
1986+
self.end_encoding()
19801987
return
19811988

19821989
if not video_to_send:
@@ -1987,32 +1994,37 @@ def status_update(self, status_response):
19871994
break
19881995

19891996
if not video_to_send:
1990-
self.app.fastflix.currently_encoding = False
19911997
self.conversion_complete(success=True)
1992-
self.video_options.update_queue()
1998+
self.end_encoding()
19931999
return
19942000

19952001
self.app.fastflix.currently_encoding = True
19962002
if not same_video and self.app.fastflix.conversion_paused:
1997-
self.app.fastflix.currently_encoding = False
1998-
self.video_options.update_queue()
1999-
return
2003+
return self.end_encoding()
20002004

20012005
self.send_video_request_to_worker_queue(video_to_send)
20022006

2007+
def end_encoding(self):
2008+
self.app.fastflix.currently_encoding = False
2009+
allow_sleep_mode()
2010+
self.video_options.queue.run_after_done()
2011+
self.video_options.update_queue()
2012+
20032013
def send_next_video(self) -> bool:
20042014
if not self.app.fastflix.currently_encoding:
20052015
for video in self.app.fastflix.conversion_list:
20062016
if video.status.ready:
20072017
video.status.running = True
20082018
self.send_video_request_to_worker_queue(video)
20092019
self.app.fastflix.currently_encoding = True
2020+
prevent_sleep_mode()
20102021
return True
20112022
return False
20122023

20132024
def send_video_request_to_worker_queue(self, video: Video):
20142025
command = video.video_settings.conversion_commands[video.status.current_command]
20152026
self.app.fastflix.currently_encoding = True
2027+
prevent_sleep_mode()
20162028

20172029
# logger.info(f"Sending video {video.uuid} command {command.uuid} called from {inspect.stack()}")
20182030

fastflix/widgets/panels/queue_panel.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
from pathlib import Path
99

10+
from appdirs import user_data_dir
1011
import reusables
1112
from box import Box
1213
from PySide2 import QtCore, QtGui, QtWidgets
@@ -19,6 +20,8 @@
1920
from fastflix.shared import no_border, open_folder, yes_no_message, message, error_message
2021
from fastflix.widgets.panels.abstract_list import FlixList
2122
from fastflix.exceptions import FastFlixInternalException
23+
from fastflix.windows_tools import allow_sleep_mode, prevent_sleep_mode
24+
from fastflix.command_runner import BackgroundRunner
2225

2326
logger = logging.getLogger("fastflix")
2427

@@ -38,6 +41,8 @@
3841
},
3942
}
4043

44+
after_done_path = Path(user_data_dir("FastFlix", appauthor=False, roaming=True)) / "after_done_logs"
45+
4146

4247
class EncodeItem(QtWidgets.QTabWidget):
4348
def __init__(self, parent, video: Video, index, first=False):
@@ -49,6 +54,7 @@ def __init__(self, parent, video: Video, index, first=False):
4954
self.last = False
5055
self.video = video.copy()
5156
self.setFixedHeight(60)
57+
self.after_done_action = None
5258

5359
self.widgets = Box(
5460
up_button=QtWidgets.QPushButton(
@@ -412,6 +418,7 @@ def pause_resume_queue(self):
412418

413419
def pause_resume_encode(self):
414420
if self.encode_paused:
421+
allow_sleep_mode()
415422
self.pause_encode.setText(t("Pause Encode"))
416423
self.pause_encode.setIcon(self.app.style().standardIcon(QtWidgets.QStyle.SP_MediaPause))
417424
self.app.fastflix.worker_queue.put(["resume encode"])
@@ -425,6 +432,7 @@ def pause_resume_encode(self):
425432
"Pause Warning",
426433
):
427434
return
435+
prevent_sleep_mode()
428436
self.pause_encode.setText(t("Resume Encode"))
429437
self.pause_encode.setIcon(self.app.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay))
430438
self.app.fastflix.worker_queue.put(["pause encode"])
@@ -441,15 +449,15 @@ def set_after_done(self):
441449
option = self.after_done_combo.currentText()
442450

443451
if option == "None":
444-
command = ""
452+
command = None
445453
elif option in self.app.fastflix.config.custom_after_run_scripts:
446454
command = self.app.fastflix.config.custom_after_run_scripts[option]
447455
elif reusables.win_based:
448456
command = done_actions["windows"][option]
449457
else:
450458
command = done_actions["linux"][option]
451459

452-
self.app.fastflix.worker_queue.put(["set after done", command])
460+
self.after_done_action = command
453461

454462
def retry_video(self, current_video):
455463

@@ -494,3 +502,11 @@ def add_to_queue(self):
494502
self.app.fastflix.conversion_list.append(copy.deepcopy(self.app.fastflix.current_video))
495503
self.new_source()
496504
save_queue(self.app.fastflix.conversion_list, self.app.fastflix.queue_path, self.app.fastflix.config)
505+
506+
def run_after_done(self):
507+
if not self.after_done_action:
508+
return
509+
logger.info(f"Running after done action: {self.after_done_action}")
510+
BackgroundRunner(self.app.fastflix.log_queue).start_exec(
511+
self.after_done_action, str(after_done_path), shell=True
512+
)

fastflix/windows_tools.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# -*- coding: utf-8 -*-
2+
import logging
23

4+
import reusables
5+
6+
logger = logging.getLogger("fastflix")
37

48
tool_window = None
59
tool_icon = None
10+
CONTINUOUS = 0x80000000
11+
SYSTEM_REQUIRED = 0x00000001
612

713

814
def show_windows_notification(title, msg, icon_path):
@@ -72,3 +78,28 @@ def cleanup_windows_notification():
7278
if tool_window:
7379
DestroyWindow(tool_window)
7480
UnregisterClass("FastFlix", None)
81+
82+
83+
def prevent_sleep_mode():
84+
"""https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx"""
85+
if reusables.win_based:
86+
import ctypes
87+
88+
try:
89+
ctypes.windll.kernel32.SetThreadExecutionState(CONTINUOUS | SYSTEM_REQUIRED)
90+
except Exception:
91+
logger.exception("Could not prevent system from possibly going to sleep during conversion")
92+
else:
93+
logger.debug("System has been asked to not sleep")
94+
95+
96+
def allow_sleep_mode():
97+
if reusables.win_based:
98+
import ctypes
99+
100+
try:
101+
ctypes.windll.kernel32.SetThreadExecutionState(CONTINUOUS)
102+
except Exception:
103+
logger.exception("Could not allow system to resume sleep mode")
104+
else:
105+
logger.debug("System has been allowed to enter sleep mode again")

0 commit comments

Comments
 (0)