98 lines
2.7 KiB
Python
98 lines
2.7 KiB
Python
|
import os
|
||
|
from urllib.parse import urlparse
|
||
|
import urllib
|
||
|
from unidecode import unidecode
|
||
|
import subprocess
|
||
|
import glob
|
||
|
import shutil
|
||
|
import emoji
|
||
|
import re
|
||
|
from functools import reduce
|
||
|
import time
|
||
|
|
||
|
LIMIT = 20
|
||
|
|
||
|
|
||
|
def extractURL():
|
||
|
with open("external_links.txt", "r") as file:
|
||
|
url_list = file.read().splitlines()
|
||
|
return url_list
|
||
|
|
||
|
|
||
|
def writeToText(text):
|
||
|
with open("converted_links.txt", "a", encoding="utf-8", errors="ignore") as f:
|
||
|
f.write(f"{text}\n")
|
||
|
|
||
|
|
||
|
def replaceString(string):
|
||
|
repls = (".", " "), ("()", "")
|
||
|
return reduce(lambda a, kv: a.replace(*kv), repls, string)
|
||
|
|
||
|
|
||
|
def removeYearString(string):
|
||
|
pattern = r"\d{4}"
|
||
|
return re.sub(pattern, "", string)
|
||
|
|
||
|
|
||
|
def checkURLConverted(value, status):
|
||
|
if status:
|
||
|
emojitype = emoji.emojize(":check_mark_button:")
|
||
|
text = f"{value} - Stato : {emojitype} Convertito"
|
||
|
else:
|
||
|
emojitype = emoji.emojize(":cross_mark:")
|
||
|
text = f"{value} - Stato : {emojitype} Non Convertito {emojitype}"
|
||
|
writeToText(text)
|
||
|
|
||
|
|
||
|
def convertMP4toHLSFFmpeg(url,folder,subfolder):
|
||
|
limitCounter = 1
|
||
|
if (limitCounter % LIMIT) == 0:
|
||
|
time.sleep(3600)
|
||
|
|
||
|
# Encode URL
|
||
|
encoded_url = urllib.parse.quote(url)
|
||
|
|
||
|
# Extract the filename from the URL
|
||
|
filename = os.path.basename(urlparse(encoded_url).path)
|
||
|
|
||
|
# Remove the file extension from the filename
|
||
|
extractFileName = os.path.splitext(filename)[0]
|
||
|
|
||
|
# Decode Folder in More Human Friendly Name
|
||
|
decode_filename = urllib.parse.unquote(extractFileName)
|
||
|
|
||
|
decode_filename = urllib.parse.unquote(decode_filename)
|
||
|
|
||
|
decode_filename = unidecode(decode_filename.replace(".", " "))
|
||
|
|
||
|
convertedFileName = decode_filename.replace(" ", "")
|
||
|
|
||
|
convertedFileName = removeYearString(convertedFileName)
|
||
|
|
||
|
convertedFileName = replaceString(convertedFileName)
|
||
|
|
||
|
try:
|
||
|
|
||
|
ffmpeg_command = f"ffmpeg -i {url} -c:v copy -c:a aac -start_number 0 -hls_time 10 -hls_list_size 0 -f hls '{convertedFileName}.m3u8'"
|
||
|
|
||
|
subprocess.call(ffmpeg_command, shell=True)
|
||
|
|
||
|
os.makedirs(folder)
|
||
|
|
||
|
os.makedirs(os.path.join(folder, subfolder))
|
||
|
|
||
|
ts_files = glob.glob(f"/var/www/{convertedFileName}*.ts")
|
||
|
m3u8_files = glob.glob(f"/var/www/{convertedFileName}*.m3u8")
|
||
|
vtt_files = glob.glob(f"/var/www/{convertedFileName}*.vtt")
|
||
|
all_files = ts_files + m3u8_files + vtt_files
|
||
|
for file in all_files:
|
||
|
shutil.move(file, subfolder)
|
||
|
checkURLConverted(folder, status=True)
|
||
|
limitCounter += 1
|
||
|
except Exception:
|
||
|
checkURLConverted(folder, status=False)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
convertMP4toHLSFFmpeg()
|