Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Apple Script for automatically changing time codes in a text document

Hi, I have a bunch of what are called LRC files that I need to fix and would love to be able to do it automatically. LRC files are text files with time codes and lyrics and are named "songtitle.lrc". So for example the LRC for the song Happy would be called "Happy.lrc" and if you open it up with a text editor it looks like this:


[ti:HAPPY]

[ar:PHARRELL WILLIAMS]

[la:en]

[00:20.08]<00:21.08>IT MIGHT SEEM CRAZY

[00:22.41]<00:22.41>WHAT IM ABOUT TO SAY

[00:26.93]<00:26.93>SUNSHINE SHES HERE

[00:28.37]<00:28.37>YOU CAN TAKE A BREAK

[00:32.16]<00:32.16>IM A HOT AIR BALLOON


and this continues for the whole song, but I think you get the idea. What I need to do is change all of the time codes by the same amount. Specifically I want to be able to subtract a certain amount of time from every line in the lrc file. So somewhere in the script I could designate the amount of seconds I'd like to subtract and then the script would take the lrc file and subtract that amount of time from every line. For example if I entered 8 seconds and pointed it to the lrc file for the song happy it would output:


[ti:HAPPY]

[ar:PHARRELL WILLIAMS]

[la:en]

[00:12.08]<00:13.08>IT MIGHT SEEM CRAZY

[00:14.41]<00:14.41>WHAT IM ABOUT TO SAY

[00:18.93]<00:18.93>SUNSHINE SHES HERE

[00:20.37]<00:20.37>YOU CAN TAKE A BREAK

[00:24.16]<00:24.16>IM A HOT AIR BALLOON


and so on. Unfortunately I don't know any Apple Script so I wouldn't know where to begin. So my question is: is this script feasible? easy? If it is feasible I'd appreciate any help with figuring out how to write it. If you know any of the commands that I will need to utilize please let me know. Or if you can help me write the script or parts of it, that would be amazing. Also if AppleScript can only manipulate text documents in this manner, I can easily paste the lrc file into a text document and use that for editing and then turn it back into an lrc file.


Thank you for your help

MacBook Pro, Mac OS X (10.7.5), null

Posted on Apr 15, 2015 3:00 PM

Reply
Question marked as Best reply

Posted on Apr 15, 2015 3:13 PM

Use code such as:


set the_text to "[ti:HAPPY]

[ar:PHARRELL WILLIAMS]

[la:en]

[00:20.08]<00:21.08>IT MIGHT SEEM CRAZY

[00:22.41]<00:22.41>WHAT IM ABOUT TO SAY

[00:26.93]<00:26.93>SUNSHINE SHES HERE

[00:28.37]<00:28.37>YOU CAN TAKE A BREAK

[00:32.16]<00:32.16>IM A HOT AIR BALLOON"

set new_text to ""

repeat with this_paragraph from 1 to 3

set new_text to new_text & paragraph this_paragraph of the_text & return

end repeat

repeat with this_paragraph from 4 to (count paragraphs of the_text)

set the_paragraph to paragraph this_paragraph of the_text

set new_number to (items 5 thru 6 of the_paragraph as string as number) - 8 --the 8 is the time in seconds

set new_text to new_text & ((items 1 thru 4 of the_paragraph) & new_number & (items 7 thru 14 of the_paragraph) & new_number & (items 17 thru -1 of the_paragraph)) & return as string

end repeat


(126054)

2 replies
Question marked as Best reply

Apr 15, 2015 3:13 PM in response to thundercleese8

Use code such as:


set the_text to "[ti:HAPPY]

[ar:PHARRELL WILLIAMS]

[la:en]

[00:20.08]<00:21.08>IT MIGHT SEEM CRAZY

[00:22.41]<00:22.41>WHAT IM ABOUT TO SAY

[00:26.93]<00:26.93>SUNSHINE SHES HERE

[00:28.37]<00:28.37>YOU CAN TAKE A BREAK

[00:32.16]<00:32.16>IM A HOT AIR BALLOON"

set new_text to ""

repeat with this_paragraph from 1 to 3

set new_text to new_text & paragraph this_paragraph of the_text & return

end repeat

repeat with this_paragraph from 4 to (count paragraphs of the_text)

set the_paragraph to paragraph this_paragraph of the_text

set new_number to (items 5 thru 6 of the_paragraph as string as number) - 8 --the 8 is the time in seconds

set new_text to new_text & ((items 1 thru 4 of the_paragraph) & new_number & (items 7 thru 14 of the_paragraph) & new_number & (items 17 thru -1 of the_paragraph)) & return as string

end repeat


(126054)

Apr 19, 2015 8:11 AM in response to thundercleese8

Hello


Unfortunately, I'm afraid, the provided code won't yield valid time code for general cases.


E.g., Given time shift = -8 seconds,

[01:05.00] will be changed to [01:-3.00] where it should be [00:57.00],

[00:12.00] will be changed to [00:4.00] where it should be [00:04.00],

etc.



Here's an Automator workflow which should treat these cases correctly.


It will let you to choose LRC file(s) and specify time shift amount in hundredths of a second (e.g., -800 for -8 seconds) and then edit the chosen files as specified in place. Please make sure you have backup of original files because the script will orverwrite them.



The Automator workflow consists of the following actions.



1) Run AppleScript action

- Code as follows



set ff to choose file with prompt "Choose LRC file(s)" with multiple selections allowed repeat display dialog "Enter shift amount [1/100 sec]" default answer "100" set t to text returned of result try set n to t as integer exit repeat on error -- display dialog "Invalid number: " & t end try end repeat return {n as string} & ff





2) Run Shell Script action

- Shell = /usr/bin/ruby

- Pass inputs = as arguments

- Code = as follows



#!/usr/bin/ruby -w # # ARGV[0] : time shift amount in hundredths of second, e.g., 800, +800, -800, -825, etc # ARGV[1..] : file [file ...] [output_directory] # # * if output_directory is specified it must exist in advance. # * if output_directary is not specified, input files are overwritten. # # written by Hiroto, 2015-04 # def usage $stderr.puts "Usage: #{File.basename($0)} shift_in_hundredths_second file [file ...] [output_directory]" exit 1 end usage unless ARGV.length > 1 outdir = File.directory?(ARGV.last) ? ARGV.pop : nil usage unless ARGV.length > 1 delta = ARGV.shift.to_f ARGV.each do |a| # read the entire content of input file t = '' File.open(a) { |f| t = f.read } # process the text t.gsub!(/([-+])? ([0-9]{2,}) : ([0-9]{2}) (\.|,) ([0-9]{2})/x) do sg = $~[1] == '-' ? -1 : 1 ds = $~[4] m, s, z = $~.values_at(2, 3, 5).map { |e| e.to_i } u = sg * (m * 6000 + s * 100 + z) + delta if u < 0 u = -u "-%02d:%02d%s%02d" % [u / 6000, u % 6000 / 100, ds, u % 100] else "%02d:%02d%s%02d" % [u / 6000, u % 6000 / 100, ds, u % 100] end end print "%s\n%s\n" % [a, t] # for test # write the processed text to output file if outdir File.open("%s/%s" % [outdir, File.basename(a)], "w") { |f| f.print t } else File.open(a, "w") { |f| f.print t } end end





The resulting workflow will look something like this.


User uploaded file




Briefly tested under OS X 10.6.8.


Good luck,

H


EDIT: fixed typos

Apple Script for automatically changing time codes in a text document

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.