本文整理汇总了Python中babelfish.Language.fromname方法的典型用法代码示例。如果您正苦于以下问题:Python Language.fromname方法的具体用法?Python Language.fromname怎么用?Python Language.fromname使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类babelfish.Language
的用法示例。
在下文中一共展示了Language.fromname方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_converter_name
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
def test_converter_name(self):
self.assertEqual(Language('eng').name, 'English')
self.assertEqual(Language.fromname('English'), Language('eng'))
self.assertEqual(Language.fromcode('English', 'name'), Language('eng'))
with self.assertRaises(LanguageReverseError):
Language.fromname('Zzzzzzzzz')
self.assertEqual(len(get_language_converter('name').codes), 7874)
示例2: getEmbeddedLanguages
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
def getEmbeddedLanguages(video_path):
embedded_subtitle_languages = set()
try:
with open(video_path, 'rb') as f:
mkv = MKV(f)
if mkv.subtitle_tracks:
for st in mkv.subtitle_tracks:
if st.language:
try:
embedded_subtitle_languages.add(Language.fromalpha3b(st.language))
except BabelfishError:
logger.log('Embedded subtitle track is not a valid language', logger.DEBUG)
embedded_subtitle_languages.add(Language('und'))
elif st.name:
try:
embedded_subtitle_languages.add(Language.fromname(st.name))
except BabelfishError:
logger.log('Embedded subtitle track is not a valid language', logger.DEBUG)
embedded_subtitle_languages.add(Language('und'))
else:
embedded_subtitle_languages.add(Language('und'))
else:
logger.log('MKV has no subtitle track', logger.DEBUG)
except MalformedMKVError:
logger.log('MKV seems to be malformed, ignoring embedded subtitles', logger.WARNING)
return embedded_subtitle_languages
示例3: subtitlesLanguages
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
def subtitlesLanguages(video_path):
"""Return a list detected subtitles for the given video file"""
resultList = []
embedded_subtitle_languages = set()
# Serch for embedded subtitles
if not sickbeard.EMBEDDED_SUBTITLES_ALL:
if video_path.endswith('mkv'):
try:
with open(video_path.encode(sickbeard.SYS_ENCODING), 'rb') as f:
mkv = MKV(f)
if mkv.subtitle_tracks:
for st in mkv.subtitle_tracks:
if st.language:
try:
embedded_subtitle_languages.add(Language.fromalpha3b(st.language))
except BabelfishError:
logger.log('Embedded subtitle track is not a valid language', logger.DEBUG)
embedded_subtitle_languages.add(Language('und'))
elif st.name:
try:
embedded_subtitle_languages.add(Language.fromname(st.name))
except BabelfishError:
logger.log('Embedded subtitle track is not a valid language', logger.DEBUG)
embedded_subtitle_languages.add(Language('und'))
else:
embedded_subtitle_languages.add(Language('und'))
else:
logger.log('MKV has no subtitle track', logger.DEBUG)
except MalformedMKVError:
logger.log('MKV seems to be malformed, ignoring embedded subtitles', logger.WARNING)
# Search subtitles in the absolute path
if sickbeard.SUBTITLES_DIR and ek(os.path.exists, sickbeard.SUBTITLES_DIR):
video_path = ek(os.path.join, sickbeard.SUBTITLES_DIR, ek(os.path.basename, video_path))
# Search subtitles in the relative path
elif sickbeard.SUBTITLES_DIR:
video_path = ek(os.path.join, ek(os.path.dirname, video_path), sickbeard.SUBTITLES_DIR, ek(os.path.basename, video_path))
external_subtitle_languages = subliminal.video.scan_subtitle_languages(video_path)
subtitle_languages = external_subtitle_languages.union(embedded_subtitle_languages)
if (len(subtitle_languages) is 1 and len(wantedLanguages()) is 1) and Language('und') in subtitle_languages:
subtitle_languages.remove(Language('und'))
subtitle_languages.add(fromietf(wantedLanguages()[0]))
for language in subtitle_languages:
if hasattr(language, 'opensubtitles') and language.opensubtitles:
resultList.append(language.opensubtitles)
elif hasattr(language, 'alpha3') and language.alpha3:
resultList.append(language.alpha3)
elif hasattr(language, 'alpha2') and language.alpha2:
resultList.append(language.alpha2)
defaultLang = wantedLanguages()
if ('pob' in defaultLang or 'pb' in defaultLang) and ('pt' not in defaultLang and 'por' not in defaultLang):
resultList = [x if not x in ['por', 'pt'] else u'pob' for x in resultList]
return sorted(resultList)
示例4: _get_embedded_subtitles
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
def _get_embedded_subtitles(dirname, filename):
"""
Based on subliminal.video.scan_video(...) but only keep the check for embedded subtitles
"""
log.debug("Checking for embedded subtitle")
embedded_subtitle_languages = set()
try:
path = os.path.join(dirname, filename)
if filename.endswith('.mkv'):
with open(path, 'rb') as f:
mkv = MKV(f)
# subtitle tracks
if mkv.subtitle_tracks:
for st in mkv.subtitle_tracks:
if st.language:
try:
embedded_subtitle_languages.add(Language.fromalpha3b(st.language))
except BabelfishError:
log.error('Embedded subtitle track language %r is not a valid language', st.language)
embedded_subtitle_languages.add(Language('und'))
elif st.name:
try:
embedded_subtitle_languages.add(Language.fromname(st.name))
except BabelfishError:
log.debug('Embedded subtitle track name %r is not a valid language', st.name)
embedded_subtitle_languages.add(Language('und'))
else:
embedded_subtitle_languages.add(Language('und'))
log.debug('Found embedded subtitles %r with enzyme', embedded_subtitle_languages)
else:
log.debug('MKV has no subtitle track')
except:
log.exception('Parsing video metadata with enzyme failed')
return embedded_subtitle_languages
示例5: getEmbeddedLanguages
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
def getEmbeddedLanguages(video_path):
embedded_subtitle_languages = set()
try:
with ek(io.open, video_path, 'rb') as f:
mkv = MKV(f)
if mkv.subtitle_tracks:
for st in mkv.subtitle_tracks:
if st.language:
try:
embedded_subtitle_languages.add(Language.fromalpha3b(st.language))
except BabelfishError:
logging.debug('Embedded subtitle track is not a valid language')
embedded_subtitle_languages.add(Language('und'))
elif st.name:
try:
embedded_subtitle_languages.add(Language.fromname(st.name))
except BabelfishError:
logging.debug('Embedded subtitle track is not a valid language')
embedded_subtitle_languages.add(Language('und'))
else:
embedded_subtitle_languages.add(Language('und'))
else:
logging.debug('MKV has no subtitle track')
except MalformedMKVError:
logging.info('MKV seems to be malformed ( %s ), ignoring embedded subtitles' % video_path)
return embedded_subtitle_languages
示例6: has_subtitle
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
def has_subtitle(self, language):
""" Returns true if the video has already
a subtitle for a specific language. """
has_subtitle = False
# Look for embedded subtitle in mkv video
if Video.is_mkv(self.signature):
with open(self.filename, 'rb') as file_handler:
mkv_video = enzyme.MKV(file_handler)
for sub in mkv_video.subtitle_tracks:
try:
if sub.language and \
Language.fromalpha3b(sub.language) == language:
has_subtitle = True
break
elif sub.name and \
Language.fromname(sub.name) == language:
has_subtitle = True
break
except BabelfishError:
LOG.error(
"Embedded subtitle track"
"language {} is not a valid language"
.format(sub.language))
# Look for external subtitle
dir_name = os.path.dirname(self.filename)
base_name, _ = os.path.splitext(os.path.basename(self.filename))
search_subtitle = os.path.join(
dir_name, "{}.{}.*".format(base_name, language.alpha2))
existing_subtitles = [
sub_file for sub_file in glob.glob(search_subtitle)
if os.path.splitext(sub_file)[1] in Subtitle.EXTENSIONS
]
if existing_subtitles:
has_subtitle = True
return has_subtitle
示例7: get_embedded_subtitles
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
def get_embedded_subtitles(dirname, filename, log_scan=False):
"""Get the embedded subtitle languages for a video file.
Based on subliminal.video.scan_video(...) but only keep the check for embedded subtitles.
"""
log.debug('Checking for embedded subtitle(s)')
embedded_subtitle_languages = set()
path = os.path.join(dirname, filename)
try:
if filename.endswith('.mkv'):
with open(path, mode='rb') as f:
mkv = MKV(f)
# subtitle tracks
if mkv.subtitle_tracks:
for st in mkv.subtitle_tracks:
if st.language:
try:
embedded_subtitle_languages.add(Language.fromalpha3b(st.language))
except BabelfishError:
log_scan and log.error('Embedded subtitle track language %r is not a valid language',
st.language)
elif st.name:
try:
embedded_subtitle_languages.add(Language.fromname(st.name))
except BabelfishError:
log_scan and log.error('Embedded subtitle track name %r is not a valid language', st.name)
else:
log_scan and log.error('Embedded subtitle track language %r is not a valid language',
st.language)
log_scan and log.debug('Found embedded subtitles %r with enzyme', embedded_subtitle_languages)
else:
log_scan and log.debug('MKV has no subtitle track')
else:
log_scan and log.debug('Check is only supported for MKV containers, skipping')
except Exception:
log_scan and log.error('Parsing video metadata with enzyme failed')
return [Subtitle(EMBEDDED, text_type(language), path) for language in embedded_subtitle_languages if
language != Language('und')]
示例8: getLangList
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
def getLangList(self):
l = {Language.fromietf(Prefs["langPref1"])}
langCustom = Prefs["langPrefCustom"].strip()
if Prefs["langPref2"] != "None":
l.update({Language.fromietf(Prefs["langPref2"])})
if Prefs["langPref3"] != "None":
l.update({Language.fromietf(Prefs["langPref3"])})
if len(langCustom) and langCustom != "None":
for lang in langCustom.split(u","):
lang = lang.strip()
try:
real_lang = Language.fromietf(lang)
except:
try:
real_lang = Language.fromname(lang)
except:
continue
l.update({real_lang})
return l
示例9: test_converter_name
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
def test_converter_name(self):
self.assertEqual(Language('eng').name, 'English')
self.assertEqual(Language.fromname('English'), Language('eng'))
self.assertEqual(Language.fromcode('English', 'name'), Language('eng'))
self.assertRaises(LanguageReverseError, lambda: Language.fromname('Zzzzzzzzz'))
self.assertEqual(len(language_converters['name'].codes), 7874)
示例10: scan_video
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
#.........这里部分代码省略.........
# check video extension
if not path.endswith(VIDEO_EXTENSIONS):
raise ValueError('%s is not a valid video extension' % os.path.splitext(path)[1])
dirpath, filename = os.path.split(path)
logger.info('Scanning video %r in %r', filename, dirpath)
# guess
video = Video.fromguess(path, guessit(path))
# size and hashes
video.size = os.path.getsize(path)
if video.size > 10485760:
logger.debug('Size is %d', video.size)
video.hashes['opensubtitles'] = hash_opensubtitles(path)
video.hashes['thesubdb'] = hash_thesubdb(path)
video.hashes['napiprojekt'] = hash_napiprojekt(path)
logger.debug('Computed hashes %r', video.hashes)
else:
logger.warning('Size is lower than 10MB: hashes not computed')
# external subtitles
if subtitles:
video.subtitle_languages |= set(search_external_subtitles(path, directory=subtitles_dir).values())
# video metadata with enzyme
try:
if filename.endswith('.mkv'):
with open(path, 'rb') as f:
mkv = MKV(f)
# main video track
if mkv.video_tracks:
video_track = mkv.video_tracks[0]
# resolution
if video_track.height in (480, 720, 1080):
if video_track.interlaced:
video.resolution = '%di' % video_track.height
else:
video.resolution = '%dp' % video_track.height
logger.debug('Found resolution %s with enzyme', video.resolution)
# video codec
if video_track.codec_id == 'V_MPEG4/ISO/AVC':
video.video_codec = 'h264'
logger.debug('Found video_codec %s with enzyme', video.video_codec)
elif video_track.codec_id == 'V_MPEG4/ISO/SP':
video.video_codec = 'DivX'
logger.debug('Found video_codec %s with enzyme', video.video_codec)
elif video_track.codec_id == 'V_MPEG4/ISO/ASP':
video.video_codec = 'XviD'
logger.debug('Found video_codec %s with enzyme', video.video_codec)
else:
logger.warning('MKV has no video track')
# main audio track
if mkv.audio_tracks:
audio_track = mkv.audio_tracks[0]
# audio codec
if audio_track.codec_id == 'A_AC3':
video.audio_codec = 'AC3'
logger.debug('Found audio_codec %s with enzyme', video.audio_codec)
elif audio_track.codec_id == 'A_DTS':
video.audio_codec = 'DTS'
logger.debug('Found audio_codec %s with enzyme', video.audio_codec)
elif audio_track.codec_id == 'A_AAC':
video.audio_codec = 'AAC'
logger.debug('Found audio_codec %s with enzyme', video.audio_codec)
else:
logger.warning('MKV has no audio track')
# subtitle tracks
if mkv.subtitle_tracks:
if embedded_subtitles:
embedded_subtitle_languages = set()
for st in mkv.subtitle_tracks:
if st.language:
try:
embedded_subtitle_languages.add(Language.fromalpha3b(st.language))
except BabelfishError:
logger.error('Embedded subtitle track language %r is not a valid language', st.language)
embedded_subtitle_languages.add(Language('und'))
elif st.name:
try:
embedded_subtitle_languages.add(Language.fromname(st.name))
except BabelfishError:
logger.debug('Embedded subtitle track name %r is not a valid language', st.name)
embedded_subtitle_languages.add(Language('und'))
else:
embedded_subtitle_languages.add(Language('und'))
logger.debug('Found embedded subtitle %r with enzyme', embedded_subtitle_languages)
video.subtitle_languages |= embedded_subtitle_languages
else:
logger.debug('MKV has no subtitle track')
except:
logger.exception('Parsing video metadata with enzyme failed')
return video
示例11: refine
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
def refine(video, embedded_subtitles=True, **kwargs):
"""Refine a video by searching its metadata.
Several :class:`~subliminal.video.Video` attributes can be found:
* :attr:`~subliminal.video.Video.resolution`
* :attr:`~subliminal.video.Video.video_codec`
* :attr:`~subliminal.video.Video.audio_codec`
* :attr:`~subliminal.video.Video.subtitle_languages`
:param bool embedded_subtitles: search for embedded subtitles.
"""
# skip non existing videos
if not video.exists:
return
# check extensions
extension = os.path.splitext(video.name)[1]
if extension == '.mkv':
with open(video.name, 'rb') as f:
mkv = MKV(f)
# main video track
if mkv.video_tracks:
video_track = mkv.video_tracks[0]
# resolution
if video_track.height in (480, 720, 1080):
if video_track.interlaced:
video.resolution = '%di' % video_track.height
else:
video.resolution = '%dp' % video_track.height
logger.debug('Found resolution %s', video.resolution)
# video codec
if video_track.codec_id == 'V_MPEG4/ISO/AVC':
video.video_codec = 'H.264'
logger.debug('Found video_codec %s', video.video_codec)
elif video_track.codec_id == 'V_MPEG4/ISO/SP':
video.video_codec = 'DivX'
logger.debug('Found video_codec %s', video.video_codec)
elif video_track.codec_id == 'V_MPEG4/ISO/ASP':
video.video_codec = 'Xvid'
logger.debug('Found video_codec %s', video.video_codec)
else:
logger.warning('MKV has no video track')
# main audio track
if mkv.audio_tracks:
audio_track = mkv.audio_tracks[0]
# audio codec
if audio_track.codec_id == 'A_AC3':
video.audio_codec = 'Dolby Digital'
logger.debug('Found audio_codec %s', video.audio_codec)
elif audio_track.codec_id == 'A_DTS':
video.audio_codec = 'DTS'
logger.debug('Found audio_codec %s', video.audio_codec)
elif audio_track.codec_id == 'A_AAC':
video.audio_codec = 'AAC'
logger.debug('Found audio_codec %s', video.audio_codec)
else:
logger.warning('MKV has no audio track')
# subtitle tracks
if mkv.subtitle_tracks:
if embedded_subtitles:
embedded_subtitle_languages = set()
for st in mkv.subtitle_tracks:
if st.language:
try:
embedded_subtitle_languages.add(Language.fromalpha3b(st.language))
except BabelfishError:
logger.error('Embedded subtitle track language %r is not a valid language', st.language)
embedded_subtitle_languages.add(Language('und'))
elif st.name:
try:
embedded_subtitle_languages.add(Language.fromname(st.name))
except BabelfishError:
logger.debug('Embedded subtitle track name %r is not a valid language', st.name)
embedded_subtitle_languages.add(Language('und'))
else:
embedded_subtitle_languages.add(Language('und'))
logger.debug('Found embedded subtitle %r', embedded_subtitle_languages)
video.subtitle_languages |= embedded_subtitle_languages
else:
logger.debug('MKV has no subtitle track')
else:
logger.debug('Unsupported video extension %s', extension)
示例12: scan_video
# 需要导入模块: from babelfish import Language [as 别名]
# 或者: from babelfish.Language import fromname [as 别名]
#.........这里部分代码省略.........
# check video extension
if not path.endswith(VIDEO_EXTENSIONS):
raise ValueError("%s is not a valid video extension" % os.path.splitext(path)[1])
dirpath, filename = os.path.split(path)
logger.info("Scanning video %r in %r", filename, dirpath)
# guess
video = Video.fromguess(path, guess_file_info(path))
# size and hashes
video.size = os.path.getsize(path)
if video.size > 10485760:
logger.debug("Size is %d", video.size)
video.hashes["opensubtitles"] = hash_opensubtitles(path)
video.hashes["thesubdb"] = hash_thesubdb(path)
video.hashes["napiprojekt"] = hash_napiprojekt(path)
logger.debug("Computed hashes %r", video.hashes)
else:
logger.warning("Size is lower than 10MB: hashes not computed")
# external subtitles
if subtitles:
video.subtitle_languages |= set(search_external_subtitles(path, directory=subtitles_dir).values())
# video metadata with enzyme
try:
if filename.endswith(".mkv"):
with open(path, "rb") as f:
mkv = MKV(f)
# main video track
if mkv.video_tracks:
video_track = mkv.video_tracks[0]
# resolution
if video_track.height in (480, 720, 1080):
if video_track.interlaced:
video.resolution = "%di" % video_track.height
else:
video.resolution = "%dp" % video_track.height
logger.debug("Found resolution %s with enzyme", video.resolution)
# video codec
if video_track.codec_id == "V_MPEG4/ISO/AVC":
video.video_codec = "h264"
logger.debug("Found video_codec %s with enzyme", video.video_codec)
elif video_track.codec_id == "V_MPEG4/ISO/SP":
video.video_codec = "DivX"
logger.debug("Found video_codec %s with enzyme", video.video_codec)
elif video_track.codec_id == "V_MPEG4/ISO/ASP":
video.video_codec = "XviD"
logger.debug("Found video_codec %s with enzyme", video.video_codec)
else:
logger.warning("MKV has no video track")
# main audio track
if mkv.audio_tracks:
audio_track = mkv.audio_tracks[0]
# audio codec
if audio_track.codec_id == "A_AC3":
video.audio_codec = "AC3"
logger.debug("Found audio_codec %s with enzyme", video.audio_codec)
elif audio_track.codec_id == "A_DTS":
video.audio_codec = "DTS"
logger.debug("Found audio_codec %s with enzyme", video.audio_codec)
elif audio_track.codec_id == "A_AAC":
video.audio_codec = "AAC"
logger.debug("Found audio_codec %s with enzyme", video.audio_codec)
else:
logger.warning("MKV has no audio track")
# subtitle tracks
if mkv.subtitle_tracks:
if embedded_subtitles:
embedded_subtitle_languages = set()
for st in mkv.subtitle_tracks:
if st.language:
try:
embedded_subtitle_languages.add(Language.fromalpha3b(st.language))
except BabelfishError:
logger.error("Embedded subtitle track language %r is not a valid language", st.language)
embedded_subtitle_languages.add(Language("und"))
elif st.name:
try:
embedded_subtitle_languages.add(Language.fromname(st.name))
except BabelfishError:
logger.debug("Embedded subtitle track name %r is not a valid language", st.name)
embedded_subtitle_languages.add(Language("und"))
else:
embedded_subtitle_languages.add(Language("und"))
logger.debug("Found embedded subtitle %r with enzyme", embedded_subtitle_languages)
video.subtitle_languages |= embedded_subtitle_languages
else:
logger.debug("MKV has no subtitle track")
except:
logger.exception("Parsing video metadata with enzyme failed")
return video