ayghub / rename-sub

Rename current subtitle file as the playing video and move it to the same directory as the video.
1 stars 0 forks source link

Convert subs to UPPERCASE while keeping original #2

Open mrfragger opened 1 month ago

mrfragger commented 1 month ago

Some fonts are uppercase only and just take normal subs. Meaning they just uppercase all characters. However, some fonts require all UPPERCASE subs for ligatures, and if there are lowercase characters then essential the font is useless. So this modified script of rename-sub.lua ...It renames filename subs like the original does and creates a new sub file with _UPPERCASE.vtt or .srt with all subs uppercased.
edit: Modified it so it loads sub as soon as it converts.

rename-subupper.lua

--made by ayghub, edited by https://github.com/elhirchek

function path_ext(filename)
        local pattern = "%.%l+$"
        if filename then
          local i, j = string.find(filename, pattern)
          local path = string.sub(filename, 1, i-1)
          local ext = string.sub(filename, i, j )
          return path, ext
        end
end

function rename_subupper()
    local external_file = mp.get_property("current-tracks/sub/external-filename")
        local sub_path, sub_ext = path_ext(external_file)
        local movie_path, _ = path_ext(mp.get_property("path"))
        local org_sub = io.open(sub_path..sub_ext, "r")
        local uppercase_file = io.open(movie_path..".uppercase"..sub_ext, "w")

            for line in org_sub:lines() do
                local upper_line = string.upper(line)
                uppercase_file:write(upper_line .. "\n")
            end

            local uppercase_new = (movie_path..".uppercase"..sub_ext)

        org_sub:close()
        uppercase_file:close()        
        mp.osd_message("Sub converted to UPPERCASE")
        print("Sub converted to UPPERCASE")
        mp.commandv('sub-add', uppercase_new)

end

mp.add_key_binding(nil, "rename-subupper", rename_subupper)

It works but if there is no active sub then it gives a attempt to conecate local 'sub_path' (a nil value) Anyone know how to error check it with an if / else statement. I tried many ways but it keeps failing although it's not that big of a deal.

mrfragger commented 2 weeks ago

Here's one for converting to demofont... changes . --> AND ? --> ¥ AND , --> ¸ and other punctuation. Doesn't touch numbers though. Couldn't figure out looping through tables in lua. Probably worse programming ever but it does the job. Pretty much the equivalent on mac with gsed cat some.vtt | gsed -E -e 's/\.$|\. /\_ /g' -e 's/'\''//g' -e 's/\?/¥/g' -e 's/([A-Za-z])-/\1~/g' -e 's/,/¸/g' -e 's/(|[/{/g' -e 's/)|]//g' -e 's/#|\: |\;|\<\=/~/g' -e 's/!/||/g' -e 's/([A-Za-z])./\1_/g' > some.demofont.vtt`

edit: Got nearly all punctuation converted and it loads as soon as it's done now.

rename-subdemofont.lua

--made by ayghub, edited by https://github.com/elhirchek

function path_ext(filename)
        local pattern = "%.%l+$"
        if filename then
          local i, j = string.find(filename, pattern)
          local path = string.sub(filename, 1, i-1)
          local ext = string.sub(filename, i, j )
          return path, ext
        end
end

