Userbot / media_analyzer.py
mrwabnalas40's picture
Upload 105 files
40d06ea verified
raw
history blame
5.43 kB
import requests
from PIL import Image
import pytesseract
import io
import speech_recognition as sr
import os
import uuid
try:
import moviepy.editor as mp
MOVIEPY_AVAILABLE = True
except ImportError:
print("Warning: moviepy not available, video analysis will be limited")
MOVIEPY_AVAILABLE = False
def analyze_image_from_url(url):
try:
response = requests.get(url, timeout=10)
image = Image.open(io.BytesIO(response.content))
text = pytesseract.image_to_string(image, lang='eng+ara')
return text.strip() if text.strip() else "ู„ู… ูŠุชู… ุงู„ุนุซูˆุฑ ุนู„ู‰ ู†ุต ููŠ ุงู„ุตูˆุฑุฉ"
except Exception as e:
return f"ุฎุทุฃ ููŠ ุชุญู„ูŠู„ ุงู„ุตูˆุฑุฉ: {str(e)}"
def analyze_audio_from_url(url):
try:
audio_path = f"temp_{uuid.uuid4().hex}.mp3"
with open(audio_path, 'wb') as f:
f.write(requests.get(url, timeout=15).content)
recognizer = sr.Recognizer()
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data, language="ar")
os.remove(audio_path)
return text
except Exception as e:
return f"ุชุญู„ูŠู„ ุงู„ุตูˆุช ูุดู„: {str(e)}"
def analyze_video_from_url(url):
if not MOVIEPY_AVAILABLE:
return "ุชุญู„ูŠู„ ุงู„ููŠุฏูŠูˆ ุบูŠุฑ ู…ุชุงุญ ุญุงู„ูŠุงู‹"
try:
video_path = f"temp_{uuid.uuid4().hex}.mp4"
audio_path = f"temp_{uuid.uuid4().hex}.wav"
with open(video_path, 'wb') as f:
f.write(requests.get(url, timeout=20).content)
clip = mp.VideoFileClip(video_path)
clip.audio.write_audiofile(audio_path, codec='pcm_s16le')
recognizer = sr.Recognizer()
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data, language="ar")
os.remove(video_path)
os.remove(audio_path)
return text
except Exception as e:
return f"ุชุญู„ูŠู„ ุงู„ููŠุฏูŠูˆ ูุดู„: {str(e)}"
import requests
from PIL import Image
import pytesseract
import io
import speech_recognition as sr
import os
import uuid
import logging
# ุฅุนุฏุงุฏ ุณุฌู„ ุงู„ุฃุฎุทุงุก
logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s')
# ุงู„ุชุญู‚ู‚ ู…ู† ุชูˆูุฑ ู…ูƒุชุจุฉ moviepy
try:
import moviepy.editor as mp
MOVIEPY_AVAILABLE = True
except ImportError:
logging.warning("moviepy ุบูŠุฑ ู…ุชูˆูุฑุฉุŒ ุณูŠุชู… ุชุนุทูŠู„ ุชุญู„ูŠู„ ุงู„ููŠุฏูŠูˆ")
MOVIEPY_AVAILABLE = False
class MediaAnalyzer:
def __init__(self, lang='eng+ara'):
self.lang = lang
def _download_file(self, url, extension):
try:
response = requests.get(url, timeout=20)
response.raise_for_status()
file_path = f"temp_{uuid.uuid4().hex}.{extension}"
with open(file_path, 'wb') as f:
f.write(response.content)
return file_path
except Exception as e:
logging.error(f"ูุดู„ ุชุญู…ูŠู„ ุงู„ู…ู„ู: {e}")
return None
def analyze_image_from_url(self, url):
try:
response = requests.get(url, timeout=10)
image = Image.open(io.BytesIO(response.content))
text = pytesseract.image_to_string(image, lang=self.lang)
return text.strip() if text.strip() else "ู„ู… ูŠุชู… ุงู„ุนุซูˆุฑ ุนู„ู‰ ู†ุต ููŠ ุงู„ุตูˆุฑุฉ"
except Exception as e:
logging.error(f"ุฎุทุฃ ููŠ ุชุญู„ูŠู„ ุงู„ุตูˆุฑุฉ: {e}")
return f"ุฎุทุฃ ููŠ ุชุญู„ูŠู„ ุงู„ุตูˆุฑุฉ: {str(e)}"
def analyze_audio_from_url(self, url):
audio_path = self._download_file(url, "mp3")
if not audio_path:
return "ูุดู„ ุชุญู…ูŠู„ ุงู„ุตูˆุช"
try:
recognizer = sr.Recognizer()
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data, language="ar")
return text
except Exception as e:
logging.error(f"ุชุญู„ูŠู„ ุงู„ุตูˆุช ูุดู„: {e}")
return f"ุชุญู„ูŠู„ ุงู„ุตูˆุช ูุดู„: {str(e)}"
finally:
if os.path.exists(audio_path):
os.remove(audio_path)
def analyze_video_from_url(self, url):
if not MOVIEPY_AVAILABLE:
return "ุชุญู„ูŠู„ ุงู„ููŠุฏูŠูˆ ุบูŠุฑ ู…ุชุงุญ ุญุงู„ูŠุงู‹"
video_path = self._download_file(url, "mp4")
if not video_path:
return "ูุดู„ ุชุญู…ูŠู„ ุงู„ููŠุฏูŠูˆ"
audio_path = f"temp_{uuid.uuid4().hex}.wav"
try:
with mp.VideoFileClip(video_path) as clip:
clip.audio.write_audiofile(audio_path, codec='pcm_s16le')
recognizer = sr.Recognizer()
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data, language="ar")
return text
except Exception as e:
logging.error(f"ุชุญู„ูŠู„ ุงู„ููŠุฏูŠูˆ ูุดู„: {e}")
return f"ุชุญู„ูŠู„ ุงู„ููŠุฏูŠูˆ ูุดู„: {str(e)}"
finally:
for path in [video_path, audio_path]:
if os.path.exists(path):
os.remove(path)