ffmpegvideo-streamingh.264video-capturempeg

How to extract encoded transport stream frames from a video file?


Searching for a method to extract from video stream (.m2t or .ts file) a certain frames as is, encoded. OpenCV also extracts frames easily but decodes them immediately. Given:

I need to read from the file all frames in the given interval and provide to another program as buffer frame by frame as they are. The catch here is to keep the frames encoded as they are, do not decode/encode/encapsulate them.

Picking a frame from the H.264 m2t to a pipe:

ffmpeg -ss 0:2:1.12 -i .\my_video.ts -c:v copy -f mpegts -frames:v 1 pipe: -y -hide_banner

Obviously, the time stamp is increasing for every next frame. From the pipe it is not a problem to convert it to a buffer.

Questions:

  1. Is this method correct to extract a separate frame as it is without any reference/recalculations with neighbor frames?
  2. Not sure that flag -f mpegts really keeps the frame untouched. Is there better flag? (Maybe -f null?)
  3. How to know the type of extracted frame (i, P, or B)?

Thank you.


Solution

  • This answer slightly diverts the original question. However, it does the job and gives good enough result.

    The original question requested to extract encoded video frame by frame. Suggested variants extract to the standar output a compressed videos in batches of several frames. This is easy to pick in software and process/concatenate later as needed.

    Variant 1

    This method gives really smooth video by concatenation of resulting chunks. This method consumes more CPU than the Variant 2. And processing of every next chunk in the movie takes longer and longer. Therefore, Variant 2 is way better in terms of performance.

    ffmpeg.exe -y -nostdin -loglevel quiet -hide_banner -i "c:\\temp\\in.ts" -vf trim=<from_second>:<to_second> -f mpegts pipe:

    The order of keys is important and means:

    Variant 2

    This method gives almost smooth video by concatenation of resulting chunks. Almost good is visible jumping video, so it is not perfect method in terms of quality.

    ffmpeg.exe -y -nostdin -loglevel quiet -hide_banner -ss <from_second> -t <chunk_duration> -i "c:\\temp\\in.ts" -copyts -f mpegts pipe:

    Not repeating the explained options in the variant 1, only new options: