-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression.py
More file actions
154 lines (122 loc) · 6.75 KB
/
compression.py
File metadata and controls
154 lines (122 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- encoding: utf-8 -*-
import os
import logging
import csv
import platform
from jproperties import Properties
import subprocess
import time
from datetime import timedelta
import telegram
configs = Properties()
with open('local.properties', 'rb') as read_prop:
configs.load(read_prop)
prop_view = configs.items()
if platform.system() == 'Windows':
TORRENT_PATH = configs.get("torrent_path_win").data
if platform.system() == 'Darwin':
TORRENT_PATH = configs.get("torrent_path_mac").data
if platform.system() == 'Linux':
TORRENT_PATH = configs.get("torrent_path_unix").data
# Настройки ffmpeg
num_processors = 4
memory = 10
codec = 'libx264' # Нужный нам кодек. Увидеть все введите в косоли ffmpeg -codecs рабочий: libx265, libx264, hevc h264_nvenc
qscale = 1 # Значение 1 соответствует самому лучшему качеству, а 31 — самому худшему.
format_video = 'mkv' # поменять контейнер c формат m4v на mkv
crf = 24 # Cтепень сжатия. Чем меньше число, тем лучше качество, но больше размер.
input_path = os.path.join(TORRENT_PATH, configs.get("torrent_renamed_dirname").data)
output_path = os.path.join(TORRENT_PATH, configs.get("torrent_renamed_dirname").data)
CSV_SAVE_PATH = os.path.join(TORRENT_PATH, configs.get("torrent_renamed_dirname").data, "compress_video.csv")
# настройка логирования
log_path = os.path.join(TORRENT_PATH, configs.get("torrent_renamed_dirname").data, "my_log_file.log")
logging.basicConfig(filename=log_path, level=logging.ERROR)
# Настройки бота для телеграмм
telegram_bot_token = 'telegram_bot_token'
telegram_chat_id = 'telegram_chat_id'
# Проверьте, является ли файл видеофайлом
def is_video_file(filename):
video_file_extensions = [".mp4", ".avi", ".mkv", ".mov", ".wmv"]
return any(filename.endswith(extension) for extension in video_file_extensions)
# Сжать видео файл
def compress_video(filename):
input_file = os.path.join(input_path, filename)
# output_file = os.path.join(output_path, filename)
output_folder = os.path.join(output_path, os.path.splitext(filename)[0])
file_name_path = f"{output_folder}/compressed-{filename}"
if not os.path.exists(input_file):
print(f"Error: Input file {input_file} does not exist")
return filename, 0, 0
# Проверьте, является ли входной файл видеофайлом
if not is_video_file(filename):
return filename, 0, 0
# Создайте выходную папку, если она не существует
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Получаем размер исходного файла
input_size = round(os.path.getsize(input_file) / (10 ** 9), 3)
# Старт времени начало сжатия
start_time = time.time()
# Сжать видеофайл с помощью указанного кодека -an -map 0:a -g 30
cmd = f"ffmpeg -i \"{input_file}\" -c:v {codec} -c:a copy -threads {num_processors} -crf {crf} -q:v {qscale} -y \"{file_name_path}\""
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Конец измерения времени сжатия
end_time = time.time()
# Расчет времени сжатия
compression_time = end_time - start_time
# Запишем данные в формате ЧЧ:ММ:СС
time_in_hhmmss = str(timedelta(seconds=compression_time)).split(".")[0]
print(time_in_hhmmss)
# Проверьте наличие ошибок во время сжатия
if result.returncode != 0:
print(f"Error: Failed to compress {filename}")
print(result.stderr.decode())
return filename, 0, 0, time_in_hhmmss
# Получаем размер выходных файлов
output_size = round(os.path.getsize(file_name_path) / (10 ** 9), 3)
# Удалить исходный файл
# os.remove(input_file)
return filename, input_size, output_size, time_in_hhmmss
async def send_telegram_notification(filename, time_in_hhmmss):
try:
bot = telegram.Bot(token=telegram_bot_token)
time_delta = timedelta(hours=0, minutes=0, seconds=0)
time_list = time_in_hhmmss.split(":")
if len(time_list) == 3:
time_delta = timedelta(hours=int(time_list[0]), minutes=int(time_list[1]), seconds=int(time_list[2]))
elif len(time_list) == 2:
time_delta = timedelta(hours=0, minutes=int(time_list[0]), seconds=int(time_list[1]))
elif len(time_list) == 1:
time_delta = timedelta(hours=0, minutes=0, seconds=int(time_list[0]))
else:
raise ValueError(f"Invalid time format: {time_in_hhmmss}")
total_seconds = time_delta.total_seconds()
message = f"Видео {filename} было успешно сжато! Время сжатия: {str(timedelta(seconds=total_seconds))}"
await bot.send_message(chat_id=telegram_chat_id, text=message)
except Exception as e:
logging.error(f"Error occurred while sending telegram notification for file {filename}: {str(e)}")
# Сохраните информацию о сжатом видео в файл CSV.
def save_to_csv(data):
try:
with open(CSV_SAVE_PATH, "a", encoding="UTF-8", newline="") as csvfile:
writer = csv.writer(csvfile, delimiter=',')
# Проверьте, существует ли файл CSV
if os.path.exists(CSV_SAVE_PATH) and os.path.getsize(CSV_SAVE_PATH) == 0:
writer.writerow(["Filename", "Input Size (GB)", "Output Size (GB)", "Compression Time"])
writer.writerows(data)
# csvfile.close()
except Exception as e:
print(f"Error: Failed to write to CSV file {CSV_SAVE_PATH}. Error: {e}")
if __name__ == "__main__":
import asyncio
# Получить список всех видеофайлов во входном пути
video_files = [file for file in os.listdir(input_path) if is_video_file(file)]
compressed_video_data = []
# Сжать каждый видеофайл
for file in video_files:
filename, input_size, output_size, time_in_hhmmss = compress_video(file)
compressed_video_data.append([filename, input_size, output_size, time_in_hhmmss])
# Сохраните сжатые видеоданные в файл CSV.
save_to_csv(compressed_video_data)
asyncio.run(send_telegram_notification(filename, time_in_hhmmss))
print("Video compression and saving to CSV file completed.")