parse srt file

Subtitle file is very needful to watch a movie of different languages. If you want to save a srt file in database, you need to parse it. Here I am describing how to parse a srt file and insert it in wordpress database. Please follow the instructions one by one.

Step 01 : Make a php file named srt_parse.php
<?php
//make shortcode for upload form

function get_srt_form($atts, $content = null)
{
    $form = '<form action="#" method="post" enctype="multipart/form-data">
                <label for="srt_file">Upload srt file</label>
                <input type="file" name="srt_file" id="srt_file" />
                <input type="submit" name="srt_file_upload" >
            </form>';
    return $form;
}
add_shortcode( 'srt-form', 'get_srt_form' );

//make functions for parsing srt file
if(isset($_POST['srt_file_upload'])){
    //call the parse function
    parseSrt($_FILES['srt_file']['tmp_name']);
}
/*
*functions for parsing srt file
*/
function parseSrt($file)
{
    define('SRT_STATE_SUBNUMBER', 0);
    define('SRT_STATE_TIME',      1);
    define('SRT_STATE_TEXT',      2);
    define('SRT_STATE_BLANK',     3);

    $lines   = file($file);
    $subs    = array();
    $state   = SRT_STATE_SUBNUMBER;
    $subNum  = 0;
    $subText = '';
    $subTime = '';
   //
    foreach($lines as $line) {
        switch($state) {
            case SRT_STATE_SUBNUMBER:
                $subNum = trim($line);
                $state  = SRT_STATE_TIME;
                break;
            case SRT_STATE_TIME:
                $subTime = trim($line);
                $state   = SRT_STATE_TEXT;
                break;
            case SRT_STATE_TEXT:
                if (trim($line) == '') {
                    $sub = new stdClass;
                    $sub->number = $subNum;
                    list($sub->startTime, $sub->stopTime) = explode(' --> ', $subTime);
                    $sub->text   = $subText;
                    $subText     = '';
                    $state       = SRT_STATE_SUBNUMBER;
                    $subs[]      = $sub;
                } else {
                    $subText .= $line;
                }
                break;
        }
    }
    if ($state == SRT_STATE_TEXT) {
        // if file was missing the trailing newlines, we'll be in this
        // state here.  Append the last read text and add the last sub.
        $sub->text = $subText;
        $subs[] = $sub;
    }
    //insert the verses into database
    global $wpdb;
    $table_name = 'uploaded_srts';
    foreach ($subs as $key=>$subtitle) {
        $data_array = [
            'timing'=> $sub->startTime." --> ".$sub->stopTime,
            'text'=>$subtitle->text
        ];
        $result = $wpdb->insert($table_name, $data_array, $format=null);
    }
}
Step 02: Include the file in functions.php
/**
 * Functions and shortcode for upload and parsing srt file.
 */
require get_template_directory() . '/srt_parse.php';
Step 03 : Make a table in your wordpress database with following sql code
CREATE TABLE `uploaded_srts` (
  `id` int(11) NOT NULL,
  `text` varchar(300) NOT NULL,
  `timing` varchar(300) NOT NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `uploaded_srts`
  ADD PRIMARY KEY (`id`);
ALTER TABLE `uploaded_srts`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT;
Step 04 : You have all done just add a page and add the shortcode [srt-form] and save the page. Now open the page and upload a srt file and click upload. Then check in databases all the lines of srt file have uploaded.

Please make comment if you have any difficulties. Also share your ideas with a post. Just register to share your ideas.

Leave a Reply

Your email address will not be published. Required fields are marked *