[MPlayer-users] subtitles

Kuno Strassmann kuno.strassmann at gmx.ch
Tue May 18 16:36:46 CEST 2004


hi folks

mplayer is great, thanks!

I use it often to play movies with subtitles. Because I also write subtitles
myself, I wrote an emacs-tool to do the timing using mplayer. It's only a
hack, but I like it...

Maybe this will be useful to someone else. It works by launching mplayer
>from within emacs. The display times for the subtitles can be recorded by
pausing mplayer repeatedly. At the end, a template subtitle file (SubViewer
format) can be generated, or the times can be used to process a file with
the subtitle lines into a working subtitle file. This makes the process of
creating subtitles really much more enjoyable!

If anybody decides to try it, I would enjoy receiving feedback. Thanks.

Kuno

;;Code following below

;; subtitles.el -  a utility to interactively prepare a subtitle-file in
SubViewer format using mplayer 
;;
;; Version 0.1
;;
;; license granted according to the terms of the GNU Public License
;;
;;;; this programm can be used to record and process the timing for
subtitles
;;;; while watching a movie with mplayer (only tested on my machine...)
;;
;;;; usage
;;
;;  load this file into emacs (M-x load-file)
;;
;;  M-x subtitle-start will
;;    1. offer an editable default set of options to pass to mplayer
;;    2. ask for the movie file name
;;    3. ask for the time position at which to start playing
;;
;;  While playing the movie, the times at which subtitles should be
displayed
;;  can be recorded by typing (and releasing) pause (i.e. hitting the
;;  spacebar). While the movie is running, emacs remains active and can be
used
;;  e.g. to navigate the subtitle file for comparison with the movie
(preferably
;;  during pauses).  Pauses between the subtitles are calculated
automatically
;;  (and can be adjusted later).
;;
;;  Recording is stopped by quitting mplayer (hitting q). The user will be
asked
;;  if he/she wants to process the recorded timing with an existing
;;  subtitle-file, or create a template file with dummy subtitles.
;;
;;  It is recommended to prepare a subtitle file without the timing and use
;;  subtitle.el to do the timing.  The file should look like this before...
;;  -------------------
;;    Jack...
;;
;;    Yes, Bonny
;;
;;    I love you!
;;  -------------------
;;  
;;  ...and after...
;;
;;  ---------------------------------
;;    1
;;    00:00:01,500 --> 00:00:03,500
;;    Jack...
;;
;;    2
;;    00:00:03,500 --> 00:00:05,0
;;    Yes, Bonny
;;
;;    3
;;    00:00:05,0 --> 00:00:09,0
;;    I love you!
;;   --------------------------------
;;
;; Such a subtitle file can be edited in several sessions.
;;
;;
;; IMPORTANT: Starting at the right position in the subtitles file is a bit
;; tricky. The program goes to WHERE THE MARK WAS SET LAST (C-Space), and
gives
;; the user the chance to skip forward to the right starting position.
;;
;;
;; enjoy,
;; Kuno
;; 
;; kuno.strassmann at gmx.ch


