From: <die...@us...> - 2010-02-01 19:03:55
|
Revision: 1751 http://openutils.svn.sourceforge.net/openutils/?rev=1751&view=rev Author: diego_schivo Date: 2010-02-01 19:03:44 +0000 (Mon, 01 Feb 2010) Log Message: ----------- MEDIA-67 Parse flv files for metadata at upload Modified Paths: -------------- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/types/impl/VideoTypeHandler.java Added Paths: ----------- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/FLVMetaData.java Modified: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/types/impl/VideoTypeHandler.java =================================================================== --- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/types/impl/VideoTypeHandler.java 2010-02-01 17:44:41 UTC (rev 1750) +++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/types/impl/VideoTypeHandler.java 2010-02-01 19:03:44 UTC (rev 1751) @@ -19,6 +19,19 @@ package net.sourceforge.openutils.mgnlmedia.media.types.impl; +import info.magnolia.cms.core.Content; +import info.magnolia.cms.util.ContentUtil; +import info.magnolia.cms.util.NodeDataUtil; +import info.magnolia.context.MgnlContext; +import info.magnolia.context.WebContext; + +import javax.servlet.http.HttpServletRequest; + +import net.sourceforge.openutils.mgnlmedia.media.utils.FLVMetaData; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Type handler for flv videos * @author molaschi @@ -27,4 +40,40 @@ public class VideoTypeHandler extends MediaWithPreviewImageTypeHandler { + /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(VideoTypeHandler.class); + + @Override + public boolean onPostSave(Content media) + { + try + { + HttpServletRequest request = ((WebContext) MgnlContext.getInstance()).getRequest(); + String baseUrl = request.getScheme() + "://" + request.getServerName(); + if ("http".equals(request.getScheme()) && request.getServerPort() != 80) + { + baseUrl += ":" + request.getServerPort(); + } + + String url = baseUrl + request.getContextPath() + getUrl(media); + FLVMetaData flvMetaData = new FLVMetaData(url, true); + + Content original = ContentUtil.getContent(media, "original"); + if (original != null) + { + NodeDataUtil.getOrCreateAndSet(original, "width", flvMetaData.getWidth()); + NodeDataUtil.getOrCreateAndSet(original, "height", flvMetaData.getHeight()); + NodeDataUtil.getOrCreateAndSet(original, "duration", flvMetaData.getDuration()); + original.save(); + } + } + catch (Exception e) + { + log.error(e.getMessage(), e); + } + return super.onPostSave(media); + } + } Added: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/FLVMetaData.java =================================================================== --- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/FLVMetaData.java (rev 0) +++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/FLVMetaData.java 2010-02-01 19:03:44 UTC (rev 1751) @@ -0,0 +1,300 @@ +/** + * + * Magnolia SimpleMedia Module (http://www.openmindlab.com/lab/products/media.html) + * Copyright (C)2008 - 2010, Openmind S.r.l. http://www.openmindonline.it + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package net.sourceforge.openutils.mgnlmedia.media.utils; +import java.io.InputStream; +import java.io.FileInputStream; +import java.nio.ByteBuffer; +import java.net.URL; +import java.net.HttpURLConnection; +import java.text.DecimalFormat; + +/** + * This class will get the FLV meta data information. + * @author SANTHOSH REDDY MANDADI + * @version 1.0 + * @since 10th June 2009 + */ +public class FLVMetaData +{ + /** + * Represents the FLV path either a URL or a absolute file path. If it is URL + * url property should be true to access the file from web + */ + private String flv=null; + + /** + * Represents whether the FLV is remote or local, if this is true + * data will be fetched through the HTTP connection otherwise data will + * be fetched from the local file. + */ + private boolean url=true; + + //All the FLV properties + private String duration; + private double width; + private double height; + private double audioDataRate; + private double videoDataRate; + private double fileSize; + private String createdDate; + private String mimeType; + private double frameRate; + + /** + * Constructs an object with flv as a url + * @param flv represents the web url + * @since 10-Jun-2009 + */ + public FLVMetaData(String flv) throws Exception + { + this.flv=flv; + getMetaData(); + } + + /** + * Constructs an object with flv and boolean value url. + * @param flv represents the FLV path either a URL or a absolute file path. + * @param url represents boolean value. + */ + public FLVMetaData(String flv, boolean url) throws Exception + { + this.flv=flv; + this.url=url; + getMetaData(); + } + + /** + * Extract the metadata for the flv and sets them in the properties. + * If the property has 0.0 or null, then the information is not available on + * the target FLV. + * @throws Exception if something goes wrong. + */ + private void getMetaData() throws Exception + { + InputStream fis=null; + try{ + if(url) + { + //Creating the URL object + URL url = new URL(flv); + //Establishing the connection to the server + HttpURLConnection connection=(HttpURLConnection)url.openConnection(); + //Getting the remote input stream + fis=connection.getInputStream(); + } + else + { + fis=new FileInputStream(flv); + } + //Creating the bytes array to read the first 400 bytes data from input stream + byte bytes[]=new byte[400]; + //Reading the data from the input stream + fis.read(bytes); + + /*Fetching the properties. If the output shows -1 or null then + consider that the FLV doesn't have that info on metadata*/ + double duration=getDouble(bytes,"duration"); + DecimalFormat f = new DecimalFormat("00"); + setDuration(f.format((int)duration/60)+":"+f.format((int)duration%60)); + setWidth(getDouble(bytes,"width")); + setHeight(getDouble(bytes,"height")); + setAudioDataRate(getDouble(bytes,"audiodatarate")); + setVideoDataRate(getDouble(bytes,"videodatarate")); + setFileSize(getDouble(bytes,"filesize")); + setCreatedDate(getString(bytes,"creationdate")); + setMimeType(getString(bytes,"mimetype")); + setFrameRate(getDouble(bytes,"framerate")); + + //Closing the remote input stream + fis.close(); + }catch(Exception e) { + throw new Exception(e); + } + finally { + if(fis!=null) { + fis.close(); + } + } + } + private double getDouble(byte[] bytes, String property) + { + //Creating a string from the bytes + String metaData=new String(bytes); + //Checking whether the property exists on the metadata + int offset=metaData.indexOf(property); + if(offset!=-1) + { + //Calculating the value from the bytes received from getBytes method + return ByteBuffer.wrap(getBytes(bytes, offset+property.length()+1, 8)).getDouble(); + } + else + { + //Returning -1 to notify the info not available + return -1; + } + } + private String getString(byte[] bytes, String property) + { + //Creating a string from the bytes + String metaData=new String(bytes); + //Checking whether the property exists on the metadata + int offset=metaData.indexOf(property); + if(offset!=-1) + { + //Constructing the string from the bytes received from getBytes method + return new String(getBytes(bytes, offset+property.length()+3, 24)); + } + else + { + //Returning null to notify the info not available + return null; + } + } + private byte[] getBytes(byte[] bytes, int offset, int length) + { + //Fetching the required number of bytes from the source and returning + byte requiredBytes[]=new byte[length]; + for(int i=offset, j=0;j<length;i++,j++) + { + requiredBytes[j]=bytes[i]; + } + return requiredBytes; + } + + public void setFlv(String flv) + { + this.flv = flv; + } + + public String getFlv() + { + return flv; + } + + public void setUrl(boolean url) + { + this.url = url; + } + + public boolean isUrl() + { + return url; + } + + public void setCreatedDate(String createdDate) + { + this.createdDate = createdDate; + } + + public String getCreatedDate() + { + return createdDate; + } + + public void setMimeType(String mimeType) + { + this.mimeType = mimeType; + } + + public String getMimeType() + { + return mimeType; + } + + public void setWidth(double width) + { + this.width = width; + } + + public double getWidth() + { + return width; + } + + public void setHeight(double height) + { + this.height = height; + } + + public double getHeight() + { + return height; + } + + public void setAudioDataRate(double audioDataRate) + { + this.audioDataRate = audioDataRate; + } + + public double getAudioDataRate() + { + return audioDataRate; + } + + public void setVideoDataRate(double videoDataRate) + { + this.videoDataRate = videoDataRate; + } + + public double getVideoDataRate() + { + return videoDataRate; + } + + public void setFileSize(double fileSize) + { + this.fileSize = fileSize; + } + + public double getFileSize() + { + return fileSize; + } + + public void setFrameRate(double frameRate) + { + this.frameRate = frameRate; + } + + public double getFrameRate() + { + return frameRate; + } + + public void setDuration(String duration) + { + this.duration = duration; + } + + public String getDuration() + { + return duration; + } + + public static void main(String args[]) throws Exception + { + FLVMetaData metaData=new FLVMetaData("http://domain/o18/HCINTL/LIVE/72224/myhc_200.flv"); + System.out.println(metaData.getDuration()); + metaData.setUrl(false); + metaData.setFlv("1.flv"); + metaData.getMetaData(); + System.out.println(metaData.getDuration()); + } +} \ No newline at end of file Property changes on: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/FLVMetaData.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |