Click here to Skip to main content
14,879,654 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to write to Form1.texBox1 from another class. Pin
OriginalGriff19-Mar-21 12:24
mveOriginalGriff19-Mar-21 12:24 
QuestionCombobox Text Pin
Ismael Oliveira 202119-Mar-21 8:01
MemberIsmael Oliveira 202119-Mar-21 8:01 
AnswerRe: Combobox Text Pin
OriginalGriff19-Mar-21 9:44
mveOriginalGriff19-Mar-21 9:44 
GeneralRe: Combobox Text Pin
Ismael Oliveira 202119-Mar-21 10:14
MemberIsmael Oliveira 202119-Mar-21 10:14 
AnswerRe: Combobox Text Pin
BillWoodruff20-Mar-21 6:15
mveBillWoodruff20-Mar-21 6:15 
QuestionUnhandle System.AccessViolationException from FFMediaToolkit Pin
huycaothu18-Mar-21 23:05
Memberhuycaothu18-Mar-21 23:05 
AnswerRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
OriginalGriff18-Mar-21 23:19
mveOriginalGriff18-Mar-21 23:19 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
huycaothu19-Mar-21 2:43
Memberhuycaothu19-Mar-21 2:43 
here is my code that using ffmpeg lib, it's extract frame in video file and do something else. The exception may raise at any time and my question is not find out why exception occur. I only want to know why I already add try-catch block to my code but when the exception raised, it make my app crash instead of jump to the cach block?
[HandleProcessCorruptedStateExceptions]
[SecurityCritical]
private void timerCallback(object state)
{
MediaFile file = null;
long startTick = DateTime.Now.Ticks;
try
{
//int duration = 1000 / Properties.Settings.Default.FrameProcess;
if (PlayListInfo == null || PlayListInfo.Count == 0) return;
int idx = 0;
bool newVideo = true;
TimeSpan endTime = new TimeSpan();
double time_4_frame_ms = 0;
while (idx < PlayListInfo.Count && !request_stop)
{
var startProcess = DateTime.Now;
var curVideo = PlayListInfo[idx];
bool firstFrame = false;
if (newVideo)
{
if (file != null) file.Dispose();
startTick = DateTime.Now.Ticks;
file = MediaFile.Open(curVideo.fileName);
newVideo = false;
endTime = curVideo.stopTime < 0 ? file.Info.Duration : TimeSpan.FromSeconds(curVideo.stopTime);
if (!file.HasVideo)
{
Console.WriteLine($"Cannot load video file: {curVideo.fileName}");
}
else
{
Console.WriteLine($"Process video: {curVideo.fileName}, frame rate: {file.Video.Info.AvgFrameRate}");
time_4_frame_ms = file.Video.Info.NumberOfFrames == null ? 0 : file.Video.Info.Duration.TotalMilliseconds / file.Video.Info.NumberOfFrames.Value;
firstFrame = true;
}
}
TimeSpan sp = new TimeSpan(DateTime.Now.Ticks - startTick);
sp.Add(TimeSpan.FromSeconds(curVideo.startTime <= file.Info.StartTime.TotalSeconds ? file.Info.StartTime.TotalSeconds : curVideo.startTime));
ImageData img;
if (sp > endTime)
{
idx++;
newVideo = true;
continue;
}
if (file != null && file.HasVideo)
{
var t1 = DateTime.Now;
bool ck = false;
if (firstFrame)
{
ck = file.Video.TryGetFrame(sp, out img);
firstFrame = false;
}
else
{
do
{
ck = file.Video.TryGetNextFrame(out img);
}
while (ck && file.Video.Position < sp);
}
if (!ck)
{
Console.WriteLine($"Cannot read frame at {sp} of {curVideo.fileName}");
idx++;
newVideo = true;
continue;
}
else
using (var bitmap = ToBitmap(img))
{
var frametime = DateTime.Now - t1;
//Console.WriteLine($"Take {frametime.TotalMilliseconds} to extract frame of {curVideo.fileName}");
// dataShare.WriteFrameData(img.ImageSize.Width, img.ImageSize.Height,
//DataUtils.PixelFormatToBitCount(bitmap.PixelFormat), img.Data.ToArray());
if (checkBlankFrame(bitmap))
{
Console.WriteLine($"Found black frame at {sp} of {curVideo.fileName}");
}
else ProcessFrame(bitmap);
}
}
var processTime = DateTime.Now - startProcess;
//Console.WriteLine($"Process time of {curVideo.fileName}: {processTime.TotalMilliseconds}/ms");
int sleeptime = (int)(time_4_frame_ms - processTime.TotalMilliseconds);
if (sleeptime > 0)
System.Threading.Thread.Sleep(sleeptime);
}
}
catch (AccessViolationException ex)
{
Console.WriteLine(ex.ToString());
}
catch
{

        }

        finally
        {
            if (file != null)
            {
                try
                {
                    file.Dispose();
                }
                catch
                {

                }
                file = null;
            }
        }

    }