(setq mplayer-command "mplayer")
(setq mplayer-options "-utf8 -autosync 33 -v -vo x11")
(setq mplayer-file "zima.v.prostokvashino.avi")
(set-variable 'sub-padding -0.2)

(defun subtitle-start ()
  ""
  (interactive)
  ;;options erfragen
  (setq mplayer-options 
		(read-from-minibuffer "options for mplayer: " mplayer-options))
  ;;file erfragen
  (setq mplayer-file (read-file-name "movie file: " "" mplayer-file t))
  (while (when (not (numberp 
					 (setq mplayer-startingtime (read-minibuffer "start playing at time
(s): " "0"))))
					 (message "please enter a number") (sit-for 2)))
  ;;file spielen
  (start-process-shell-command
	 "movie"
	 nil
	 (format
	  "2>/dev/null %s -ss %s %s %s | sed 's/
/\\n/g' |grep -B 2 PAUSE | awk '/[0-9.]/ {print $2}'"
	  mplayer-command
	  mplayer-startingtime 
	  mplayer-options
	  mplayer-file
	  )
	 )
  (setq sub-times nil)
  (set-process-filter (get-process "movie") 'keep-output);;setzt sub-times
  ;;resultat lesen
  (set-process-sentinel (get-process "movie") 'movie-fin)
  )

;;filter function
(defun keep-output (process output) 
  (setq sub-times output)
  )
;;sentinel
(defun movie-fin (process event)
  (catch 'end
	(if (not sub-times) (throw 'end (progn (beep) (message "no times marked")
(sit-for 2)))
	  (setq sub-times-list (eval (read (format "(list %s)" sub-times))))
	  (setq sub-times-list ;; add last time element
			(append sub-times-list (list (+ 6 (car (last sub-times-list))))))
	  (if (y-or-n-p "timing of existing subtitle-file? ")
		  (subtitle-timing)
		(if (y-or-n-p "create a subtitle template file (or quit)? ")
			(subtitle-template)
		  (message "see you later...")
		  (sit-for 1.5))
		)
	  )
	)
	(message "thanx for using me! bye.")
  )

(setq subreg "^\[ \]*\n.*?\[^ \n\]+?.*")
(defun subtitle-timing ()
  "to process the subtitles, they must be separed by blank lines"
  (find-file (read-file-name "subtitle file: " "" mplayer-file))
  (set-mark-command 1);jump back to last mark
  (search-forward-regexp "[^ ]") ;;first non-blank char
  (beginning-of-line)
  (catch 'check
	(while 
	  (let ((answer (y-or-n-p-with-timeout "start here? " 10 'cancel)))
		(cond
		 ((eq answer 'cancel) (throw 'check (progn (beep) (message "timed out...")
(sit-for 2))))
		 ((not answer))
		 ))
	(search-forward-regexp subreg nil t)
	(beginning-of-line)
	)	
	(while (when (not (numberp (setq zaehler (read-minibuffer "starting number:
" "1")))) 
			 (message "please enter a number") (sit-for 2)))
	(setq sub-times-list-temp sub-times-list)
	(while (cdr sub-times-list-temp)
	  (subtitle-times)
	  (insert-string (typeset-title subtime-1 subtime-2 zaehler))
	  (setq sub-times-list-temp (cdr sub-times-list-temp))
	  (setq zaehler (+ 1 zaehler))
	  (search-forward-regexp subreg nil t)
	  (beginning-of-line)
	  )
	)
  )

(defun subtitle-template ()
  (get-buffer-create "subtitles")
  (switch-to-buffer (get-buffer-create "subtitles"))
  (insert-string 
   (format "# Subtitle-Template fr %s\n\n" mplayer-file))
  (setq zaehler 1)
  (setq sub-times-list-temp sub-times-list)
  (while (cdr sub-times-list-temp)
	(subtitle-times)
	(insert-string (typeset-title subtime-1 subtime-2 zaehler "hello world"))
	(setq sub-times-list-temp (cdr sub-times-list-temp))
	(setq zaehler (+ 1 zaehler))
	(search-forward-regexp subreg nil t)
	(beginning-of-line)
	)
  )

(defun subtitle-times ()
  "changes the values of subtime-1 and subtime-2"
  (setq subtime-1 (car sub-times-list-temp))
  (setq subtime-2 
		(min
		 (- (cadr sub-times-list-temp) .1);not longer than next one
		 (+ subtime-1 2 ;at least 2 s
			(min (* .5 (- (cadr sub-times-list-temp) subtime-1 2)) 4))))
  ;;plus half of the remaining time, but at most 4 s
  )


(defun typeset-title (subtime-1 subtime-2 zaehler &optional subtitle)
  "typesets one title template at time seconds (float), 2 s long"
  (concat 
   (format 
   "\n%d\n%s,%s --> %s,%s\n"
   zaehler
   (format-time-string "%H:%M:%S" (list 1 (floor (+ 17264 subtime-1))))
   (round (* 1000 (- subtime-1 (floor subtime-1))))
   (format-time-string "%H:%M:%S" (list 1 (floor (+ 17264 subtime-2))))
   (round (* 1000 (- subtime-2 (floor subtime-2))))
   )
   (if subtitle (format "%s\n\n" subtitle))
   )
  )

;;end of code



-- 
"Sie haben neue Mails!" - Die GMX Toolbar informiert Sie beim Surfen!
Jetzt aktivieren unter http://www.gmx.net/info



-- 
"Sie haben neue Mails!" - Die GMX Toolbar informiert Sie beim Surfen!
Jetzt aktivieren unter http://www.gmx.net/info




More information about the MPlayer-users mailing list