Newest 'ffmpeg' Questions - Stack Overflow
Les articles publiés sur le site
-
Can I use the "stream copy" of ffmpeg in OpenCV with VideoWriter class ?
14 mai 2019, par DerekGPython: 3.6, Ubuntu 18.04, OpenCV 4.1.0
I have an IP camera that streams video data in H.264 encoding. I would like to take this video stream and save it in a .avi file using Python without any encoding or decoding. Using a command line interface and ffmpeg commands, this is trivial to do.
mycomputer@home:~$ ffmpeg -i rtsp://username:password@192.168.1.1/?framerate=30.0?streamprofile=defaultcameraprofile -acodec copy -vcodec copy output_file_name.avi
I'd like to do a similar thing from within Python using OpenCV. Currently, I am using VideoCapture class to read in each frame, and then using the OpenCV VideoWriter class to write this frame to a file. My issue is that the VideoWriter class requires a four_cc code to specify which codec is to be used during writing, whereas I don't want to use any codec at all (or more precisely, I want to use the stream copy of FFmpeg, but there is to my knowledge no corresponding four_cc code). OpenCV documentation specifies that if I use the FFmpeg backend API and specify both a codec and framerate of 0, the raw stream should be written to the new file. However, when I do this, no file is created.
cap = cv2.VideoCapture("rtsp://username:password@192.168.1.1/?framerate=30.0?streamprofile=defaultcameraprofile", cv2.CAP_FFMPEG) frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) # output file, backend API, four_cc code integer, framerate, frame size tuple out = cv2.videoWriter("output_file.avi",cv2.CAP_FFMPEG,0,0,(frame_width,frame_height))
I have tried many combinations of other input parameters to the videoWriter constructor but all of them either re-encode the stream or do not write an output file. For instance, I have tried nearly every combination of framerate {0,30}, output file extension {.avi, .mp4, .h264, .mkv, None} and codec {0, "H.264", "RAW ", "DBI ", " ", "MPEG", "COPY", "NONE"}.
out = cv2.videoWriter("output_file.avi",cv2.CAP_FFMPEG,cv2.VideoWriter_fourcc('H','2','6','4'),30,(frame_width,frame_height))
Is it possible to use the FFmpeg stream copy within OpenCV to save video data without encoding it? If not, I will change tact to running shell commands from within Python, but I'd prefer the former approach if feasible.
-
Convert video with ffmpeg to have the same quality
14 mai 2019, par zlonI want to make some present for my grandma. And prepare some video. Unfortunately here video player is old and could not read some file formats. She is not at home now, so I don't know which player she has. But I have a file which is OK for here system:
File which is in a correct format:
General Complete name : let.avi Format : AVI Format/Info : Audio Video Interleave File size : 688 MiB Duration : 1 h 35 min Overall bit rate : 1 002 kb/s Writing application : VirtualDubMod 1.4.13 Writing library : VirtualDub build 14328/release FileExtension_Invalid : avi Video ID : 0 Format : MPEG-4 Visual Format profile : Simple@L3 Format settings, BVOP : No Format settings, QPel : No Format settings, GMC : No warppoints Format settings, Matrix : Default (H.263) Codec ID : XVID Codec ID/Hint : XviD Duration : 1 h 35 min Bit rate : 800 kb/s Width : 544 pixels Height : 416 pixels Display aspect ratio : 4:3 Frame rate : 25.000 FPS Color space : YUV Chroma subsampling : 4:2:0 Bit depth : 8 bits Scan type : Progressive Compression mode : Lossy Bits/(Pixel*Frame) : 0.141 Stream size : 549 MiB (80%) Writing library : XviD 0.0.09 (UTC 2003-03-25)
I have the following file, which here system could not read:
File in the incorrect format:
General Complete name : amf.avi Format : AVI Format/Info : Audio Video Interleave Overall bit rate : 2 167 kb/s Video ID : 0 Format : MPEG-4 Visual Format profile : Advanced Simple@L5 Format settings : BVOP1 Format settings, BVOP : 1 Format settings, QPel : No Format settings, GMC : No warppoints Format settings, Matrix : Default (MPEG) Codec ID : DX50 Codec ID/Hint : DivX 5 Bit rate : 1 710 kb/s Width : 720 pixels Height : 528 pixels Display aspect ratio : 4:3 Frame rate : 23.976 (24000/1001) FPS Color space : YUV Chroma subsampling : 4:2:0 Bit depth : 8 bits Scan type : Progressive Compression mode : Lossy Bits/(Pixel*Frame) : 0.188 Stream size : 1.15 GiB (79%) Writing library : XviD 64
Could you help me to convert the second file to a format of the first? (It would be nice if the output would be of the similar quality)
I have Ubuntu, and I think
ffmpeg
should be able to do it.CROSS POSTED FROM: SE Video production.
-
Why are no dts pts written to my mp4 container
14 mai 2019, par KiamurBased on my (self-answered) question here Muxing AVPackets into mp4 file - revisited, I have to ask, what could be the reason why there are no values written for pts/dts in the resulting mp4 container.
I examined the container file with the tool MediaInfo. I observe that only the very first Frame contains a value for pts in the container. After that, pts is not even shown in the mp4 file anymore, but dts is, with a value of all zeros.
This is the output from MediaInfo for the first 3 frames:
0000A2 slice_layer_without_partitioning (IDR) - 0 (0x0) - Frame 0 - slice_type I - frame_num 0 - DTS 00:00:00.000 - PTS 00:00:00.017 (141867 bytes) 0000A2 Header (5 bytes) 0000A2 zero_byte: 0 (0x00) 0000A3 start_code_prefix_one_3bytes: 1 (0x000001) 0000A6 nal_ref_idc: 3 (0x3) - (2 bits) 0000A6 nal_unit_type: 5 (0x05) - (5 bits) 0000A7 slice_header (3 bytes) 0000A7 first_mb_in_slice: 0 (0x0) 0000A7 slice_type: 7 (0x07) - I 0000A8 pic_parameter_set_id: 0 (0x0) 0000A8 frame_num: 0 (0x0) 0000A8 idr_pic_id: 0 (0x0) 0000A8 no_output_of_prior_pics_flag: No 0000A8 long_term_reference_flag: No 0000A9 slice_qp_delta: -5 (0xFFFFFFFB) 0000AA disable_deblocking_filter_idc: 0 (0x0) 0000AA slice_alpha_c0_offset_div2: 0 (0x0) 0000AA slice_beta_offset_div2: 0 (0x0) 0000AA slice_data (141856 bytes) 0000AA (ToDo): (Data) 022ACD slice_layer_without_partitioning (IDR) - 0 (0x0) - Frame 0 - slice_type I - frame_num 0 - DTS 00:00:00.000 - PTS 00:00:00.017 - first_mb_in_slice 8040 (2248 bytes) 022ACD Header (5 bytes) 022ACD zero_byte: 0 (0x00) 022ACE start_code_prefix_one_3bytes: 1 (0x000001) 022AD1 nal_ref_idc: 3 (0x3) - (2 bits) 022AD1 nal_unit_type: 5 (0x05) - (5 bits) 022AD2 slice_header (6 bytes) 022AD2 first_mb_in_slice: 8040 (0x001F68) 022AD5 slice_type: 7 (0x07) - I 022AD6 pic_parameter_set_id: 0 (0x0) 022AD6 frame_num: 0 (0x0) 022AD6 idr_pic_id: 0 (0x0) 022AD6 no_output_of_prior_pics_flag: No 022AD6 long_term_reference_flag: No 022AD7 slice_qp_delta: -5 (0xFFFFFFFB) 022AD8 disable_deblocking_filter_idc: 0 (0x0) 022AD8 slice_alpha_c0_offset_div2: 0 (0x0) 022AD8 slice_beta_offset_div2: 0 (0x0) 022AD8 slice_data (2237 bytes) 022AD8 (ToDo): (Data) 023395 1 (36212 bytes) 023395 slice_layer_without_partitioning (non-IDR) - 2 (0x2) - Frame 1 - slice_type P - frame_num 1 - DTS 00:00:00.000 (36017 bytes) 023395 Header (5 bytes) 023395 zero_byte: 0 (0x00) 023396 start_code_prefix_one_3bytes: 1 (0x000001) 023399 nal_ref_idc: 3 (0x3) - (2 bits) 023399 nal_unit_type: 1 (0x01) - (5 bits) 02339A slice_header (3 bytes) 02339A first_mb_in_slice: 0 (0x0) 02339A slice_type: 5 (0x5) - P 02339A pic_parameter_set_id: 0 (0x0) 02339A frame_num: 1 (0x1) 02339B num_ref_idx_active_override_flag (0 bytes) 02339B num_ref_idx_active_override_flag: Yes 02339B num_ref_idx_l0_active_minus1: 0 (0x0) 02339B ref_pic_list_modification_flag_l0: No 02339B adaptive_ref_pic_marking_mode_flag: No 02339C cabac_init_idc: 0 (0x0) 02339C slice_qp_delta: -3 (0xFFFFFFFD) 02339C disable_deblocking_filter_idc: 0 (0x0) 02339C slice_alpha_c0_offset_div2: 0 (0x0) 02339D slice_beta_offset_div2: 0 (0x0) 02339D slice_data (36012 bytes) 02339D (ToDo): (Data) 02C046 slice_layer_without_partitioning (non-IDR) - 2 (0x2) - Frame 1 - slice_type P - frame_num 1 - DTS 00:00:00.000 - first_mb_in_slice 8040 (195 bytes) 02C046 Header (5 bytes) 02C046 zero_byte: 0 (0x00) 02C047 start_code_prefix_one_3bytes: 1 (0x000001) 02C04A nal_ref_idc: 3 (0x3) - (2 bits) 02C04A nal_unit_type: 1 (0x01) - (5 bits) 02C04B slice_header (6 bytes) 02C04B first_mb_in_slice: 8040 (0x001F68) 02C04E slice_type: 5 (0x5) - P 02C04E pic_parameter_set_id: 0 (0x0) 02C04E frame_num: 1 (0x1) 02C04F num_ref_idx_active_override_flag (0 bytes) 02C04F num_ref_idx_active_override_flag: Yes 02C04F num_ref_idx_l0_active_minus1: 0 (0x0) 02C04F ref_pic_list_modification_flag_l0: No 02C04F adaptive_ref_pic_marking_mode_flag: No 02C050 cabac_init_idc: 0 (0x0) 02C050 slice_qp_delta: -3 (0xFFFFFFFD) 02C050 disable_deblocking_filter_idc: 0 (0x0) 02C050 slice_alpha_c0_offset_div2: 0 (0x0) 02C051 slice_beta_offset_div2: 0 (0x0) 02C051 slice_data (190 bytes) 02C051 (ToDo): (Data) 02C109 1 (26280 bytes) 02C109 slice_layer_without_partitioning (non-IDR) - 4 (0x4) - Frame 2 - slice_type P - frame_num 2 - DTS 00:00:00.000 (26157 bytes) 02C109 Header (5 bytes) 02C109 zero_byte: 0 (0x00) 02C10A start_code_prefix_one_3bytes: 1 (0x000001) 02C10D nal_ref_idc: 3 (0x3) - (2 bits) 02C10D nal_unit_type: 1 (0x01) - (5 bits) 02C10E slice_header (3 bytes) 02C10E first_mb_in_slice: 0 (0x0) 02C10E slice_type: 5 (0x5) - P 02C10E pic_parameter_set_id: 0 (0x0) 02C10E frame_num: 2 (0x2) 02C10F num_ref_idx_active_override_flag (0 bytes) 02C10F num_ref_idx_active_override_flag: Yes 02C10F num_ref_idx_l0_active_minus1: 0 (0x0) 02C10F ref_pic_list_modification_flag_l0: No 02C10F adaptive_ref_pic_marking_mode_flag: No 02C110 cabac_init_idc: 0 (0x0) 02C110 slice_qp_delta: -2 (0xFFFFFFFE) 02C110 disable_deblocking_filter_idc: 0 (0x0) 02C110 slice_alpha_c0_offset_div2: 0 (0x0) 02C111 slice_beta_offset_div2: 0 (0x0) 02C111 slice_data (26152 bytes) 02C111 (ToDo): (Data) 032736 slice_layer_without_partitioning (non-IDR) - 4 (0x4) - Frame 2 - slice_type P - frame_num 2 - DTS 00:00:00.000 - first_mb_in_slice 8040 (123 bytes) 032736 Header (5 bytes) 032736 zero_byte: 0 (0x00) 032737 start_code_prefix_one_3bytes: 1 (0x000001) 03273A nal_ref_idc: 3 (0x3) - (2 bits) 03273A nal_unit_type: 1 (0x01) - (5 bits) 03273B slice_header (6 bytes) 03273B first_mb_in_slice: 8040 (0x001F68) 03273E slice_type: 5 (0x5) - P 03273E pic_parameter_set_id: 0 (0x0) 03273E frame_num: 2 (0x2) 03273F num_ref_idx_active_override_flag (0 bytes) 03273F num_ref_idx_active_override_flag: Yes 03273F num_ref_idx_l0_active_minus1: 0 (0x0) 03273F ref_pic_list_modification_flag_l0: No 03273F adaptive_ref_pic_marking_mode_flag: No 032740 cabac_init_idc: 0 (0x0) 032740 slice_qp_delta: -2 (0xFFFFFFFE) 032740 disable_deblocking_filter_idc: 0 (0x0) 032740 slice_alpha_c0_offset_div2: 0 (0x0) 032741 slice_beta_offset_div2: 0 (0x0) 032741 slice_data (118 bytes) 032741 (ToDo): (Data) 0327B1 1 (21125 bytes)
It goes on like that, even though I set pts and dts. The settings may not be correct already (I do some calculations like (1 / framerate) * FrameNumber), but I would expect at least some numbers in pts and dts, when I set the according fields in the avPacket structure and write that via av_interleaved_write_frame(outFmtCtx, &avPacket); to the file.
What could be wrong here?
Edit:
(please see below in the comments the download to my testdata and source file) One thing that bugs me is the fact, if I compare the output of MediaInfo from my file and that of the muxing.c generated is, that in the header, the muxing.c generated already mentions the duration of the file as 9960 ms, whereas mine is only 40 ms.
muxing.c also does call avformat_write_header before even one frame is drawn. Yes, I suppose that the header will be updated, when the either av_interleaved_write_frame or av_write_trailer is called, but I totally not understand the mechanics behind it. Maybe somebody can enlighten me with some background information of any kind.
Additionally, I think it could be necessarry to extract some SPS and PPS from my raw data (preceding the I-slice), and give that as extra data to the avformat_write_header call. But I just cannot figure out myself if I have to do that at all and if so, how to do it.
-
How to switch from yuyv422 to yuv420p for better framerate in ffmpeg on Windows 10
14 mai 2019, par someone from belgium is calledI upgraded PC from Windows 7 to Windows 10, as it is discontinued. Problem is, I had low latency monitoring and recording solution with FFmpeg.
After the upgrade, Logitech camera switched from yuv420p to yuyv422 and I lost 30 fps support at 1280x720. Now it is only limited to 10 FPS.
Tried different drivers, it still yuyv422
Here is a code i use.
ffmpeg -y -loglevel panic -hwaccel qsv -threads 1 -fflags nobuffer -flags low_delay -strict experimental -f dshow -video_size 1280x720 -framerate 10 -pixel_format yuyv422 -i video="C922 Pro Stream Webcam" -codec:v libx264 -preset ultrafast -crf 24 -tune zerolatency -map 0 -f segment -segment_time 600 -segment_wrap 2 -reset_timestamps 1 dvr_%%04d.avi -codec:v copy -f nut - | ffplay -fflags nobuffer -flags low_delay -vf scale=1920x1080:flags=lanczos -window_title "kamera" -noborder -left 1920 -top 150 -fast -framedrop -
I really need low CPU, no latency monitoring at minimum 24 FPS and recording capabilities. File size doesn't matter so much.
Using mjpeg eats CPU like crazy.
-
Playing MP4 file in Powerpoint
14 mai 2019, par BrewGoldI have a huge MP4 file (4GB) an hour long video. I want to extract port of the file. So I used the following command to extract 70 seconds(00:01:10) of video starting from 11 minutes
ffmpeg -i INPUT.mp4 -ss 00:11:00 -t 00:01:10 -c:v copy -c:a copy OUTPUT.mp4
Now I got a small file extracted from Input.MP4 The output.mp4 file size is still big(90 MB). So I used the following command
ffmpeg -i OUTPUT.mp4 -c:v libx264 -crf 30 SmallSizeVideo.avi
I got SmallSizeVideo.avi file which is approximately 6MB.
I am using Powerpoint 2010. I want to insert the video in Powerpoint and play.
Unfortunately when I embed SmallSizeVideo.avi Powerpoint is unable to play
1) Is my approach correct? 2) What is the best way to situation like me to play small portion of clips in powerpoint
Thank you