Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time-based Automated Conversion #342

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ ENV \
AUTOMATED_CONVERSION_HANDBRAKE_CUSTOM_ARGS= \
AUTOMATED_CONVERSION_INSTALL_PKGS= \
AUTOMATED_CONVERSION_NO_GUI_PROGRESS=0 \
AUTOMATED_CONVERSION_USE_TRASH=0
AUTOMATED_CONVERSION_USE_TRASH=0 \
AUTOMATED_CONVERSION_ALLOWED_TIME_RANGES=

# Define mountable directories.
VOLUME ["/storage"]
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ of this parameter has the format `<VARIABLE_NAME>=<VALUE>`.
|`AUTOMATED_CONVERSION_HANDBRAKE_CUSTOM_ARGS`| Custom arguments to pass to HandBrake when performing a conversion. | (no value) |
|`AUTOMATED_CONVERSION_INSTALL_PKGS`| Space-separated list of Alpine Linux packages to install. This is useful when the automatic video converter's hooks require tools not available in the container image. See https://pkgs.alpinelinux.org for the list of available Alpine Linux packages. | (no value) |
|`AUTOMATED_CONVERSION_USE_TRASH`| When set to `1`, the automatic video converter uses the trash directory. So when the automatic video converter is configured to *not* keep sources, it will move them to the trash directory (`/trash` inside the container) instead of deleting them. | `0` |
|`AUTOMATED_CONVERSION_ALLOWED_TIME_RANGES`| Comma-separated list of time ranges during which the automatic video converter is allowed to convert videos. See the [Time-based Automated Conversion](#time-based-automated-conversion) section for more details. | (no value) |

#### Deployment Considerations

Expand Down Expand Up @@ -752,6 +753,33 @@ subdirectory. For example, if `AUTOMATED_CONVERSION_OUTPUT_SUBDIR` is set to
host, `/home/user/appvolumes/HandBrake/TV Shows` should be monitored by the
application.

### Time-based Automated Conversion

The automatic video converter can be configured to convert videos only during a specific time range(s).
This is useful to prevent the automatic video converter from running during the night (or day), active hours, etc.

The time ranges are defined by the `AUTOMATED_CONVERSION_ALLOWED_TIME_RANGES` environment variable.

The value of this variable is a comma-separated list of time ranges.
The format of a single time range is `DAY-START_HOUR-END_HOUR`, where:
- `DAY` is a short name of the day of the week. It can be one of the following values: `Mon`, `Tue`, `Wed`, `Thu`, `Fri`, `Sat`, `Sun`.
- `START_HOUR` is the starting hour of the time range. It must be an integer between 0 and 23.
- `END_HOUR` is the ending hour of the time range. It must be an integer between 1 and 24. Always greater than `START_HOUR`. End hour is exclusive.

`START_HOUR` and `END_HOUR` are optional. If `END_HOUR` is omitted, it defaults to `START_HOUR + 1`.
If both `START_HOUR` and `END_HOUR` are omitted, the time range is set to the whole day (0-24).

Examples of valid values:
- `Tue-14-18`: Convert videos only on Tuesday, between 14:00 and 17:59.
- `Wed-9-17,Thu-9-17,Fri-9-17`: Convert videos only on Wednesday, Thursday and Friday, between 9:00 and 16:59.
- `Sun-1-2`: Convert videos only on Sunday, between 1:00 and 1:59. Equivalent to `Sun-1`.
- `Mon-0-24`: Convert videos only on Monday, at any hour. Equivalent to `Mon`.
- `Fri-18-24,Sat,Sun,Mon-0-6`: Convert videos from Friday 18:00 to Monday 5:59.

Keep in mind, if a video conversion starts within an allowed time range, and the conversion lasts longer than the time range, the conversion process will continue until it's finished.

**NOTE**: Don't forget to adjust the time zone of the container to avoid confusion.

## Intel Quick Sync Video

Intel Quick Sync Video is Intel's brand for its dedicated video encoding and
Expand Down
71 changes: 70 additions & 1 deletion rootfs/etc/services.d/autovideoconverter/run
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,70 @@ process_watch_folder() {
fi
}

is_now_within_time_range() {
allowed_time_ranges=$1

if [ -z "$allowed_time_ranges" ]; then
return 0
fi

# Example formats: Mon, Tue-3, Wed-12-15
range_pattern="^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)(-[0-9]{1,2})?(-[0-9]{1,2})?$"

day_now=$(LC_ALL=C date +%a) # Mon, Tue, Wed, etc.
hour_now=$(LC_ALL=C date +%k) # 0-23

IFS=',' read -r -a ranges_array <<< "$allowed_time_ranges"

for range in "${ranges_array[@]}"; do
if [[ $range =~ $range_pattern ]]; then
day=${BASH_REMATCH[1]}
# :1 removes the leading dash
start_hour=${BASH_REMATCH[2]:1}
end_hour=${BASH_REMATCH[3]:1}

# Mon turns into Mon-0-24
if [ -z "$start_hour" ]; then
start_hour=0
end_hour=24
fi

start_hour=$(echo "$start_hour" | bc) # Remove leading zeros

# Mon-12 turns into Mon-12-13
if [ -z "$end_hour" ]; then
end_hour=$((start_hour + 1))
fi

end_hour=$(echo "$end_hour" | bc) # Remove leading zeros

if [ "$start_hour" -lt 0 ] || [ "$start_hour" -gt 23 ]; then
echo "Invalid range: '$range', start hour must be from 0 to 23"
exit 1
fi

if [ "$end_hour" -lt 1 ] || [ "$end_hour" -gt 24 ]; then
echo "Invalid range: '$range', end hour must be from 1 to 24"
exit 1
fi

if [ "$start_hour" -ge "$end_hour" ]; then
echo "Invalid range: '$range', start hour must be less than end hour"
exit 1
fi

if [ "$day" == "$day_now" ] && [ "$hour_now" -ge "$start_hour" ] && [ "$hour_now" -lt "$end_hour" ]; then
return 0
fi
else
echo "Invalid 'allowed_time_ranges' format"
exit 1
fi
done

return 1
}

[ -f "$FAILED_CONVERSIONS" ] || touch "$FAILED_CONVERSIONS"
[ -f "$SUCCESSFUL_CONVERSIONS" ] || touch "$SUCCESSFUL_CONVERSIONS"

Expand All @@ -509,15 +573,20 @@ while true; do
AC_NO_GUI_PROGRESS="${AUTOMATED_CONVERSION_NO_GUI_PROGRESS:-0}"
AC_HANDBRAKE_CUSTOM_ARGS="${AUTOMATED_CONVERSION_HANDBRAKE_CUSTOM_ARGS:-}"
AC_OVERWRITE_OUTPUT="${AUTOMATED_CONVERSION_OVERWRITE_OUTPUT:-0}"
AC_ALLOWED_TIME_RANGES="${AUTOMATED_CONVERSION_ALLOWED_TIME_RANGES:-}"

# Apply per-watch folder settings.
if [ -n "${DIR#*/watch}" ]; then
for VAR in PRESET FORMAT SOURCE_STABLE_TIME SOURCE_MIN_DURATION SOURCE_MAIN_TITLE_DETECTION OUTPUT_DIR OUTPUT_SUBDIR KEEP_SOURCE VIDEO_FILE_EXTENSIONS NON_VIDEO_FILE_ACTION NON_VIDEO_FILE_EXTENSIONS NO_GUI_PROGRESS HANDBRAKE_CUSTOM_ARGS OVERWRITE_OUTPUT
for VAR in PRESET FORMAT SOURCE_STABLE_TIME SOURCE_MIN_DURATION SOURCE_MAIN_TITLE_DETECTION OUTPUT_DIR OUTPUT_SUBDIR KEEP_SOURCE VIDEO_FILE_EXTENSIONS NON_VIDEO_FILE_ACTION NON_VIDEO_FILE_EXTENSIONS NO_GUI_PROGRESS HANDBRAKE_CUSTOM_ARGS OVERWRITE_OUTPUT ALLOWED_TIME_RANGES
do
eval "AC_$VAR=\"\${AUTOMATED_CONVERSION_${VAR}_${DIR#*/watch}:-\$AC_$VAR}\""
done
fi

if ! is_now_within_time_range "$AC_ALLOWED_TIME_RANGES"; then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not the correct place to do the check. Doing it here means that the whole watch folder will be processed. The watch folder might contains a lot of videos and converting all of them might take multiple hours, going well over the defined range.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check should be done in process_watch_folder(), in the "Process video files" loop.

continue
fi

# Process watch folder.
process_watch_folder "$DIR"
done
Expand Down