Linux ass2srt

bash script

#! /usr/bin/env bash

if [ $# -ne  ]
then
echo "USAGE: $0 <directory> <fromEncoding> <toEnoding>"
exit ;
fi if [ -d $ ]
then
echo "Notice: Directory is set $1"
DIRNAME=$
else
echo "Error: $1 is not a valid directory"
exit ;
fi if [ $ = 'utf16-le' -o $ = 'big5' -o $ = 'utf8' -o $ = 'ucs-2le' ]
then
echo "Notice: ASS2SRT from encoding is set $2"
ASS2SRT_FROM=$
else
echo "Error: ASS2SRT from encoding setting error"
exit ;
fi if [ $ = 'utf16-le' -o $ = 'big5' -o $ = 'utf8' -o $ = 'ucs-2le' ]
then
echo "Notice: ASS2SRT to encoding is set $3"
ASS2SRT_TO=$
else
echo "Error: ASS2SRT to encoding setting error"
exit ;
fi if [ ! -f ./ass2srt.pl ]
then
echo "Error: ass2srt.pl is not in the current directory"
exit ;
fi for file in `ls $`
do
if [ -f $file ]
then
if [ ${file##*.} = 'ass' ]
then
echo "perl ./ass2srt.pl -f $ASS2SRT_FROM -t $ASS2SRT_TO ${DIRNAME}/${file%.*}.ass ${DIRNAME}/${file%.*}.srt"
perl ./ass2srt.pl -f $ASS2SRT_FROM -t $ASS2SRT_TO ${DIRNAME}/${file%.*}.ass ${DIRNAME}/${file%.*}.srt
fi
fi
done

ass2srt.pl

#!/usr/bin/perl -w
use strict;
use warnings;
#use encoding 'utf-8'; # compilation aborted at
use utf8; # Source code is UTF-8
use open ':std', ':utf8'; # STDIN,STOUT,STERR is UTF-8.
use Getopt::Long;
use File::Spec; Getopt::Long::Configure( "pass_through", "no_ignore_case" ); # Default encoding
my $defaultEnc = 'utf8';
my $fromEnc = '';
my $toEnc = '';
my $help = ;
my $preserve = ;
my $showVer = ;
my $showLicense = ;
my $dosFormat = ;
my $macFormat = ; my $oldTime = ""; $fromEnc = $ENV{'ASS2SRT_FROM'};
$toEnc = $ENV{'ASS2SRT_TO'}; my $DEBUG = ;
my $noCorrect = ; GetOptions(
"help|h", \$help, "from|f=s", \$fromEnc,
"to|t=s", \$toEnc, "default|d=s", \$defaultEnc,
"preserve|p", \$preserve,
"version|v", \$showVer, "dos|o", \$dosFormat,
"mac|m", \$macFormat, "license|l", \$showLicense,
"debug", \$DEBUG, "nocorrect", \$noCorrect
); # Lines read from .ass file will be put here (each in a hash with the
# relevant info) to be written to the .srt file after some post-processing.
my @lines; # Wrap format
my $crlf = "\n"; # If the specified output at the same time as Mac and DOS format,
# responding to an error message and terminated
( $dosFormat && $macFormat )
&& die( "\n Error: \n\tOption -m|--mac and -o|--dos can not exist together \n" ); if ($dosFormat) { $crlf = "\r" . $crlf; }
if ($macFormat) { $crlf = "\r"; } # Program version
my $version = "0.3.0.1"; # Display help message
if ($help) { usage(); } # Show license information
if ($showLicense) { license(); } # Show version Information
if ($showVer) { showVersion(); } # If the source file encoding is not specified, then set it equals to defaultEnc
if ( !$fromEnc ) { $fromEnc = $defaultEnc; } # If the purpose file encoding is not specified, then set it equals to defaultEnc
if ( !$toEnc ) { $toEnc = $defaultEnc; } # The .ass file file name
my $assFile = shift || '';
if ( !$assFile ) { usage(); } # The .srt file file name, the default value is .ass's filename,
# but changed the name extension to .srt
my $srtFile = shift || '';
if ( !$srtFile ) {
$srtFile = $assFile;
$srtFile =~ s|.[^.]*$||;
$srtFile .= ".srt";
} # Using the parameters listed
print "Using parameters: \n";
print "\t .ass File Encoding: $fromEnc\n";
print "\t .SRT File Encoding: $toEnc\n";
print "\n"; # Open file
open( ASSFILE, "<", $assFile )
or die 'Can not open ".ass" source file:' . $assFile . ", please check it!!\n"; # Specify the file encoding
binmode( ASSFILE, ':encoding(' . $fromEnc . ')' ); # Check the existence of the system environment variable OS (Windows OS should be set this)
my $OS_TYPE = $ENV{'OS'}; # Assigned the .srt file encodings
my $toEncString = ':encoding(' . $toEnc . ')'; # Depending on OS type (only for Windows), fix the .srt file encoding setting.
if ( $OS_TYPE && $OS_TYPE =~ m/windows/i ) {
print "Operation System is: " . $OS_TYPE
. ", Checking File Encoding... \n"; # Only use UTF16 or UCS format when otherwise specified (others need another test)
if ( ( $toEnc =~ m/utf16/i ) || ( $toEnc =~ m/ucs/i ) ) {
$toEncString = ':raw' . $toEncString;
}
} # Unicode output file on Windows need to join the BOM identification mark.
if ( $toEncString =~ m/:raw/i ) {
print SRTFILE "\x{FEFF}";
print "Using unicode encoding, print BOM signature to file: " . $srtFile
. "\n\n";
} # Number of records subtitles
my $lineNum = ; # Read .ass and deal with the source file
while (<ASSFILE>) {
chomp; # If a line begin with 'Dialougue', extract this line's parameters
if (m/^Dialogue:/) {
$lineNum = extractLine( $lineNum, $_ );
}
elsif (m/^(Title:|Original)/) {
# If a line begins with 'Title' or 'Original', it is the source for the subtitles
print $_ . "\n";
} } # Close the file
close ASSFILE; # post-process lines to remove errors.
if (!$noCorrect)
{
for (my $i = ; $i < scalar @lines; $i++)
{
my $line = $lines[$i];
my $prevLine = undef;
$prevLine = $lines[$i-] if $i > ; # if the begin of the previous line is the same as the begin of this line,
# merge the two lines.
if ($prevLine && $prevLine->{begin} eq $line->{begin})
{
# merge subtitles
$prevLine->{subtitle} .= "\n" . $line->{subtitle}; # remove line $i from the list
@lines = (@lines[..$i-], @lines[$i+..$#lines]); # resync things for next test
$i--;
$prevLine = undef;
$prevLine = $lines[$i-] if $i > ;
} # if the end of the previous line is smaller than the begin of this line,
# change the end of the previous line to be the begin of this line.
if ($prevLine && $prevLine->{end} gt $line->{begin})
{
$prevLine->{end} = $line->{begin};
}
}
} open( SRTFILE, ">", $srtFile )
or die 'Can not open the ".srt" purpose of file:' . $srtFile . ", please check!\n"; # Specified file used to write code
binmode( SRTFILE, $toEncString ); # Write SRT file.
my $lineNumber = ;
foreach my $line (@lines)
{
# Write .srt file
# Because .ass unit of time (10ms) different with .srt (1ms), so the full complement of 0
my $currentTime = $line->{begin} . "0 --> " . $line->{end} . "0"; print SRTFILE $lineNumber . $crlf;
print SRTFILE $currentTime . $crlf;
print SRTFILE $line->{subtitle} . " " . $crlf . $crlf . $crlf; $lineNumber++;
} close SRTFILE; # Extract data for one line in the .ass file
sub extractLine { # Deal with the number of rows
# From. ass of the original content
my ($lineNumber, $content) = @_; my $begin;
my $end;
my $subtitle;
my $currentTime; # Solved starting time, ending time, subtitle format, subtitles content
if ( $content
=~ m/Dialogue: [^,]*,([^,]*),([^,]*),([^,]*),[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,(.*)$/
)
{
$begin = $;
$end = $;
$subtitle = $; my $isComment = $; print "\nLine: $lineNumber\n Begin: [$begin] End: [$end] isComment: [$isComment]\n Subtitle: [$subtitle]\n"
if $DEBUG; # the separator between seconds and ms is "," -- not ".", so we change it !
$begin =~ s/\./,/g;
$end =~ s/\./,/g; # If the time format will not be part of the hour when the two chars, make up two chars.
if ( $begin =~ m/^\d{}:/ ) {
$begin = "0" . $begin;
} if ( $end =~ m/^\d{}:/ ) {
$end = "0" . $end;
} # First filter out the end of every title to the digital sign in order to follow-up to the output under a variety of formats on different platforms
$subtitle =~ s/\r$//g; # If there is no such setting .ass control commands, then filter out the
if ( !$preserve ) {
$subtitle =~ s/{[^}]*}//g;
} # Comment if the subtitle format, then in the before and after the add ()
if ( $isComment eq 'comment' ) {
$subtitle = '(' . $subtitle . ')';
} print "\nAfter:\n Begin: [$begin] End: [$end] isComment: [$isComment]\n Subtitle: [$subtitle]\n"
if $DEBUG; my %line = ( begin => $begin, end => $end, isComment => $isComment, subtitle => $subtitle );
push @lines, \%line; return $lineNumber;
}
} # Use
sub usage {
print <<__HELP__; ass2srt [option] .ass source files [.srt purpose of file] The Advanced SubStation Alpha ".ass" file format to SubRip ".srt" format. Options:
-h --help help show this help message -d, --default=encoding default = encoding set the default file encoding, which is the purpose of the source file and use the same code file.
The default value is UTF- encoding format. -f, --from=encoding from = encoding specified source file. ass coding system used. The set will be covered by the aforementioned pre -
Coding based on the settings file, when not specified the default file encoding for the default value.
* Tip: You can also use the system environment variables specified ASS2SRT_FROM -t, --to=encoding encoding file specified purposes. srt coding system used. The set will be covered by the aforementioned pre -
Coding based on the settings file, when not specified the default file encoding for the default value.
* Tip: You can also use the system environment variables specified ASS2SRT_TO -p, --preserve preserve the source file to retain. ass subtitles in the control instructions, write together. srt file. -o, --dos dos specified output file format for DOS (the default format for Unix)
* Warning: can not be set this option with -m/--mac together. -m, --mac mac specify the output file format for the Mac (the default format for Unix)
* Warning: can not be this option with set -o/--dos together. --debug print debugging output for each line of the .ass file read. --nocorrect don't attempt to correct errors in subtitles
if not specified, corrections will be made for the following errors:
- subtitle i+1 begins before subtitle i ends - subtitle i's end will be set to subtitle i+'s begin
- subtitle i+1 begins at the same time as subtitle i begins - subtitles will be merged into one subtitle .ass source file:
The Subtitles of the original file.
It can be Advanced SubStation Alpha (.ass) or SubStation Alpha (.ssa) format. [.srt purpose file]:
The default SubRip format output filename, the default value is the same as
the .ass source filename but it's extension changed to .srt. Operation Example: Like iconv utility, use -f to specified from encoding set, the -t to specified target encoding set. . Convert a utf16-le.ass file encoded with Unicode/UTF- LE to big5.srt with Big5 encoding. myhost \$ perl ass2srt.pl -f utf16-le -t big5 utf16-le.ass big5.srt . Convert a Big5 encoding .ass file to UTF- encoding .srt file myhost \$ perl ass2srt.pl -f big5 -t utf8 big5.ass utf8.srt . Convert a UTF- encoding .ass file to UTF- encoding .srt file,
no -f or -t parameter because UTF- is the default encoding setting. myhost \$ perl ass2srt.pl utf-.ass utf-.srt . Convert a UCS- LE encoding .ass file to UCS- LE encoding .srt file,
UCS- LE is the same as Unicode encoding on Windows Platform. myhost \$ perl ass2srt.pl -f ucs-2le -t ucs-2le ucs-2le.ass ucs-2le.srt . Using the environment variables to specified encoding parameter
* Warning: Some operation system can not add " or ' quotation marks to the varialbe's value myhost \$ export ASS2SRT_FROM = utf16-le
myhost \$ export ASS2SRT_TO = big5
myhost \$ perl ass2srt.pl utf16-le.ass big5.srt __HELP__
exit 2;
} # Show version of the message
sub showVersion { print <<__VERSION__; ass2srt $version - Convert subtitles from .ass to .srt format Advanced SubStation Alpha subtitle file format conversion tool
(c) 2006 Ada Hsu, hungwei.hsu (at) gmail (dot) com
(c) 2009 Jean-Sebastien Guay, jean_seb (at) videotron (dot) ca __VERSION__
exit 2;
} # License
sub license { print <<__LICENSE__; ass2srt $version - Convert subtitles from .ass to .srt format Advanced SubStation Alpha subtitle file format conversion tool
(c) 2006 Ada Hsu, hungwei.hsu (at) gmail (dot) com
(c) 2009 Jean-Sebastien Guay, jean_seb (at) videotron (dot) ca This utility use of the software CC-GNU GPL (http://creativecommons.org/licenses/GPL/2.0/)
authorization, You can view the relevant provisions of
http://www.gnu.org/copyleft/gpl.html text. You are free to use the software, but software maintainer will hold no liability for any damages,
Thank You. __LICENSE__
exit 2; }

使用样例:

lsgx@DESKTOP-OS0DFSI:/mnt/e/Download/Hellsing.Ultimate.[OVA][1920×1080][BDRip][10bit][FLAC].uni_sub$ ./xx.bash ./ utf8 utf8
Notice: Directory is set ./
Notice: ASS2SRT from encoding is set utf8
Notice: ASS2SRT to encoding is set utf8
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][01][1920×1080][BDRip][10bit][FLAC].ass .//Hellsing.Ultimate.[OVA][01][1920×1080][BDRip][10bit][FLAC].srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title: RP
Original Script: RP
Original Translation:
Original Timing:
Original Editing:
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][02][1920×1080][BDRip][10bit][FLAC].ass .//Hellsing.Ultimate.[OVA][02][1920×1080][BDRip][10bit][FLAC].srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title: RP
Original Script: RP
Original Translation:
Original Timing:
Original Editing:
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][03][1920×1080][BDRip][10bit][FLAC].ass .//Hellsing.Ultimate.[OVA][03][1920×1080][BDRip][10bit][FLAC].srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title: rp
Original Script: rp
Original Translation:
Original Timing:
Original Editing:
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][04][1920×1080][BDRip][10bit][FLAC].ass .//Hellsing.Ultimate.[OVA][04][1920×1080][BDRip][10bit][FLAC].srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title: RP工作室
Original Script: RP工作室
Original Translation:
Original Timing:
Original Editing:
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][05][1920×1080][BDRip][10bit][FLAC].ass .//Hellsing.Ultimate.[OVA][05][1920×1080][BDRip][10bit][FLAC].srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title: RP工作室
Original Script: RP工作室
Original Translation:
Original Timing:
Original Editing:
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][06][1920×1080][BDRip][10bit][FLAC].ass .//Hellsing.Ultimate.[OVA][06][1920×1080][BDRip][10bit][FLAC].srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title:
Original Script:
Original Translation:
Original Timing:
Original Editing:
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][07][1920×1080][BDRip][10bit][FLAC].ass .//Hellsing.Ultimate.[OVA][07][1920×1080][BDRip][10bit][FLAC].srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title: RP工作室
Original Script: RP工作室
Original Translation:
Original Timing:
Original Editing:
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][08][1920×1080][BDRip][10bit][FLAC].ass .//Hellsing.Ultimate.[OVA][08][1920×1080][BDRip][10bit][FLAC].srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title: RP
Original Script: RP
Original Translation:
Original Timing:
Original Editing:
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][09][1920×1080][BDRip][10bit][FLAC].ass .//Hellsing.Ultimate.[OVA][09][1920×1080][BDRip][10bit][FLAC].srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title: RP
Original Script: RP
Original Translation:
Original Timing:
Original Editing:
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][10][1920×1080][BDRip][10bit][FLAC].ass .//Hellsing.Ultimate.[OVA][10][1920×1080][BDRip][10bit][FLAC].srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title: RP
Original Script: RP
Original Translation:
Original Timing:
Original Editing:
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][The.Dawn.Ⅲ][1920×1080][BDRip][10bit][FLAC].ass .//Hellsing.Ultimate.[OVA][The.Dawn.Ⅲ][1920×1080][BDRip][10bit][FLAC].srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][The.Dawn.Ⅰ][1920×1080][BDRip][10bit][FLAC].mkv.ass .//Hellsing.Ultimate.[OVA][The.Dawn.Ⅰ][1920×1080][BDRip][10bit][FLAC].mkv.srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title: RP
Original Script: RP
Original Translation:
Original Timing:
Original Editing:
perl ./ass2srt.pl -f utf8 -t utf8 .//Hellsing.Ultimate.[OVA][The.Dawn.Ⅱ][1920×1080][BDRip][10bit][FLAC].mkv.ass .//Hellsing.Ultimate.[OVA][The.Dawn.Ⅱ][1920×1080][BDRip][10bit][FLAC].mkv.srt
Using parameters:
.ass File Encoding: utf8
.SRT File Encoding: utf8 Title: RP
Original Script: RP
Original Translation:
Original Timing:
Original Editing:
lsgx@DESKTOP-OS0DFSI:/mnt/e/Download/Hellsing.Ultimate.[OVA][1920×1080][BDRip][10bit][FLAC].uni_sub$