function rename_demofont()
    local external_file = mp.get_property("current-tracks/sub/external-filename")
        local sub_path, sub_ext = path_ext(external_file)
        local movie_path, _ = path_ext(mp.get_property("path"))
        local org_sub = io.open(sub_path..sub_ext, "r")
        local demofont_file = io.open(movie_path..".demofont"..sub_ext, "w")
        local tempa_file = io.open(movie_path.."_tempa"..sub_ext, "w")
        local tempb_file = io.open(movie_path.."_tempb"..sub_ext, "w")
        local tempc_file = io.open(movie_path.."_tempc"..sub_ext, "w")
        local tempd_file = io.open(movie_path.."_tempd"..sub_ext, "w")
        local tempe_file = io.open(movie_path.."_tempe"..sub_ext, "w")
        local tempf_file = io.open(movie_path.."_tempf"..sub_ext, "w")
        local tempg_file = io.open(movie_path.."_tempg"..sub_ext, "w")
        local temph_file = io.open(movie_path.."_temph"..sub_ext, "w")
        local tempi_file = io.open(movie_path.."_tempi"..sub_ext, "w")
        local tempj_file = io.open(movie_path.."_tempj"..sub_ext, "w")
        local tempk_file = io.open(movie_path.."_tempk"..sub_ext, "w")
        local templ_file = io.open(movie_path.."_templ"..sub_ext, "w")
        local tempm_file = io.open(movie_path.."_tempm"..sub_ext, "w")

        for line in org_sub:lines() do
          local replace_line = string.gsub(line, "(%d)%.(%d)", "%1QAZXSW%2")     
          tempa_file:write(replace_line .. "\n")
      end

      local first_sub = io.open(movie_path.."_tempa"..sub_ext, "r")

        for line in first_sub:lines() do
          local replace_line = string.gsub(line, "(%d),(%d)", "%1XZSAWQ%2")     
          tempb_file:write(replace_line .. "\n")
            end

      local second_sub = io.open(movie_path.."_tempb"..sub_ext, "r")

        for line in second_sub:lines() do
          local replace_line = string.gsub(line, "[@#&%*%%%+%*%^%$%,]", "¸")     
          tempc_file:write(replace_line .. "\n")
            end

      local third_sub = io.open(movie_path.."_tempc"..sub_ext, "r")

        for line in third_sub:lines() do
          local replace_line = string.gsub(line, "[;!%.]", "_")     
          tempd_file:write(replace_line .. "\n")
            end

      local fourth_sub = io.open(movie_path.."_tempd"..sub_ext, "r")

        for line in fourth_sub:lines() do
          local replace_line = string.gsub(line, "[%$%?]", "_¥")     
          tempe_file:write(replace_line .. "\n")
            end

      local fifth_sub = io.open(movie_path.."_tempe"..sub_ext, "r")

        for line in fifth_sub:lines() do
          local replace_line = string.gsub(line, "%[%(<", "{")     
          tempf_file:write(replace_line .. "\n")
          end

      local sixth_sub = io.open(movie_path.."_tempf"..sub_ext, "r")

        for line in sixth_sub:lines() do
              local replace_line = string.gsub(line, "%]%)>", "}")     
              tempg_file:write(replace_line .. "\n")
          end

      local seventh_sub = io.open(movie_path.."_tempg"..sub_ext, "r")

          for line in seventh_sub:lines() do
                local replace_line = string.gsub(line, '"', "``")     
                temph_file:write(replace_line .. "\n")
            end     
      local eighth_sub = io.open(movie_path.."_temph"..sub_ext, "r")

            for line in eighth_sub:lines() do
                  local replace_line = string.gsub(line, "%-%->", "XSWQAZ")     
                  tempi_file:write(replace_line .. "\n")
              end     
      local ninth_sub = io.open(movie_path.."_tempi"..sub_ext, "r")

              for line in ninth_sub:lines() do
                    local replace_line = string.gsub(line, "[%-]", "~")     
                    tempj_file:write(replace_line .. "\n")
                end  
      local tenth_sub = io.open(movie_path.."_tempj"..sub_ext, "r")

                for line in tenth_sub:lines() do
                      local replace_line = string.gsub(line, "XSWQAZ", "-->")     
                      tempk_file:write(replace_line .. "\n")
                  end  

      local eleventh_sub = io.open(movie_path.."_tempk"..sub_ext, "r")

                  for line in eleventh_sub:lines() do
                        local replace_line = string.gsub(line, "'", "´")     
                        templ_file:write(replace_line .. "\n")
                    end 

      local twelfth_sub = io.open(movie_path.."_templ"..sub_ext, "r")

          for line in twelfth_sub:lines() do
            local replace_line = string.gsub(line, "QAZXSW", ".")     
                tempm_file:write(replace_line .. "\n")
                      end 

    local thirteenth_sub = io.open(movie_path.."_tempm"..sub_ext, "r")

          for line in thirteenth_sub:lines() do
            local replace_line = string.gsub(line, "XZSAWQ", ",")     
                demofont_file:write(replace_line .. "\n")
                      end 

      local demofont_new = (movie_path..".demofont"..sub_ext)

        org_sub:close()
        first_sub:close()
        second_sub:close()
        third_sub:close()
        fourth_sub:close()
        fifth_sub:close()
        sixth_sub:close()
        seventh_sub:close()
        eighth_sub:close()
        ninth_sub:close()
        tenth_sub:close()
        eleventh_sub:close()
        twelfth_sub:close()
        tempa_file:close()
        tempb_file:close()
        tempc_file:close()
        tempd_file:close()
        tempe_file:close()
        tempf_file:close()
        tempg_file:close()
        temph_file:close()
        tempi_file:close()
        tempj_file:close()
        tempk_file:close()
        templ_file:close()
        tempm_file:close()

        os.remove(movie_path.."_tempa"..sub_ext)
        os.remove(movie_path.."_tempb"..sub_ext)
        os.remove(movie_path.."_tempc"..sub_ext)
        os.remove(movie_path.."_tempd"..sub_ext)
        os.remove(movie_path.."_tempe"..sub_ext)
        os.remove(movie_path.."_tempf"..sub_ext)
        os.remove(movie_path.."_tempg"..sub_ext)
        os.remove(movie_path.."_temph"..sub_ext)
        os.remove(movie_path.."_tempi"..sub_ext)
        os.remove(movie_path.."_tempj"..sub_ext)
        os.remove(movie_path.."_tempk"..sub_ext)
        os.remove(movie_path.."_templ"..sub_ext)
        os.remove(movie_path.."_tempm"..sub_ext)

        mp.osd_message("Sub converted to DemoFont")
        print("Sub converted to DemoFont")
        mp.commandv('sub-add', demofont_new)

