VideoHelp Forum

+ Reply to Thread
Results 1 to 3 of 3
Thread
  1. Member
    Join Date
    Aug 2018
    Location
    Phoenix
    Search Comp PM
    Hello all,

    Is there a way (preferably with ffmpeg as that is what I'm familiar with) to specify a closed GOP around a clip.

    Many of my recordings require cuts and it is very difficult to accurately cut clips with the common open GOP structure. I have tried for a long time to cut only on IDR frames but even this is unreliable because of open GOPs.

    Then I had the idea, what if I could re-encode and force a closed GOP around my desired clips? Is that possible? I know ffmpeg has a forced keyframes flag, but that doesn't garauntee the clip between keyframes will even be a single GOP, let alone a closed one. I also don't think I need to convert the entire source to closed GOP, just between the frames I want to cut.

    Any ideas? I should clarify, sources are h264.
    Quote Quote  
  2. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    Unless I am mistaken and all the videos I cut and paste are not what I think they are, FFMpeg creates Closed GOP by default. Most of the hundreds I have downloaded are also closed GOP. Cutting on key frame is a specific method to assure :
    1. The final frame is a P frame
    2. The final 5? frames Pts times are the same interval
    3. No following frames have to be decoded before the P frame
    4. FFMpeg script has to be specific, I.E. -ss Pecedes or follows -i filename depending on whjat you want to cut

    if you copy the following to a BAT file and then drag and drop a video on the it, you will have a lot of information you need.

    Code:
    setlocal enabledelayedexpansion
    @echo off
    ::for %%a in (*.mp4,*.mpg,*.flv) Do (
    for %%a in ("%~dpnx1") Do (
    set /a count=0
    cd %%~dpa
    echo frame,media_type,stream_index,key_frame,pkt_pts,pkt_pts_time,pkt_dts,pkt_dts_time,best_effort_timestamp,^
    best_effort_timestamp_time,pkt_duration,pkt_duration_time,pkt_pos,pkt_size,^
    Width,Height,pix_fmt,sample_aspect_ratio,pict_type,coded_picture_number,display_picture_number,^
    interlaced_frame,top_field_first,repeat_pict,color_range,color_space,color_primaries,color_transfer,^
    chroma_location > "%%~na_ffprobe.csv"
    echo No.  pts_time  type  Frame# > "%%~na_AllFrames.txt"
    ver > nul
    set /a Number=0
    ffprobe.exe -v quiet -select_streams v:0 -print_format csv -show_entries frame "%%~nxa"  >> "%%~na_ffprobe.csv"
    for /F "tokens=4,6,18,19,20 delims=," %%b in ('findstr "video" "%%~na_ffprobe.csv"') do (
    set "x=%%b"
    set "y=%%c"
    set "z=%%d"
    set "w=%%e"
    set "v=%%f"
    set "u=%%g"
    set sort=!y:~0,-7!
    ::if !sort! GEQ 0 if !sort! LEQ 10 echo !count!  !y:~0,13!  !x! !w:~0,1! >> "%%~na_AllFrames.txt"
    echo !count!    !y:~0,13!  !x! !w:~0,1!     !v! >> "%%~na_AllFrames.txt"
    set /a count+=1
     set /a ekko=count%%100
     if !ekko! EQU 0 echo Frame !count! processed
    )
    )
    rem pause

    This will produce a CSV file with all information and a shorter text file such as what follows here. notice that not only does the frame before the next 'I' frame is a 'P' frame but no following frames have a lowere decode number, meaning it would have to be decoded before the 'P' frame, signifying Closed GOP. Cutting here works fine for all my cuts.

    No. pts_time type Frame#
    0 0.080000 1 I 0
    1 0.120000 0 B 3
    2 0.160000 0 B 2
    3 0.200000 0 B 4
    4 0.240000 0 P 1
    5 0.280000 0 B 7
    6 0.320000 0 B 6
    7 0.360000 0 P 5

    70 2.880000 0 P 66
    71 2.920000 0 B 73
    72 2.960000 0 B 72
    73 3.000000 0 B 74
    74 3.040000 0 P 71
    75 3.080000 1 I 75
    76 3.120000 0 B 78
    77 3.160000 0 B 79
    78 3.200000 0 B 77
    79 3.240000 0 B 80
    80 3.280000 0 P 76
    81 3.320000 0 B 83
    82 3.360000 0 B 84
    83 3.400000 0 B 82
    84 3.440000 0 B 85
    85 3.480000 0 P 81
    Last edited by Budman1; 9th Jul 2021 at 22:34.
    Quote Quote  
  3. Member
    Join Date
    Aug 2018
    Location
    Phoenix
    Search Comp PM
    This has got to be the most helpful response I've ever gotten, on any forum. Thank you budman, for such a detailed response!
    Quote Quote  



Similar Threads