=========== End

 

Linux ass2srt的更多相关文章

  1. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  2. Linux 内核概述 - Linux Kernel

    Linux 内核学习笔记整理. Unix unix 已有40历史,但计算机科学家仍认为其是现存操作系统中最大和最优秀的系统,它已成为一种传奇的存在,历经时间的考验却依然声名不坠. 1973 年,在用 ...

  3. 死磕内存篇 --- JAVA进程和linux内存间的大小关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

  4. NodeJs在Linux下使用的各种问题

    环境:ubuntu16.04 ubuntu中安装NodeJs 通过apt-get命令安装后发现只能使用nodejs,而没有node命令 如果想避免这种情况请看下面连接的这种安装方式: 拓展见:Linu ...

  5. [linux]阿里云主机的免登陆安全SSH配置与思考

    公司服务器使用的第三方云端服务,即阿里云,而本地需要经常去登录到服务器做相应的配置工作,鉴于此,每次登录都要使用密码是比较烦躁的,本着极速思想,我们需要配置我们的免登陆. 一 理论概述 SSH介绍 S ...

  6. Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part3:db安装和升级

    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part3:db安装和升级 环境:OEL 5.7 + Oracle 10.2.0.5 RAC 5.安装Database软件 5. ...

  7. Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part1:准备工作

    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part1:准备工作 环境:OEL 5.7 + Oracle 10.2.0.5 RAC 1.实施前准备工作 1.1 服务器安装操 ...

  8. SQL Server on Linux 理由浅析

    SQL Server on Linux 理由浅析 今天的爆炸性新闻<SQL Server on Linux>基本上在各大科技媒体上刷屏了 大家看到这个新闻都觉得非常震精,而美股,今天微软开 ...

  9. Microsoft Loves Linux

    微软新任CEO纳德拉提出的“Microsoft Loves Linux”,并且微软宣布.NET框架的开源,近期Microsoft不但宣布了Linux平台的SQL Server,还宣布了Microsof ...

