I need to write an ASP handler that will return a trimmed mp4 video file. I will specify filename, starting time and ending time.
Right now I only have handler that returns whole video file. The question is how to cut it in pieces.
public void ProcessRequest(HttpContext context)
{
fileName = context.Request.QueryString["filename"];
start = float.Parse(context.Request.QueryString["start"]);
end = float.Parse(context.Request.QueryString["end"]);
context.Response.ContentType = "video/mp4";
context.Response.WriteFile("~/videofile.mp4");
}
I have read some topics on MP4 files, headers etc, but it's not that easy. If I found a way to calculate bytes for individual frames. But how to do it with variable bitrate?
If you can run a system command to run a command line utility like ffmpeg to cut the file for you. That is the simplest way.
ffmpeg -i input-file -ss <start time> -t <duration> -acodec copy -vcodec copy out.mp4
It is otherwise too much trouble to trim the mp4 programatically. Let ffmpeg do it for you.
I managed to copy some part of the file using simple calculations based on metadata acquired with MediaInfo.
I copy whole header and then part of video and audio streams. But this only works if I start at the beginning of the video. When I want it to start after 2secs, the file is totally corrupted.
Also, header metadata says it's still original duration, even if it's only 4 secs.
Seeking is also quite inaccurate. Video is approx 10% longer that it should be.
Maybe there is some way to find seekpoints in stream?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.