开发者

Generating thumbnails from multiple videos on Desktop, using ffmpeg, or something similar

开发者 https://www.devze.com 2023-01-27 07:22 出处:网络
Hi guys this is a long shot but here goes... I basically 开发者_高级运维have what I mentioned in the title running on my server.When I upload a video ffmpeg decomplies it and gives me screenshots, th

Hi guys this is a long shot but here goes...

I basically 开发者_高级运维have what I mentioned in the title running on my server. When I upload a video ffmpeg decomplies it and gives me screenshots, then I pick a screenshot that I want to use for that video. Currently, my server can process 3 videos at a time. The down side is that this uses up A LOT of the server processing power. :(

Is there a way, or a program, that can process several video at a time and generate me screenshots on my Desktop? If this is possible then I can just use my spare computer here to process everything then upload the screenshots/video to my server.

This is what I basically have running now on the server. kayweb.com.au/blogs/Web-Development/Generating-screenshots-using-FFmpeg

Something like this, But this thumbnail generator puts everything into one image. I need to be able to choose with thumbnail I want to use. http://www.tothepc.com/archives/make-movie-caps-screenshots-with-free-video-thumbnails-maker/

Anyone have any suggestions?


The problem with getting multiple thumbs is the time. So far I have not been able to get multiple thumbs from a single scan, so it means I have to scan three times for three images.

In my case I took the video time in seconds then got the the three images from 1/4, 1/2 and 3/4. But for long videos this is far too slow, even using ffmpeg's offset value. It is better if you can to select from the first 10-20 seconds into the video.

So I start with a pre-process, this scans the images at the time of the file search then any that haven't got a thumb created it creates one from a few seconds into the video, that is a quick process and in a thread of it's own isn't really noticeable, unless there are a lot of videos needing an icon, so a first run will be slow.

Then as with the functions form, if the thumb isn't any good I allow the selection from my 3 thumbs which are generated on the fly. In my case I used MDI so I could marry various parts of a media suite together. So the part below is a form on it's own, the file list 'videoList', has been filled when the user selected a directory in another form.

Just for completeness I will include my MediaItem and MediaType enums.

Ignore the way I have named my temporary files, I am still working on that because I will be marrying it with a service later which will supply the name. You can get problems using the same files in C# that were just created or modified in an external process, so you do need some clever file use convention.

    public partial class ThumbSelector : Form
{
    public List<MediaItem> videoList = new List<MediaItem>();
    private Random ra = new Random();
    int iCount = 1;

    public ThumbSelector()
    {
        InitializeComponent();
    }

    public void FillList()
    {
        if(videoList.Count() < 1)
            return;
        //only get mp4 files
        var videosOnly = from n in videoList
                      where n.mainType == MediaMainType.MMT_VIDEO
                      select n;
        lbVideoList.Items.Clear();
        foreach (MediaItem mi in videosOnly)
        {
            lbVideoList.Items.Add(mi.shortName);
        }
    }

    private void lbVideoList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(lbVideoList.SelectedItems.Count < 1)
            return;
        CreateThumbs(lbVideoList.SelectedItems[0].ToString());
    }

    private void CreateThumbs(string fShortName)
    {
        string sourceVideoName;
        int sourceLength = 0;
        int quarter = 0;
        int half = 0;
        int threequarter = 0;
        string destDir = @"C:\Users\robert\dwhelper\VideoIcons";

        string ffPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\ffmpeg.exe";

        var sname = (from n in videoList
                    where n.shortName == fShortName
                     select n).First();
        sourceVideoName = sname.fullName;
        sourceLength = GetVideoLength(ffPath, sourceVideoName);

        quarter = sourceLength / 4;
        half = sourceLength / 2;
        threequarter = quarter * 3;
        Image im1;
        Image im2;
        Image im3;
        string n1;
        string n2;
        string n3;


        while (File.Exists(string.Concat(destDir, @"\", "x1_", iCount.ToString(), ".jpg")))
        {
            ++iCount;
        }
        n1 = string.Concat("x1_", iCount.ToString(), ".jpg");
        ++iCount;
        while (File.Exists(string.Concat(destDir, @"\", "x2_", iCount.ToString(), ".jpg")))
        {
            ++iCount;
        }
        n2 = string.Concat("x2_", iCount.ToString(), ".jpg");
        ++iCount;
        while (File.Exists(string.Concat(destDir, @"\", "x3_", iCount.ToString(), ".jpg")))
        {
            ++iCount;
        }
        n3 = string.Concat("x3_", iCount.ToString(), ".jpg");

        pic1.Image = null;
        pic2.Image = null;
        pic3.Image = null;


        CreateThumbs(sname, quarter, n1);
        im1 = Image.FromFile(string.Concat(destDir, @"\", n1));
        pic1.Image = im1;
        CreateThumbs(sname, half, n2);
        im2 = Image.FromFile(string.Concat(destDir, @"\", n2));
        pic2.Image = im2;
        CreateThumbs(sname, threequarter, n3);
        im3 = Image.FromFile(string.Concat(destDir, @"\", n3));
        pic3.Image = im3;

    }

    private void CreateThumbs(MediaItem mi, int timeIn, string outName)
    {
        this.Cursor = Cursors.WaitCursor;
        Process pth;
        string destDir = @"C:\Users\robert\dwhelper\VideoIcons";
        string sourceFile;
        string destFile;
        string ffPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\ffmpeg.exe";
        string strCommand;
        string fullCommand;
        string[] ssplit;

        sourceFile = mi.fullName;
        ssplit = mi.shortName.Split('.');
        destFile = string.Concat(destDir, "\\", outName);

        strCommand = string.Concat(
            " -i \"", sourceFile, "\" -r 1 -ss ", timeIn.ToString(), " -t 00:00:01 -f image2 \"", destFile, "\"");

        fullCommand = string.Concat(ffPath, strCommand);

        pth = new Process();
        pth.StartInfo.FileName = ffPath;
        pth.StartInfo.Arguments = strCommand;
        pth.StartInfo.UseShellExecute = false;
        pth.StartInfo.CreateNoWindow = true;

        pth.Start();
        pth.WaitForExit();

        this.Cursor = Cursors.Default;

    }

    private int GetVideoLength(string ffmpeg, string sourceFile)
    {
        //exec('start C:\Inetpub\ffmpeg\ffmpeg -i "D:/Jaime-flex.wmv" 2>&1 | grep "Duration" | cut -d " " -f 4 - | sed s/,//', $output);
        string result = "";
        string strCommand = string.Concat(
                " -i \"", sourceFile, "\" 2>");
        Process pth = new Process();
        pth.StartInfo.FileName = ffmpeg;
        pth.StartInfo.Arguments = strCommand;
        pth.StartInfo.UseShellExecute = false;
        pth.StartInfo.CreateNoWindow = true;
        pth.StartInfo.RedirectStandardOutput = true;
        pth.StartInfo.RedirectStandardError = true;
        pth.Start();


        pth.WaitForExit();
        //result = pth.StandardOutput.ReadToEnd();
        //result = pth.StandardError.ReadToEnd();
        string str = "";
        string[] ssplit;
        char[] splitChars = new char[] { '.', ' ' };
        int seconds = 0;
        while (!pth.StandardError.EndOfStream)
        {
            str = pth.StandardError.ReadLine();
            if (str.Contains("Duration"))
            {
                ssplit = str.Split(splitChars);
                seconds = GetSeconds(ssplit[3]);
            }
        }
        return seconds;
    }

    private int GetSeconds(string p)
    {
        string[] ssplit = p.Split(':');
        int ho = Convert.ToInt32(ssplit[0]) * 120;
        int mi = Convert.ToInt32(ssplit[1]) * 60;
        int se = Convert.ToInt32(ssplit[2]);

        return ho + mi + se;
    }

}
public class MediaItem
{
    public string shortName { get; set; }
    public string fullName { get; set; }
    public string iconName { get; set; }
    public MediaMainType mainType { get; set; }
    public MediaSubType subType { get; set; }
    public long length { get; set; }
}
public enum MediaMainType
{
    MMT_VIDEO = 0, MMT_IMAGE, MMT_ICON, MMT_NOTMEDIA
}
public enum MediaSubType
{
    MST_JPG = 0, MST_GIF, MST_BMP, MST_PNG, MST_MP4, MST_WMV, MST_FLV, MST_NOTMEDIA
3
0

精彩评论

暂无评论...
验证码 换一张
取 消