Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

how can I use captions for my video in this format? {"Timecode":"[00:00:00.42]","Caption":"Hello World\r\n\r\n"}

I have this Caption file, it is not in .srt, .ass, .ssa or vtt Format! it is .txt file but how can I make it displayed on my video?

1 Answer

You can read it in with the fs file reader and store it into a JavaScript Object.

From there you can access its properties and output it anywhere you like.

I have shown an example to the console. Using requireJS is the easiest way to use the file reader (on line one).

var fs = requirejs('fs');

                var Reader = {

                settings:{
                    input:'/Applications/XAMPP/xamppfiles/htdocs/project/app/caption.txt',
                    output:"",
                    data:null
                },
                init:function(){
                        var s=this.settings;
                        s.data =this.getInput(s.input);
                },
                getInput:function(input) {

                    fs.readFile(input, 'utf8', function (err,data) {
                      if (err) {
                         console.log(err);
                      }
                      var caption =  JSON.parse(data);
                        console.log("caption.Caption:" + caption.Caption);
                        console.log("caption.Timecode:" + caption.Timecode);
                        return caption;
                    })
                }                                     
        };

(function() {
    Reader.init();
})();

Output to console:

<p>caption.Timecode:[00:00:00.42]</p>

<p>caption.Caption:Hello World</p>

Thanks a lot but I don't understand much about JavaScript, I would rather convert it to .Srt format, so I can play it offline. How can I do that ?