Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
srt2ass [Le 15/12/2011, 15:22] 127.0.0.1 modification externe |
srt2ass [Le 06/09/2012, 12:11] (Version actuelle) Crazyfaboo Amélioration du script, correction de bugs, liste d'exclusions. |
||
---|---|---|---|
Ligne 27: | Ligne 27: | ||
</code> | </code> | ||
+ | <note>note : il est en fait possible de mettre du texte en italique mot par mot via les balise {\i1} et {\i0}</note> | ||
====== Conversion en ligne de commande ====== | ====== Conversion en ligne de commande ====== | ||
Ligne 47: | Ligne 48: | ||
import codecs | import codecs | ||
import argparse | import argparse | ||
- | import os, glob | + | import sys, os, glob |
+ | |||
+ | # Use this list to delete automatically subs containing one of the following words. All in lower case. | ||
+ | exclusion = ["subtitles by", "sync by", "<font", "synchro:", "sync:", "transcript:", "subtitles:", "subtitle:", "sync and corrections"] | ||
class Console: | class Console: | ||
Ligne 53: | Ligne 57: | ||
| | ||
@classmethod | @classmethod | ||
- | def l(c, message): | + | def l(c, message, linefeed = True): |
- | print message | + | sys.stdout.write(message) |
+ | if linefeed: | ||
+ | sys.stdout.write("\n") | ||
| | ||
@classmethod | @classmethod | ||
- | def v(c, message): | + | def v(c, message, linefeed = True): |
if c.verbose: | if c.verbose: | ||
- | c.l(message) | + | c.l(message, linefeed) |
| | ||
@classmethod | @classmethod | ||
- | def w(c, message): | + | def w(c, message, linefeed = True): |
- | c.v("## Warning ## " + message) | + | c.v("## Warning ## " + message, linefeed) |
+ | |||
+ | @classmethod | ||
+ | def nl(c): | ||
+ | if c.verbose: | ||
+ | sys.stdout.write("\n") | ||
class SubElement: | class SubElement: | ||
Ligne 84: | Ligne 95: | ||
class AdvancedSubStationAlphaWriter: | class AdvancedSubStationAlphaWriter: | ||
| | ||
- | def __init__(self, filename, subElements, regular, italic, size): | + | def __init__(self, filename, subElements, regular, size): |
Console.v('Writing "' + filename + '"') | Console.v('Writing "' + filename + '"') | ||
self.fontRegular = regular | self.fontRegular = regular | ||
- | self.fontItalic = italic | ||
self.fontSize = size | self.fontSize = size | ||
self.filename = filename | self.filename = filename | ||
Ligne 107: | Ligne 117: | ||
font = "Regular" | font = "Regular" | ||
| | ||
- | isItalic = text.find('<i>') != -1 | + | text = text.replace("<i>", "{\\i1}").replace("</i>", "{\\i0}") |
- | if isItalic: | + | # Fixes the subtitle |
- | font = "Italic" | + | if text.find("{\\i1}") != -1 and text.find("{\\i0}") == -1: |
- | text = text.replace("<i>", "").replace("</i>", "") | + | text += "{\\i0}" |
| | ||
return "Dialogue: 0,{0},{1},{2},,0000,0000,0000,,{3}\n".format(start, end, font, text) | return "Dialogue: 0,{0},{1},{2},,0000,0000,0000,,{3}\n".format(start, end, font, text) | ||
Ligne 132: | Ligne 142: | ||
+ "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n" \ | + "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n" \ | ||
+ "Style: Regular," + self.fontRegular + "," + self.fontSize + ",&H00FFFFFF,&H0000FFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,1,1,1,2,20,20,20,0\n" \ | + "Style: Regular," + self.fontRegular + "," + self.fontSize + ",&H00FFFFFF,&H0000FFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,1,1,1,2,20,20,20,0\n" \ | ||
- | + "Style: Italic," + self.fontItalic + "," + self.fontSize + ",&H00FFFFFF,&H0000FFFF,&H00000000,&H00000000,-1,-1,0,0,100,100,0,0,1,1,1,2,20,20,20,0\n" \ | ||
+ "\n" \ | + "\n" \ | ||
+ "[Events]\n" \ | + "[Events]\n" \ | ||
+ "Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text\n" | + "Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text\n" | ||
+ | |||
+ | #+ "Style: Italic," + self.fontItalic + "," + self.fontSize + ",&H00FFFFFF,&H0000FFFF,&H00000000,&H00000000,-1,-1,0,0,100,100,0,0,1,1,1,2,20,20,20,0\n" \ | ||
+ | | ||
class SubRipReader: | class SubRipReader: | ||
Ligne 198: | Ligne 210: | ||
count = 0 | count = 0 | ||
emptySub = [] | emptySub = [] | ||
+ | skip = False | ||
for line in self.lines: | for line in self.lines: | ||
l = line.encode('utf-8') \ | l = line.encode('utf-8') \ | ||
Ligne 203: | Ligne 216: | ||
.replace("\r", "") | .replace("\r", "") | ||
if isJustNew: | if isJustNew: | ||
+ | skip = False | ||
isJustNew = False | isJustNew = False | ||
nextCountReady = False | nextCountReady = False | ||
Ligne 210: | Ligne 224: | ||
elif nextCountReady and l.isdigit(): | elif nextCountReady and l.isdigit(): | ||
count += 1 | count += 1 | ||
- | Console.v("Reading SRT # " + l + "\r") | + | Console.v("Reading SRT #" + str(l) + "\r", False) |
if int(l) != count: | if int(l) != count: | ||
- | Console.w("Bad SRT number! Found: #" + l + ", but should be #" + count) | + | Console.nl() |
+ | Console.w("Bad SRT number! Found: #" + str(l) + ", but should be #" + str(count)) | ||
if el != None: | if el != None: | ||
if el.text == '': | if el.text == '': | ||
Ligne 221: | Ligne 236: | ||
isJustNew = True | isJustNew = True | ||
else: | else: | ||
- | l = l.replace("", "œ") \ | + | if not skip: |
- | .replace("", "’") \ | + | skip = self.exclude(l) |
- | .replace("", "“") \ | + | if skip: |
- | .replace("", "”") \ | + | Console.nl() |
- | .replace("´´", '"') \ | + | Console.w("Skipping SRT '" + l + "'") |
- | .replace("´", "'") \ | + | elif len(l) > 0: |
- | .replace("....", "…") \ | + | l = l.replace("", "œ") \ |
- | .replace("...", "…") \ | + | .replace("", "’") \ |
- | .replace(". . .", "…") | + | .replace("", "“") \ |
- | l = self.applyLanguageOnLine(l) | + | .replace("", "”") \ |
- | el.appendLine(l) | + | .replace("", "€") \ |
+ | .replace("´´", '"') \ | ||
+ | .replace("´", "'") \ | ||
+ | .replace("....", "…") \ | ||
+ | .replace("...", "…") \ | ||
+ | .replace(". . .", "…") | ||
+ | l = self.applyLanguageOnLine(l) | ||
+ | el.appendLine(l) | ||
nextCountReady = True | nextCountReady = True | ||
| | ||
- | if (el != None): | + | if el != None and len(str(el)) > 0: |
self.elements.append(el) | self.elements.append(el) | ||
| | ||
Ligne 251: | Ligne 273: | ||
def applyLanguageOnLine(self, line): | def applyLanguageOnLine(self, line): | ||
return line | return line | ||
+ | | ||
+ | def exclude(self, line): | ||
+ | l = line.lower() | ||
+ | for x in exclusion: | ||
+ | if l.find(x) != -1: | ||
+ | return True | ||
+ | return False | ||
class Srt2Ass: | class Srt2Ass: | ||
| | ||
- | def __init__(self, srt, fregular, fitalic, fsize, delete): | + | def __init__(self, srt, fregular, fsize, delete): |
ass = srt.replace('.srt', '.ass') | ass = srt.replace('.srt', '.ass') | ||
subs = SubRipReader(srt) | subs = SubRipReader(srt) | ||
- | AdvancedSubStationAlphaWriter(ass, subs.elements, fregular, fitalic, fsize) | + | AdvancedSubStationAlphaWriter(ass, subs.elements, fregular, fsize) |
if delete: | if delete: | ||
Console.l("Removing " + srt + "…") | Console.l("Removing " + srt + "…") | ||
Ligne 267: | Ligne 296: | ||
parser.add_argument('file', metavar='file', nargs='*', help='the SubRip (srt) file') | parser.add_argument('file', metavar='file', nargs='*', help='the SubRip (srt) file') | ||
parser.add_argument('--font-regular', metavar='fontr', nargs='?', help='Name of the font to use for regular text. Default is "Cronos Pro".', default="Cronos Pro") | parser.add_argument('--font-regular', metavar='fontr', nargs='?', help='Name of the font to use for regular text. Default is "Cronos Pro".', default="Cronos Pro") | ||
- | parser.add_argument('--font-italic', metavar='fonti', nargs='?', help='Name of the font to use for italic text. Default is "Cronos Pro".', default="Cronos Pro") | ||
parser.add_argument('--font-size', metavar='fonts', nargs='?', help='Font\'s size for both regular and italic fonts. Default is 24.', default="24") | parser.add_argument('--font-size', metavar='fonts', nargs='?', help='Font\'s size for both regular and italic fonts. Default is 24.', default="24") | ||
parser.add_argument('--delete', '-d', action='store_true', help='delete the original SubRip (srt) file') | parser.add_argument('--delete', '-d', action='store_true', help='delete the original SubRip (srt) file') | ||
Ligne 286: | Ligne 314: | ||
| | ||
for srt in srtfiles: | for srt in srtfiles: | ||
- | Srt2Ass(srt, args.font_regular, args.font_italic, args.font_size, args.delete) | + | Srt2Ass(srt, args.font_regular, args.font_size, args.delete) |
</file> | </file> | ||
+ | <note>Les erreurs d'encodage du fichier SRT ne sont pas gérées par ce script. En cas de problème de conversion, ouvrez le fichier SRT avec Subtitle Editor et faites Enregistrer. La conversion fonctionnera alors parfaitement.</note> |