here is the source code for making video from bitmap:
private unsafe void PushFrame(Bitmap bitmap)
{
try
{
lock (this)
{
var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
try
{
int len = data.Stride * data.Height;
byte[] bitmapData = new byte[len];
Marshal.Copy(data.Scan0, bitmapData, 0, len);
if (frameList.Count == 0) startTime = DateTime.Now.Ticks;
frameList.Add(bitmapData);
}
finally
{
bitmap.UnlockBits(data);
}
//ImageData data = VideoProcessEx.ToImageData(bitmap);
//mediaFile.Video.AddFrame(data);
TimeSpan sp = new TimeSpan(DateTime.Now.Ticks - startTime);
if (sp.TotalSeconds >= Properties.Settings.Default.LiveVideoDuration)
{
if (frameList.Count == 0) return;
videoCount++;
videoIndex = videoCount % 10;

                    var settings = new VideoEncoderSettings(width: bitmap.Width, height: bitmap.Height,
                        framerate: (int)(frameList.Count / sp.TotalSeconds), codec: VideoCodec.H264);
                    settings.EncoderPreset = EncoderPreset.Fast;
                    settings.CRF = 17;
                    //settings.VideoFormat = ImagePixelFormat.Rgb24;
                    string path = $"{VideoFolder}\\{videoIndex}.{videoExt}";
                    using (var mediaFile = MediaBuilder.CreateContainer(path)/*.UseFormatOption("g", "10").UseFormatOption("movflags", "frag_keyframe+empty_moov")*/.WithVideo(settings).Create())
                    {
                        Console.WriteLine($"create video: {MappingFile}.{videoIndex}.{videoExt} -- duration: {sp.TotalSeconds}, total frame: {frameList.Count}");
                        foreach (var item in frameList)
                        {
                            Span<byte> imgData = new Span<byte>(item, 0, item.Length);
                            //return new ImageData(imgData, ImagePixelFormat.Rgb24, bitmap.Size);
                            mediaFile.Video.AddFrame(new ImageData(imgData, ImagePixelFormat.Bgr24, bitmap.Size));
                            
                            //mediaFile.Video.Configuration.Framerate = (int)(mediaFile.Video.FramesCount / sp.TotalSeconds);
                        }
                        AddFileToPlayList(videoCount, $"{videoIndex}.{videoExt}", (decimal)sp.TotalSeconds);
                        //Task.Delay((int)(2) * 1000)
                        //    .ContinueWith((t) =>
                        //    {

                        //        AddFileToPlayList(videoCount, $"{videoIndex}.{videoExt}", (decimal)sp.TotalSeconds);
                        //    });
                    }
                    frameList.Clear();
                }
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
OriginalGriff19-Mar-21 2:56
mveOriginalGriff19-Mar-21 2:56 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
huycaothu20-Mar-21 0:03
Memberhuycaothu20-Mar-21 0:03 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
Dave Kreskowiak19-Mar-21 4:28
mveDave Kreskowiak19-Mar-21 4:28 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
Richard MacCutchan19-Mar-21 4:38
mveRichard MacCutchan19-Mar-21 4:38 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
huycaothu19-Mar-21 23:53
Memberhuycaothu19-Mar-21 23:53 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
Richard MacCutchan20-Mar-21 0:10
mveRichard MacCutchan20-Mar-21 0:10 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
huycaothu20-Mar-21 0:52
Memberhuycaothu20-Mar-21 0:52 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
huycaothu20-Mar-21 1:00
Memberhuycaothu20-Mar-21 1:00 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
Richard MacCutchan20-Mar-21 3:04
mveRichard MacCutchan20-Mar-21 3:04 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
huycaothu21-Mar-21 15:51
Memberhuycaothu21-Mar-21 15:51 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
Richard MacCutchan21-Mar-21 22:44
mveRichard MacCutchan21-Mar-21 22:44 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
huycaothu19-Mar-21 23:56
Memberhuycaothu19-Mar-21 23:56 
GeneralRe: Unhandle System.AccessViolationException from FFMediaToolkit Pin
Dave Kreskowiak20-Mar-21 5:02
mveDave Kreskowiak20-Mar-21 5:02 
QuestionCS1929 Pin
Josh Vanbaalen17-Mar-21 10:00
MemberJosh Vanbaalen17-Mar-21 10:00 
AnswerRe: CS1929 Pin
OriginalGriff17-Mar-21 11:39
mveOriginalGriff17-Mar-21 11:39 
GeneralRe: CS1929 Pin
Josh Vanbaalen17-Mar-21 13:40
MemberJosh Vanbaalen17-Mar-21 13:40 
GeneralRe: CS1929 Pin
OriginalGriff17-Mar-21 21:02
mveOriginalGriff17-Mar-21 21:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.