dohsoft
November 4, 2015, 3:41pm
#1
Hi,
I have a requirement as a part of an import routine I am writing to get the duration of the uploaded video so this value can be stored on a local database.
I am using the following code to do the upload and this is working fine, the duration of the video is visible in the KMC when I look at the details for the video:
FileStream fileStream = new FileStream(context.Server.MapPath("~") + @"" + fileName, FileMode.Open, FileAccess.Read);
KalturaClient client = new KalturaClient(GetConfig());
string ks = client.GenerateSession(ADMIN_SECRET, USER_ID, KalturaSessionType.ADMIN, PARTNER_ID, 86400, "");
client.KS = ks;
KalturaUploadToken uploadToken = client.UploadTokenService.Add();
client.UploadTokenService.Upload(uploadToken.Id, fileStream);
KalturaUploadedFileTokenResource mediaResource = new KalturaUploadedFileTokenResource();
mediaResource.Token = uploadToken.Id;
KalturaMediaEntry mediaEntry = new KalturaMediaEntry();
mediaEntry.Name = ds.Tables[0].Rows[0]["Synopsis"].ToString();
mediaEntry.ReferenceId = prefix + "-" + objectid;
mediaEntry.MediaType = KalturaMediaType.VIDEO;
mediaEntry = client.MediaService.Add(mediaEntry);
mediaEntry = client.MediaService.AddContent(mediaEntry.Id, mediaResource);
When I then go to the mediaEntry object to get .Duration it is always 0.
Any ideas please?
Thanks,
Steve
jess
November 4, 2015, 5:36pm
#2
Hi Steve,
The KalturaMediaEntry object has the members:
duration
msDuration
I could not reproduce a situation in which they show 0 whereas the file actually has duration.
Does msDuration also return 0? can you share your code?
dohsoft
November 5, 2015, 9:47am
#3
Hi Jesse,
Thanks for your reply. Yes both duration and msDuration return 0. My code is pasted below
try
{
string fileName = HttpContext.Current.Request.QueryString["FileName"].ToString();
fileName = SanitizeFileName(fileName);
using (FileStream fs = File.Create(context.Server.MapPath("~") + @"\" + fileName))
{
Byte[] buffer = new Byte[32 * 1024];
int read = context.Request.GetBufferlessInputStream().Read(buffer, 0, buffer.Length);
while (read > 0)
{
fs.Write(buffer, 0, read);
read = context.Request.GetBufferlessInputStream().Read(buffer, 0, buffer.Length);
}
}
string objectid = HttpContext.Current.Request.QueryString["objectid"];
string objectTypeid = HttpContext.Current.Request.QueryString["objectTypeid"];
string prefix = HttpContext.Current.Request.QueryString["prefix"];
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString);
// Get details saved already for this video which are then used
// when importing the video
SqlDataAdapter da = new SqlDataAdapter("Select * from tbl_D2_Object where objectid=" + objectid + " and objecttypeid=" + objectTypeid, conn);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
FileStream fileStream = new FileStream(context.Server.MapPath("~") + @"\" + fileName, FileMode.Open, FileAccess.Read);
KalturaClient client = new KalturaClient(GetConfig());
string ks = client.GenerateSession(ADMIN_SECRET, USER_ID, KalturaSessionType.ADMIN, PARTNER_ID, 86400, "");
client.KS = ks;
KalturaUploadToken uploadToken = client.UploadTokenService.Add();
client.UploadTokenService.Upload(uploadToken.Id, fileStream);
KalturaUploadedFileTokenResource mediaResource = new KalturaUploadedFileTokenResource();
mediaResource.Token = uploadToken.Id;
KalturaMediaEntry mediaEntry = new KalturaMediaEntry();
mediaEntry.Name = ds.Tables[0].Rows[0]["Synopsis"].ToString();
mediaEntry.ReferenceId = prefix + "-" + objectid;
mediaEntry.MediaType = KalturaMediaType.VIDEO;
mediaEntry = client.MediaService.Add(mediaEntry);
mediaEntry = client.MediaService.AddContent(mediaEntry.Id, mediaResource);
string videoid = mediaEntry.Id;
string thumbnail = mediaEntry.ThumbnailUrl;
string status = mediaEntry.Status.ToString();
int duration = mediaEntry.Duration;
}
}
Thanks,
Steve
jess
November 5, 2015, 4:02pm
#4
Hi Steve,
I believe the issue is that you are checking duration too soon after uploading the entry.
Can you try running:
mediaEntry = client.MediaService.Get(entry_id);
After the entry is in status ready and see what the result is?