随机推荐

  1. rabbitmq添加user及vhost

    rabbitmqctl add_vhost /myhost # 添加 vhost rabbitmqctl add_user me me123 # 设置用户和密码 rabbitmqctl set_per ...

  2. rust 函数的使用

    fn main() { println!("Hello, world!"); another_function(2,3); let y ={ let x =3; //表达式的结尾没 ...

  3. server2003 IIS6.0 网站不可用

    事件 ID ( 487 )的描述(在资源( Zend Optimizer )中)无法找到.本地计算机可能没有必要的注册信息或消息 DLL 文件来从远程计算机显示消息.您可能可以使用 /AUXSOURC ...

  4. flask上传文件到指定路径

    flask上传文件到指定路径 项目结构如下: 首先是:视图函数uload_file.py,代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- fro ...

  5. spring源码的设计模式

    转:https://blog.csdn.net/huyang0304/article/details/82928900 接下来我们只介绍在Spring中常用的设计模式. 1.1.简单工厂模式(Fact ...

  6. k8s 新加节点

    拷贝原来的内容过去,删除 cd /opt/kubernetes/ssl/ 1. 删除    kubelet-crt    key kube-proxy-key.pem  相关的这些key是,根据too ...

  7. PG11开启WAL归档

    -创建归档目录 mkdir -p $PGDATA/archive_wals chown -R postgres.postgres $PGDATA/archive_wals -修改参数(在配置文件中配置 ...

  8. .net程序反编译工具(ILSpy)

    ILSpy是SharpDevelop小组的反编译工具,ILSPY这个开源工具的目的就是代替reflector的,它可以反编译出比reflector更好的C#代码. PC官方版 C#反编译工具ilspy ...

  9. 64位CreateProcess逆向:(二)0环下参数的整合即创建进程的整体流程

    转载:https://bbs.pediy.com/thread-207683.htm 点击下面进入总目录: 64位Windows创建64位进程逆向分析(总目录) 在上一篇文章中,我们介绍了Create ...

  10. Spring cloud微服务安全实战-7-11PinPoint+SpringBoot环境搭建

    微服务的最后一个组件, 调用链监控,一个请求进来以后,经过N多个微服务,例如a调用了b.b又调用了c,那么在这个过程中看到,整个的调用的链路,然后每一段调用所耗费的时间,帮你去分析你的系统如果出现瓶颈 ...