end

mp.add_key_binding(nil, "rename-subdemofont", rename_demofont)
mrfragger commented 2 weeks ago

rename-subreverse.lua

Reverses words less than 10 letters (evidently lua capturing groups limit is 9). Anyway this one is for the kids. Although I put the last portion of the letters at the front of the first 8 characters that are reversed. I think just doing a string.reverse is no fun to read...it has to be done on words not a sentence.

--made by ayghub, edited by https://github.com/elhirchek

function path_ext(filename)
        local pattern = "%.%l+$"
        if filename then
          local i, j = string.find(filename, pattern)
          local path = string.sub(filename, 1, i-1)
          local ext = string.sub(filename, i, j )
          return path, ext
        end
end

function rename_subreverse()
    local external_file = mp.get_property("current-tracks/sub/external-filename")
        local sub_path, sub_ext = path_ext(external_file)
        local movie_path, _ = path_ext(mp.get_property("path"))
        local org_sub = io.open(sub_path..sub_ext, "r")
        local reverse_file = io.open(movie_path..".reverse"..sub_ext, "w")
        local t1_file = io.open(movie_path.."_t1"..sub_ext, "w")
        local t2_file = io.open(movie_path.."_t2"..sub_ext, "w")
        local t3_file = io.open(movie_path.."_t3"..sub_ext, "w")
        local t4_file = io.open(movie_path.."_t4"..sub_ext, "w")
        local t5_file = io.open(movie_path.."_t5"..sub_ext, "w")
        local t6_file = io.open(movie_path.."_t6"..sub_ext, "w")
        local t7_file = io.open(movie_path.."_t7"..sub_ext, "w")
        local t8_file = io.open(movie_path.."_t8"..sub_ext, "w")
        local t9_file = io.open(movie_path.."_t9"..sub_ext, "w")
        local t10_file = io.open(movie_path.."_t10"..sub_ext, "w")
        local t11_file = io.open(movie_path.."_t11"..sub_ext, "w")
        local t12_file = io.open(movie_path.."_t12"..sub_ext, "w")
        local t13_file = io.open(movie_path.."_t13"..sub_ext, "w")
        local t14_file = io.open(movie_path.."_t14"..sub_ext, "w")
        local t15_file = io.open(movie_path.."_t15"..sub_ext, "w")
        local t16_file = io.open(movie_path.."_t16"..sub_ext, "w")
        local t17_file = io.open(movie_path.."_t17"..sub_ext, "w")
        local t18_file = io.open(movie_path.."_t18"..sub_ext, "w")
        local t19_file = io.open(movie_path.."_t19"..sub_ext, "w")
        local t20_file = io.open(movie_path.."_t20"..sub_ext, "w")

      for line in org_sub:lines() do 
          local replace_line = string.gsub(line, "(%d)%.(%d)", "%1QAZXSW%2")     
          t1_file:write(replace_line .. "\n")
      end

      local f1_sub = io.open(movie_path.."_t1"..sub_ext, "r")

      for line in f1_sub:lines() do
        local replace_line = string.gsub(line, "(%d),(%d)", "%1XZSAWQ%2")     
        t2_file:write(replace_line .. "\n")
          end

      local f2_sub = io.open(movie_path.."_t2"..sub_ext, "r")

        for line in f2_sub:lines() do
          local replace_line = string.gsub(line, "[%]%)%[%(<%$%?;!%.@#&%*%%%+%*%^%$%,]", " ")     
          t3_file:write(replace_line .. "\n") 
            end

      local f3_sub = io.open(movie_path.."_t3"..sub_ext, "r")

        for line in f3_sub:lines() do
          local replace_line = string.gsub(line, "%-%->", "XSWQAZ")     
          t4_file:write(replace_line .. "\n")
            end

      local f4_sub = io.open(movie_path.."_t4"..sub_ext, "r")

        for line in f4_sub:lines() do
          local replace_line = string.gsub(line, "[%-]", " ")     
          t5_file:write(replace_line .. "\n")
            end

      local f5_sub = io.open(movie_path.."_t5"..sub_ext, "r")

        for line in f5_sub:lines() do
              local replace_line = string.gsub(line, "XSWQAZ", "-->")     
          t6_file:write(replace_line .. "\n")
          end

      local f6_sub = io.open(movie_path.."_t6"..sub_ext, "r")

        for line in f6_sub:lines() do
              local replace_line = string.gsub(line, "'", " ")     
          t7_file:write(replace_line .. "\n")
          end

      local f7_sub = io.open(movie_path.."_t7"..sub_ext, "r")

          for line in f7_sub:lines() do
                local replace_line = string.gsub(line, "QAZXSW", ".")     
          t8_file:write(replace_line .. "\n")
            end     

      local f8_sub = io.open(movie_path.."_t8"..sub_ext, "r")

            for line in f8_sub:lines() do
                  local replace_line = string.gsub(line, "XZSAWQ", ",")     
          t9_file:write(replace_line .. "\n")
              end    

      local f9_sub = io.open(movie_path.."_t9"..sub_ext, "r")

              for line in f9_sub:lines() do
                    local replace_line = string.gsub(line, '"', " ")     
          t10_file:write(replace_line .. "\n")
                end  

      local f10_sub = io.open(movie_path.."_t10"..sub_ext, "r")

                for line in f10_sub:lines() do
                      local replace_line = string.gsub(line, "^(%a)", " %1")     
          t11_file:write(replace_line .. "\n")
      end  

      local f11_sub = io.open(movie_path.."_t11"..sub_ext, "r")

          for line in f11_sub:lines() do
          local replace_line = string.gsub(line, "(%a)$", "%1 ")     
          t12_file:write(replace_line .. "\n")
      end 

      local f12_sub = io.open(movie_path.."_t12"..sub_ext, "r")

          for line in f12_sub:lines() do
          local replace_line = string.gsub(line, "%s(%a)(%a)%s", " %2%1 ")     
          t13_file:write(replace_line .. "\n")
  end 

  local f13_sub = io.open(movie_path.."_t13"..sub_ext, "r")

  for line in f13_sub:lines() do
  local replace_line = string.gsub(line, "%s(%a)(%a)(%a)%s", " %3%2%1 ")     
  t14_file:write(replace_line .. "\n")
