Skip to content

Commit

Permalink
Merge pull request #2 from Nigh/1-add-srt-support
Browse files Browse the repository at this point in the history
add support for srt format
  • Loading branch information
Nigh committed Aug 29, 2024
2 parents e59e3ba + 217461c commit be1e240
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 16 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# subtitle-ass-shifter
Shift ASS format Subtitle
# subtitle ass shifter
Shift Subtitle of [`.ass`, `.srt`] format


## Usage
Expand All @@ -16,12 +16,12 @@ ass-shifter [path] -t [shift ms]

## Example
```bash
go run main.go ../S05 -t -1002
[SUCCESS] Shifted -1002ms -> Better.Call.Saul.S05E01.2020.1080p.BluRay.x265.10bit.ass
[SUCCESS] Shifted -1002ms -> Better.Call.Saul.S05E02.2020.1080p.BluRay.x265.10bit.ass
[SUCCESS] Shifted -1002ms -> Better.Call.Saul.S05E03.2020.1080p.BluRay.x265.10bit.ass
[SUCCESS] Shifted -1002ms -> Better.Call.Saul.S05E04.2020.1080p.BluRay.x265.10bit.ass
[SUCCESS] Shifted -1002ms -> Better.Call.Saul.S05E05.2020.1080p.BluRay.x265.10bit.ass
ass-shifter ../BCS -t -1002
[SUCCESS] Shifted -1002ms -> Better Call Saul - 1x01 - Uno.HDTV.KILLERS.en.srt
[SUCCESS] Shifted -1002ms -> Better Call Saul - 1x02 - Mijo.HDTV.LOL.en.srt
[SUCCESS] Shifted -1002ms -> Better Call Saul - 1x03 - Nacho.HDTV.x264-LOL.en.srt
[SUCCESS] Shifted -1002ms -> Better Call Saul - 1x04 - Hero.HDTV.LOL.en.srt
[SUCCESS] Shifted -1002ms -> Better Call Saul - 1x05 - Alpine Shepherd Boy.HDTV.x264-LOL.en.srt
[SUCCESS] Shifted -1002ms -> Better.Call.Saul.S05E06.2020.1080p.BluRay.x265.10bit.ass
[SUCCESS] Shifted -1002ms -> Better.Call.Saul.S05E07.2020.1080p.BluRay.x265.10bit.ass
[SUCCESS] Shifted -1002ms -> Better.Call.Saul.S05E08.2020.1080p.BluRay.x265.10bit.ass
Expand Down
80 changes: 72 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
shift int32
)

var version = "0.0.0-dev"
var version = "1.0.0"

func init() {
flaggy.SetName("ass-shifter")
Expand All @@ -42,6 +42,65 @@ func main() {
filepath.Walk(inputPath, walker)
}

func srtShift(realPath string, shift int32) {
srtFile, err := os.ReadFile(realPath)
if err != nil {
fmt.Println(err)
return
}
lines := strings.Split(string(srtFile), "\n")
// 00:01:36,649
re := regexp.MustCompile(`(-?\d+):(\d\d):(\d\d)\,(\d{1,3})`)
newLines := make([]string, 0)
for _, v := range lines {
matches := re.FindAllStringSubmatch(v, -1)
for _, match := range matches {
hours, _ := strconv.Atoi(match[1])
minutes, _ := strconv.Atoi(match[2])
seconds, _ := strconv.Atoi(match[3])
milliseconds, _ := strconv.Atoi(match[4])

for i := 0; i < 3-len(match[4]); i++ {
milliseconds *= 10
}

sign := 1
if hours < 0 {
sign = -1
}
minutes *= sign
seconds *= sign
milliseconds *= sign

totalMs := (hours*3600+minutes*60+seconds)*1000 + milliseconds
totalMs += int(shift)

if totalMs < 0 {
sign = -1
} else {
sign = 1
}
totalMs *= sign

newTime := fmt.Sprintf("%02d:%02d:%02d,%03d",
totalMs/3600000*sign,
(totalMs/60000)%60,
(totalMs/1000)%60,
totalMs%1000)

v = strings.Replace(v, match[0], newTime, 1)
}
newLines = append(newLines, v)
}

err = os.WriteFile(realPath, []byte(strings.Join(newLines, "\n")), 0644)
if err != nil {
fmt.Println("[ERROR] "+filepath.Base(realPath), err)
return
}
fmt.Println("[SUCCESS] Shifted " + strconv.Itoa(int(shift)) + "ms -> " + filepath.Base(realPath))
}

func assShift(realPath string, shift int32) {
assFile, err := os.ReadFile(realPath)
if err != nil {
Expand All @@ -50,7 +109,7 @@ func assShift(realPath string, shift int32) {
}
lines := strings.Split(string(assFile), "\n")
// 0:00:10.19
re := regexp.MustCompile(`(-?\d+):(\d\d):(\d\d)\.(\d\d)`)
re := regexp.MustCompile(`(-?\d+):(\d\d):(\d\d)\.(\d{1,3})`)
newLines := make([]string, 0)
for _, v := range lines {
matches := re.FindAllStringSubmatch(v, -1)
Expand All @@ -60,6 +119,10 @@ func assShift(realPath string, shift int32) {
seconds, _ := strconv.Atoi(match[3])
milliseconds, _ := strconv.Atoi(match[4])

for i := 0; i < 3-len(match[4]); i++ {
milliseconds *= 10
}

sign := 1
if hours < 0 {
sign = -1
Expand All @@ -68,7 +131,7 @@ func assShift(realPath string, shift int32) {
seconds *= sign
milliseconds *= sign

totalMs := (hours*3600+minutes*60+seconds)*1000 + milliseconds*10
totalMs := (hours*3600+minutes*60+seconds)*1000 + milliseconds
totalMs += int(shift)

if totalMs < 0 {
Expand All @@ -89,8 +152,6 @@ func assShift(realPath string, shift int32) {
newLines = append(newLines, v)
}

// savePath := filepath.Dir(realPath) + "\\shifted_" + filepath.Base(realPath)
// fmt.Println(savePath)
err = os.WriteFile(realPath, []byte(strings.Join(newLines, "\n")), 0644)
if err != nil {
fmt.Println("[ERROR] "+filepath.Base(realPath), err)
Expand All @@ -104,10 +165,13 @@ func walker(realPath string, f os.FileInfo, err error) error {
if f.Name()[0] == '.' {
return filepath.SkipDir
}
if strings.ToLower(ext) != ".ass" {
switch strings.ToLower(ext) {
case ".srt":
srtShift(realPath, shift)
case ".ass":
assShift(realPath, shift)
default:
return nil
}
// fmt.Println(realPath)
assShift(realPath, shift)
return nil
}

0 comments on commit be1e240

Please sign in to comment.