我正在为一个网站编写视频转换工具。
接下来是逻辑
- 用户上传文件
- 系统为用户转换(当然用户在转换时不应该等待)
我有一个代码:
public async void AddVideo(HotelVideosViewModel model)
{
var sourceFile = FileHandler.SaveFile(model.VideoFile);
var fullFilePath = HttpContext.Current.Server.MapPath(sourceFile);
await Task.Factory.StartNew(() => video.Convert(fullFilePath));
model.FileLocation = sourceFile;
model.IsConverted = true;
this.Add<HotelVideos>(Mapper.Map<HotelVideosViewModel, HotelVideos>(model));
}
这开始编码:
await Task.Factory.StartNew(() => video.Convert(fullFilePath));
但是这之后的代码永远不会执行......有人有解决方案吗?
PS:在 video.Conver(...) 下有类似:
public bool Convert(string fileLocation)
{
// do staff
}
- - 更新 - -
刚刚得到了有趣的东西。如果我要交换video.Convert(fullFilePath)
到 Thread.Sleep(2000) 一切都开始工作。所以问题出在我认为的第二种方法中。
所以我正在添加下一个文件代码(现在草稿):
using Softpae.Media;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class VideoConvertManager
{
private readonly Job2Convert jobConverter = new Job2Convert();
private readonly MediaServer mediaServer = new MediaServer();
public bool Convert(string fileLocation)
{
this.jobConverter.pszSrcFile = fileLocation;
this.jobConverter.pszDstFile = fileLocation + ".mp4";
this.jobConverter.pszDstFormat = "mp4";
this.jobConverter.pszAudioCodec = null;
this.jobConverter.pszVideoCodec = "h264";
if (this.mediaServer.ConvertFile(this.jobConverter))
{
FileHandler.DeleteFile(fileLocation);
return true;
};
FileHandler.DeleteFile(fileLocation);
return false;
}
}