end 

local f14_sub = io.open(movie_path.."_t14"..sub_ext, "r")

for line in f14_sub:lines() do
local replace_line = string.gsub(line, "%s(%a)(%a)(%a)(%a)%s", " %4%3%2%1 ")     
t15_file:write(replace_line .. "\n")
end 

local f15_sub = io.open(movie_path.."_t15"..sub_ext, "r")

for line in f15_sub:lines() do
local replace_line = string.gsub(line, "%s(%a)(%a)(%a)(%a)(%a)%s", " %5%4%3%2%1 ")     
t16_file:write(replace_line .. "\n")
end 

local f16_sub = io.open(movie_path.."_t16"..sub_ext, "r")

for line in f16_sub:lines() do
local replace_line = string.gsub(line, "%s(%a)(%a)(%a)(%a)(%a)(%a)%s", " %6%5%4%3%2%1 ")     
t17_file:write(replace_line .. "\n")
end 

local f17_sub = io.open(movie_path.."_t17"..sub_ext, "r")

for line in f17_sub:lines() do
local replace_line = string.gsub(line, "%s(%a)(%a)(%a)(%a)(%a)(%a)(%a)%s", " %7%6%5%4%3%2%1 ")     
t18_file:write(replace_line .. "\n")
end 

local f18_sub = io.open(movie_path.."_t18"..sub_ext, "r")

for line in f18_sub:lines() do
local replace_line = string.gsub(line, "%s(%a)(%a)(%a)(%a)(%a)(%a)(%a)(%a)%s", " %8%7%6%5%4%3%2%1 ")     
t19_file:write(replace_line .. "\n")
end 

local f19_sub = io.open(movie_path.."_t19"..sub_ext, "r")

for line in f19_sub:lines() do
local replace_line = string.gsub(line, "%s(%a)(%a)(%a)(%a)(%a)(%a)(%a)(%a)(%a+)%s", " %9%8%7%6%5%4%3%2%1 ")     
t20_file:write(replace_line .. "\n")
end 

