Example #1
0
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            var codec = "";
            var obj   = context.Instance;

            if (obj is ConvertMedia)
            {
                codec = (obj as ConvertMedia).video_code;
            }
            return(new StandardValuesCollection(ConvertMedia.getPreset(codec)));
        }
Example #2
0
        public override Dictionary <string, object> GetConvertMap()
        {
            var dict = new Dictionary <string, object>()
            {
            };

            ConvertMedia.getCRF().ForEach((s, i) =>
            {
                dict.Add(s, i);
            });
            return(dict);
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="files"></param>
        /// <param name="streams">must assign fileIndex field, reference to getAllStreams()</param>
        /// <param name="toFile"></param>
        /// <returns></returns>
        public string muxerCommand(IList <FFmpeg> files, IEnumerable <MediaStream> streams, string toFile, ConvertMedia convertAll = null)
        {
            if (toFile == "")
            {
                throw new ExceptionFFmpeg(dfv.lang.dat.Please_add_file_name);
            }

            var ext = dfv.getFileExt(toFile);

            if (ext == "")
            {
                throw new ExceptionFFmpeg(dfv.lang.dat.Please_add_extension);
            }

            var           videoCount = 0;
            var           count      = 0;
            List <string> filters    = new List <string>();


            var subtitleBurnIn = "";

            streams.ForEach(st =>
            {
                var convert = convertAll;
                if (convert == null)
                {
                    convert = st.convert;
                }
                if (st.selected)
                {
                    count++;
                    if (st.isPureVideo())
                    {
                        videoCount++;
                    }

                    var fi = files[st.fileIndex].fileName;
                    if (st.isSubtitle() && ext != "mkv" && !ConvertMedia.extAudios.Contains(ext) && convert.video_code != "")
                    {
                        if (subtitleBurnIn == "")
                        {
                            if (st.isImgSubtitle())
                            {
                                subtitleBurnIn = "[" + st.fileIndex + ":s]overlay";
                            }
                            else
                            {
                                subtitleBurnIn = getSubtitle(fi, st);
                            }
                        }
                    }
                }
            });

            if (count < 1)
            {
                throw new ExceptionFFmpeg(dfv.lang.dat.At_least_one_stream);
            }

            if (subtitleBurnIn != "")
            {
                filters.Add(subtitleBurnIn);
            }

            var cmd = "";

            var inputCmd = "";

            for (int index = 0; index < files.Count; index++)
            {
                var f = files[index];
                if (f.fileName == "")
                {
                    throw new ExceptionFFmpeg(dfv.lang.dat.Could_not_found_FFmpeg_fileName);
                }

                var paras = getFieldAttr(f.parameters, 0);

                inputCmd += " " + paras + " -i \"" + f.fileName + "\" \n";
            }


            var streamI = 0;

            var shortest = "";

            foreach (var st in streams)
            {
                if (!st.selected)
                {
                    continue;
                }

                var convert = convertAll;
                if (convert == null)
                {
                    convert = st.convert;
                }


                var filterMap = "";
                var filterOut = "[out" + streamI + "]";

                if (ConvertMedia.extAudios.Contains(ext) && !st.isAudio())
                {
                    continue;
                }

                if (st.isPureVideo())
                {
                    filterMap = getFilter("[" + st.fileIndex + ":" + st.index + "]", files, filters, convert.video_filters);
                    if (filterMap != "")
                    {
                        filterMap += filterOut;
                    }
                }

                if (ext != "mkv")
                {
                    if (st.codec_type == "subtitle" && !(st.hasMovText(ext) && convert.video_code == "" && !st.isImgSubtitle()))
                    {
                        continue;
                    }
                    if (st.codec_type == "attachment")
                    {
                        continue;
                    }
                }

                if (st.isPic())
                {
                    if (videoCount == 0)
                    {
                        var fil = getFilter(mainStr, files, filters, convert.video_filters);

                        if (fil != "")
                        {
                            filterMap  = "color=c=black,scale=" + getScale(st.width, st.height) + ",setsar=1" + mainStr + ";";
                            filterMap += fil + filterOut;
                        }

                        shortest = " -shortest ";
                        if (convert.video_code == "")
                        {
                            convert.video_codec = 1;
                        }
                        videoCount++;
                    }
                    else
                    {
                        continue;
                    }
                }

                if (st.isAudio())
                {
                    filterMap = getAudioFilter("[" + st.fileIndex + ":a]", files, convert.audio_filters);
                    if (filterMap != "")
                    {
                        filterMap += filterOut;
                    }
                }

                var metaData = "";

                if (st.disposition.defaul != st.disposition2.defaul)
                {
                    metaData += " -disposition:" + streamI;
                    metaData += st.disposition2.defaul == 0 ? " 0 " : " default ";
                }

                if (st.isAttachmentFile)
                {
                    cmd += "\n -attach \"" + st.codec_long_name + "\" ";

                    cmd += getMetadata(st, streamI);
                }
                else
                {
                    if (filterMap != "")
                    {
                        cmd += "\n -filter_complex \"" + filterMap + "\" ";
                        cmd += "\n -map " + filterOut;
                    }
                    else
                    {
                        cmd += "\n -map " + st.fileIndex + ":" + st.index;
                    }



                    metaData += getMetadata(st, streamI);

                    string extraParas = "";

                    if (convertAll != null && convertAll.video_2pass == "1")
                    {
                        extraParas = "pass=1";
                    }

                    cmd += getCodec(st, streamI, convertAll, ext, extraParas) + " " + metaData;
                }
                streamI++;
            }



            if (convertAll != null && convertAll.command_line != "")
            {
                cmd += "\n" + convertAll.command_line + " ";
            }

            cmd += "\n " + shortest;

            if (convertAll != null && convertAll.video_2pass == "1")
            {
                var oldCmd     = cmd;
                var passFormat = convertAll.video_code.IndexOf("vp9") >= 0 ? "webm" : "mp4";

                if (convertAll.video_code == "hevc")
                {
                    oldCmd = oldCmd.Replace("pass=1", "pass=2");
                }

                cmd += " -pass 1 -an  -f " + passFormat + " -y NUL \n\n&&\n\n" + inputCmd + oldCmd + " \n -pass 2";
            }

            //var pass1 = cmd + " -pass 1 -f " + ext + " -y NUL";
            //var psss2 = cmd + " -pass 2  \"" + toFile + "\" ";
            //await exec(inputCmd + pass1);
            //await exec(inputCmd + psss2);



            cmd += "\n\n -strict -2 -y \"" + toFile + "\" ";

            return(inputCmd + cmd);
        }
Example #4
0
        string getCodec(MediaStream st, int streamI, ConvertMedia convertAll, string ext, string extra265Paras = "")
        {
            var cmd = " -codec:" + streamI + " ";

            var convert = st.convert;

            if (convertAll != null)
            {
                convert = convertAll;
            }

            if (convert.audio_code != "" && st.isAudio())
            {
                cmd += convert.audio_code;



                if (convert.audio_bit_rate != "")
                {
                    cmd += " -ab:" + streamI + " " + convert.audio_bit_rate + "k ";
                }
                else if (convert.audio_quality != "")
                {
                    cmd += " -aq:" + streamI + " " + convert.audio_quality + " ";
                }
                else
                {
                    cmd += " -aq:" + streamI + " 2 ";
                }



                if (convert.audio_channels != "")
                {
                    cmd += " -ac:" + streamI + " " + convert.audio_channels + " ";
                }

                if (convert.audio_sample_rate != "")
                {
                    cmd += " -ar:" + streamI + " " + convert.audio_sample_rate + " ";
                }
            }
            else if (convert.video_code != "" && st.isVideo())
            {
                cmd += convert.video_code;
            }
            else
            {
                if (st.isSubtitle() && !st.isImgSubtitle() && st.hasMovText(ext))
                {
                    cmd += " mov_text ";
                }
                else if (ext == "mkv" && st.isMovText())
                {
                    cmd += " srt ";
                }
                else
                {
                    cmd += " copy ";
                }
            }

            if (st.isVideo())
            {
                if (convert.video_crfs > 0)
                {
                    var crf = convert.video_crfs - 1;
                    if (convert.video_code.IndexOf("nvenc") > 0)
                    {
                        cmd += " -qp:" + streamI + " " + crf + " ";
                    }
                    else if (convert.video_code.IndexOf("qsv") > 0)
                    {
                        cmd += "  -global_quality:" + streamI + " " + crf + " ";
                    }
                    else if (convert.video_code.IndexOf("vp9") >= 0)
                    {
                        cmd += "  -crf:" + streamI + " " + crf + " -b:v:" + streamI + " 0 ";
                    }
                    else
                    {
                        cmd += "  -crf:" + streamI + " " + crf + " ";
                    }
                }
                else if (convert.video_bit_rate != "")
                {
                    var rate = int.Parse(convert.video_bit_rate);

                    cmd += "  -b:v:" + streamI + " " + rate + "k ";
                }

                if (convert.video_max_bit_rate != "")
                {
                    cmd += " -maxrate:v:" + streamI + " " + convert.video_max_bit_rate + "k ";

                    var buf_size = convert.video_buffer_size;
                    if (buf_size == "")
                    {
                        buf_size = convert.video_max_bit_rate + "";
                    }


                    cmd += " -bufsize " + buf_size + "k ";
                }

                if (convert.preset != "")
                {
                    if (convert.video_code == "vp9")
                    {
                        cmd += "  -deadline:" + streamI + " " + convert.preset + " ";
                    }
                    else
                    {
                        cmd += "  -preset:" + streamI + " " + convert.preset + " ";
                    }
                }


                if (convert.video_display_size != "")
                {
                    var size = convert.video_display_size;
                    if (convert.video_display_size.Contains("-1"))
                    {
                        var whs = convert.video_display_size.Replace(" ", "").Split(':');
                        if (whs.Length == 2)
                        {
                            int w = int.Parse(whs[0]);
                            int h = int.Parse(whs[1]);
                            if (whs[0] == "-1")
                            {
                                if (st.height != 0)
                                {
                                    w = h * st.width / st.height;
                                }
                            }
                            else if (whs[1] == "-1")
                            {
                                if (st.width != 0)
                                {
                                    h = w * st.height / st.width;
                                }
                            }
                            size = getScale(w, h);
                        }
                    }
                    cmd += " -s:" + streamI + " " + size + " ";
                }

                cmd += getFieldAttr(convert, streamI);


                if (convert.video_code == "h264")
                {
                    var paras = convert.x26x_params.GetType().GetProperties().JoinStr(":", it => FileConvertParameter.getFieldStr(it, convert.x26x_params));
                    if (paras != "")
                    {
                        cmd += " -x264-params:" + streamI + " \"" + paras + "\" ";
                    }
                }
                else if (convert.video_code == "hevc")
                {
                    var vals = convert.x26x_params.GetType().GetProperties().Select(it => FileConvertParameter.getFieldStr(it, convert.x26x_params, true)).
                               Concat(convert.x265_params.GetType().GetProperties().Select(it => FileConvertParameter.getFieldStr(it, convert.x265_params, true)));

                    if (extra265Paras != "")
                    {
                        vals = new string[] { extra265Paras }.Concat(vals);
                    }

                    var paras = vals.JoinStr(":", it => it);
                    if (paras != "")
                    {
                        cmd += " -x265-params:" + streamI + " \"" + paras + "\" ";
                    }
                }
            }

            if (convert.command_line != "" && convertAll == null)
            {
                cmd += " " + convert.command_line + " ";
            }

            return(cmd);
        }
Example #5
0
 public string muxerCommand(FFmpeg files, IEnumerable <MediaStream> streams, string toFile, ConvertMedia convertAll = null)
 {
     return(muxerCommand(new FFmpeg[] { files }.ToList(), streams, toFile, convertAll));
 }