Progress Updates

I spent some time today polishing up the basic video create/view/update/delete pages so they are a little easier to use. Not being a great CSS designer, I opted to use the Blueprint CSS framework [released under the MIT Lisense] to make things easier… I’ve never been a fan of manually crafting stylesheets for very complicated pages, and knowing that someone else will deal with the various browser-compatability hacks is a big relief. In addition to Blueprint, I’m using the Silk icon set from FamFamFam. I think I’ve used this icon set in just about every project I’ve done that doesn’t someone who is a “designer”. I was pleasently surprised to see they have icons for film.

On the coding front of things, the simple management (CRUD) of videos and collections is complete. I need to do a bit more work for adding additional videos to a collection, which I hope to have done by Wedneday… then I’m onto thumbnailing and conversion.

Nested Attributes

The regular disclaimer applies to this post: I’m not a rails expert, so I may not be presenting the “best practices” technique, or a technique that will work for everyone all the time. This works for me, in my use case, and I don’t see any obvious reasons why it wouldn’t work for others.

I have two models I’m working with right now in my project, a collection and a video. A collection is essential a bundle of similar videos that share a title, description, date, etc. This supports the concept multiple formats or slight variations of the same video being used in the system. Its a pretty standard setup in my models:

==Collection.rb==

class Collection < ActiveRecord::Base
has_many :videos
end

==Video.rb==

class Video < ActiveRecord::Base
belongs_to :collection
end

So every entry in the video table has an id in the collection_id field that enables that join to take place. Now its time to get a form that will create a collection and the first video in the collection at the same time… initially I spend time trying to generate a text field (using the text_field form helper) that would be called collection[uploaded_data] {uploaded_data is the field I use on my new video form} but I just couldn’t get that to work. I can’t remember where that naming syntax is used.. maybe its CakePHP, Concerto, or even a different rails technique.. but it wasn’t working.

Googling around for “nested attributes” seem to yield some interesting results, and I stubled onto the accepts_nested_attributes_for concept. I changed my collection model to something like this:

class Collection < ActiveRecord::Base
has_many :videos
accepts_nested_attributes_for :videos
end

Now, I had to add a line (@collection.videos.build) to my collections controller so that it will create a new video object:

def new
@collection = Collection.new
@collection.videos.build

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @collection }
end
end

Last, but not least, I updated updated my form with the following stuff:

<% form_for (@collection,:html => { :multipart => true }) do |f| -%>
<%= f.error_messages %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %>
<%= f.text_area :description %>
</p>
<p>
<% f.fields_for :videos do |video_fields| %>
<%= video_fields.label :uploaded_data %>
<%= video_fields.file_field :uploaded_data %>
<% end %>
</p>
<p>
<%= f.submit ‘Save’ %>
</p>
<% end -%>

Now the save stuff is all handled by the exact same save I used for the regular collection entry, no need to updated that. Whew!

Here are some additional resources I found helpful:

acts-as-timecode

In a few places I need to store the length of a video. To keep things simple and fast, I’m using an :integer, and storing the length in seconds of the video file. This is all well and good when it comes to indexing and sorting in the database, when it comes to user interaction its really junky. No one wants to know that a video 421 seconds long is 7 minutes, and 1 second long… and I didn’t want people to have to convert 3 hours, 45 minutes, and 10 seconds into a huge integer.

At first I implemented it up quick and dirty in the edit code of controller for the portion of code that needed this conversion, however as I started to build out a few other items I realized I was going to be needing the same feature so I moved it to a plugin.

You should note, this is my first time authoring a plugin and my first time trying to make it into a gem. It works for me, so I would hope it can work for you too. Its pretty straight-forward to setup and use. Here’s my model:

class Video < ActiveRecord::Base

acts_as_timecode :column => :duration

end

Now I can use this cool new timecode field in my views and controllers. For example, in my edit.html.erb I use <%= f.text_field :timecode %>. which generates a textbox with the video duration in it, looking like 00:34:12 or however long the video is. Hitting the save button works as expected, updating the duration field in my database to the correct seconds value (2052 in this case). Because I don’t always like to type leading zeros, the timecode field can take the following formats: HH:MM:SS:FF, HH:MM:SS, MM:SS, SS. The frame implementation isn’t very useful, but it will round your frames to the nearest second value, based on the :fps configuration setting (defaulted at 30).

At some point I might expand it, but it will depend on what I need it to do.

You can check it out on GitHub: http://github.com/bamnet/acts-as-timecode/

Week 2 Slides

Here are the slides I presented this week at the RCOS meeting. I got some good feedback about the meta data and search functionality. I broke rails on my development server this morning (accidentally chowned /var) so I’ve got to spend another 2-3 hours fixing that. I suspect I’ll have some code committed to git next week when I’ve got some basic upload stuff working.

To database or not to database

For the past week or so I’ve been thinking about the storage of video files, and how to implement a storage system that is fast, effecient, secure, and reliable. My big question here is to use a database or not to use a database. I’m likely using MySQL for everything else, but video files are a bit larger than you VARCHAR(255) or even larger than a photo gallery image I’d store in a database. I’m concerned that throwing video files, ranging from 100MB to 5GB in a database might start to slow things down… which I really don’t want to happen. On the other hand, I can’t afford to have video files laying around in violation of a database foreign constraint, which might happen if a user deletes a video and the file delete fails (maybe its in use).

I’ve done some work storing large datasets in MySQL, but no work storing >25MB in a single field. Of course there would have to be a seperate media table, to prevent a stray ‘SELECT * FROM videos’ fr0m crashing the whole thing. but that still doesn’t convince me it will be as fast as it needs to be.

Another nice plus on the filesystem side is the mod_flv and mod_h264 streaming plugins for Apache. I know you can write a version of the flv streaming module in PHP, but I don’t see it having the speed of an Apache plugin.

I think ideally I’d like to store videos in the database or using something besides the regular flat file approach. Using Ruby on Rails might make this a moot point from a programming perspective, attachment_fu easily handles both file and database datastores.

If you have any experience storing large files in a database, I’d love to hear about it.

Week 1 Slides

Here are some slides detailing the project objectives and timeline. I’m unsure if I’m going to be able to make it to the Friday meeting, but I’ll be continuing to make progress over the weekend. Hopefully mid/late next week there will be some code pushed to git for all to see!

Greetings.

Hi everyone! I recently heard back from the RCOS leaders and my proposal has been accepted for development of an open source digital video server. In the next few days I’m going to be working out a rough schedule and getting my hands dirty with some code. As soon as I’ve got more to share I’ll post it here!