local f20_sub = io.open(movie_path.."_t20"..sub_ext, "r")

          for line in f20_sub:lines() do
          local replace_line = string.gsub(line, "^%sTTVBEW", "WEBVTT")     
          reverse_file:write(replace_line .. "\n")
      end 

      local reverse_new = (movie_path..".reverse"..sub_ext)

        org_sub:close()
        f1_sub:close()
        f2_sub:close()
        f3_sub:close()
        f4_sub:close()
        f5_sub:close()
        f6_sub:close()
        f7_sub:close()
        f8_sub:close()
        f9_sub:close()
        f10_sub:close()
        f11_sub:close()
        f12_sub:close()
        f13_sub:close()
        f14_sub:close()
        f15_sub:close()
        f16_sub:close()
        f17_sub:close()
        f18_sub:close()
        f19_sub:close()
        f20_sub:close()

        t1_file:close()
        t2_file:close()
        t3_file:close()
        t4_file:close()
        t5_file:close()
        t7_file:close()
        t8_file:close()
        t9_file:close()
        t10_file:close()
        t11_file:close()
        t12_file:close()
        t13_file:close()
        t14_file:close()
        t15_file:close()
        t16_file:close()
        t17_file:close()
        t18_file:close()
        t19_file:close()
        t20_file:close()

        os.remove(movie_path.."_t1"..sub_ext)
        os.remove(movie_path.."_t2"..sub_ext)
        os.remove(movie_path.."_t3"..sub_ext)
        os.remove(movie_path.."_t4"..sub_ext)
        os.remove(movie_path.."_t5"..sub_ext)
        os.remove(movie_path.."_t6"..sub_ext)
        os.remove(movie_path.."_t7"..sub_ext)
        os.remove(movie_path.."_t8"..sub_ext)
        os.remove(movie_path.."_t9"..sub_ext)
        os.remove(movie_path.."_t10"..sub_ext)
        os.remove(movie_path.."_t11"..sub_ext)
        os.remove(movie_path.."_t12"..sub_ext)
        os.remove(movie_path.."_t13"..sub_ext)
        os.remove(movie_path.."_t14"..sub_ext)
        os.remove(movie_path.."_t15"..sub_ext)
        os.remove(movie_path.."_t16"..sub_ext)
        os.remove(movie_path.."_t17"..sub_ext)
        os.remove(movie_path.."_t18"..sub_ext)
        os.remove(movie_path.."_t19"..sub_ext)
        os.remove(movie_path.."_t20"..sub_ext)

        mp.osd_message("Sub converted to Reverse")
        print("Sub converted to Reverse")
        mp.commandv('sub-add', reverse_new)

end

mp.add_key_binding(nil, "rename-subreverse", rename_subreverse)
mrfragger commented 2 weeks ago

rename-sublowercase.lua

--made by ayghub, edited by https://github.com/elhirchek

function path_ext(filename)
        local pattern = "%.%l+$"
        if filename then
          local i, j = string.find(filename, pattern)
          local path = string.sub(filename, 1, i-1)
          local ext = string.sub(filename, i, j )
          return path, ext
        end
end

function rename_sublowercase()
    local external_file = mp.get_property("current-tracks/sub/external-filename")
        local sub_path, sub_ext = path_ext(external_file)
        local movie_path, _ = path_ext(mp.get_property("path"))
        local org_sub = io.open(sub_path..sub_ext, "r")
        local lowercase_file = io.open(movie_path..".lowercase"..sub_ext, "w")
        local tempa_file = io.open(movie_path.."_tempa"..sub_ext, "w")

            for line in org_sub:lines() do
                local lowercase_line = string.lower(line)
                tempa_file:write(lowercase_line .. "\n")
            end

            local second_sub = io.open(movie_path.."_tempa"..sub_ext, "r")

            for line in second_sub:lines() do
              local replace_line = string.gsub(line, "webvtt", "WEBVTT")     
              lowercase_file:write(replace_line .. "\n")
                end

            local lowercase_new = (movie_path..".lowercase"..sub_ext)

        org_sub:close()
        second_sub:close()
        lowercase_file:close()        
        os.remove(movie_path.."_tempa"..sub_ext)
        mp.osd_message("Sub converted to lowercase")
        print("Sub converted to lowercase")
        mp.commandv('sub-add', lowercase_new)

end

mp.add_key_binding(nil, "rename-sublowercase", rename_sublowercase)