
Recherche avancée
Médias (1)
-
SPIP - plugins - embed code - Exemple
2 septembre 2013, par
Mis à jour : Septembre 2013
Langue : français
Type : Image
Autres articles (99)
-
MediaSPIP 0.1 Beta version
25 avril 2011, parMediaSPIP 0.1 beta is the first version of MediaSPIP proclaimed as "usable".
The zip file provided here only contains the sources of MediaSPIP in its standalone version.
To get a working installation, you must manually install all-software dependencies on the server.
If you want to use this archive for an installation in "farm mode", you will also need to proceed to other manual (...) -
Personnaliser en ajoutant son logo, sa bannière ou son image de fond
5 septembre 2013, parCertains thèmes prennent en compte trois éléments de personnalisation : l’ajout d’un logo ; l’ajout d’une bannière l’ajout d’une image de fond ;
-
Multilang : améliorer l’interface pour les blocs multilingues
18 février 2011, parMultilang est un plugin supplémentaire qui n’est pas activé par défaut lors de l’initialisation de MediaSPIP.
Après son activation, une préconfiguration est mise en place automatiquement par MediaSPIP init permettant à la nouvelle fonctionnalité d’être automatiquement opérationnelle. Il n’est donc pas obligatoire de passer par une étape de configuration pour cela.
Sur d’autres sites (11945)
-
Crash on ffmpeg avcodec_encode_video in a Console app [closed]
11 janvier 2024, par Robel SharmaI want make an encoder which encode a raw image into h263 format.But after loading and initializing ffmpeg library I got crash on avcodec_encode_video for a demo image.



int _tmain(int argc, _TCHAR* argv[]) {
 avcodec_register_all();
 AVCodec *codec;
 AVCodecContext *c= NULL;
 int i, ret, x, y, got_output;
 FILE *f;
 AVFrame *frame;
 AVPacket pkt;

 int out_size, size, outbuf_size;

 AVFrame *picture;
 uint8_t *outbuf, *picture_buf;

 AVRational rp; 

 rp.den = 1;
 rp.num = 25;
 uint8_t endcode[] = { 0, 0, 1, 0xb7 };

 codec = avcodec_find_encoder(CODEC_ID_H263);

 c = avcodec_alloc_context3(codec);
 picture= avcodec_alloc_frame();
 c->bit_rate = 400000;
 /* resolution must be a multiple of two */
 c->width = 352;
 c->height = 288;
 /* frames per second */
 //c->time_base= (AVRational){1,25};
 c->time_base = rp;
 c->gop_size = 10; /* emit one intra frame every ten frames */
 c->max_b_frames=1;
 c->pix_fmt = PIX_FMT_YUV420P;
 avcodec_open(c, codec);


 outbuf_size = 100000;
 outbuf = (uint8_t*)malloc(outbuf_size);
 size = c->width * c->height;
 picture_buf = (uint8_t*)malloc((size * 3) / 2); /* size for YUV 420 */

 picture->data[0] = picture_buf;
 picture->data[1] = picture->data[0] + size;
 picture->data[2] = picture->data[1] + size / 4;
 picture->linesize[0] = c->width;
 picture->linesize[1] = c->width / 2;
 picture->linesize[2] = c->width / 2;

 /* encode 1 second of video */
 for(i=0;i<25;i++) {
 fflush(stdout);
 /* prepare a dummy image */
 /* Y */
 for(y=0;yheight;y++) {
 for(x=0;xwidth;x++) {
 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
 }
 }
 /* Cb and Cr */
 for(y=0;yheight/2;y++) {
 for(x=0;xwidth/2;x++) {
 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
 }
 }
 /* encode the image */

 **Crash is here** ---> ///////////////////////////////////////////////////
 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);

 printf("encoding frame %3d (size=%5d)\n", i, out_size);
 fwrite(outbuf, 1, out_size, f);
 }
 /* get the delayed frames */
 for(; out_size; i++) {
 fflush(stdout);
 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
 printf("write frame %3d (size=%5d)\n", i, out_size);
 fwrite(outbuf, 1, out_size, f);
 }
 /* add sequence end code to have a real mpeg file */
 outbuf[0] = 0x00;
 outbuf[1] = 0x00;
 outbuf[2] = 0x01;
 outbuf[3] = 0xb7;
 fwrite(outbuf, 1, 4, f);
 fclose(f);
 free(picture_buf);
 free(outbuf);

 avcodec_close(c);
 av_free(c);
 av_free(picture);
 printf("\n");
 return 0;
}



-
How can you combine multiple video files with FFMPEG and merging the audio track as well
19 décembre 2023, par CodrutI'm trying to combine multiple MP4 files in Delphi with the FFMPEG video library. I have the headers unit with all the functions. All videos are
MPEG-4
, and so is the destination output file.

I found this question on Stack Overflow asking the same question. To combine video files while keeping the audio and video tracks.
I have translated the answers to Delphi, and while the code is executed successfully, the output file is invalid and cannot be played.


Here is my implementation :


var
 Files: TArray<pansichar>;
 Output: PAnsiChar;

 I, S: integer;

 i_fmt_ctx: PAVFormatContext;
 i_video_stream: PAVStream;
 o_fmt_ctx: PAVFormatContext;
 o_video_stream: PAVStream;

 P: PPAVStream;
begin
 SetLength(Files, 2);
 Files[0] := PAnsiChar('.\Clips\file9.mp4');
 Files[1] := PAnsiChar('.\Clips\file10.mp4');
 Output := '.\Output\out.mp4';

 avcodec_register_all(); 
 av_register_all();

 (* should set to NULL so that avformat_open_input() allocate a new one *)
 i_fmt_ctx := nil;

 if avformat_open_input(@i_fmt_ctx, Files[0], nil, nil) <> 0 then
 raise Exception.Create('Could not open file');

 if avformat_find_stream_info(i_fmt_ctx, nil) < 0 then
 raise Exception.Create('Could not find stream info');
 
 (* Find 1st video stream *)
 i_video_stream := nil;
 P := i_fmt_ctx.streams;
 for i := 0 to i_fmt_ctx.nb_streams-1 do begin
 if P^.codec.codec_type = AVMEDIA_TYPE_VIDEO then
 begin
 i_video_stream := P^;
 Break;
 end;
 Inc(P);
 end;
 if i_video_stream = nil then
 raise Exception.Create('Could not find video stream');

 avformat_alloc_output_context2(@o_fmt_ctx, nil, nil, Output);

 (*
 since all input files are supposed to be identical (framerate, dimension, color format, ...)
 we can safely set output codec values from first input file
 *)
 o_video_stream := avformat_new_stream(o_fmt_ctx, nil);
 
 var c: PAVCodecContext;
 c := o_video_stream.codec;
 c.bit_rate := 400000;
 c.codec_id := i_video_stream.codec.codec_id;
 c.codec_type := i_video_stream.codec.codec_type;
 c.time_base.num := i_video_stream.time_base.num;
 c.time_base.den := i_video_stream.time_base.den;
 //fprintf(stderr, "time_base.num = %d time_base.den = %d\n", c->time_base.num, c->time_base.den);
 c.width := i_video_stream.codec.width;
 c.height := i_video_stream.codec.height;
 c.pix_fmt := i_video_stream.codec.pix_fmt;
 //printf("%d %d %d", c->width, c->height, c->pix_fmt);
 c.flags := i_video_stream.codec.flags;
 c.flags := c.flags or CODEC_FLAG_GLOBAL_HEADER;
 c.me_range := i_video_stream.codec.me_range;
 c.max_qdiff := i_video_stream.codec.max_qdiff;

 c.qmin := i_video_stream.codec.qmin;
 c.qmax := i_video_stream.codec.qmax;

 c.qcompress := i_video_stream.codec.qcompress;

 c.extradata := i_video_stream.codec.extradata;
 c.extradata_size := i_video_stream.codec.extradata_size;

 avio_open(@o_fmt_ctx.pb, Output, AVIO_FLAG_WRITE);

 (* yes! this is redundant *)
 avformat_close_input(@i_fmt_ctx);

 avformat_write_header(o_fmt_ctx, nil);

 var last_pts: integer; last_pts := 0;
 var last_dts: integer; last_dts := 0;
 for i := 1 to High(Files) do begin
 i_fmt_ctx := nil;

 if avformat_open_input(@i_fmt_ctx, Files[i], nil, nil) <> 0 then
 raise Exception.Create('Could not open input file');

 if avformat_find_stream_info(i_fmt_ctx, nil) < 0 then
 raise Exception.Create('Could not find stream info');

 av_dump_format(i_fmt_ctx, 0, Files[i], 0);
 
 (* we only use first video stream of each input file *)
 i_video_stream := nil;

 P := i_fmt_ctx.streams;
 for S := 0 to i_fmt_ctx.nb_streams-1 do
 begin
 if (P^.codec.codec_type = AVMEDIA_TYPE_VIDEO) then
 begin
 i_video_stream := P^;
 break;
 end;
 
 Inc(P);
 end;

 if i_video_stream = nil then
 raise Exception.Create('Could not find video stream');
 
 var pts, dts: int64;
 pts := 0; dts := 0;
 while true do begin
 var i_pkt: TAVPacket;
 av_init_packet( @i_pkt );
 i_pkt.size := 0;
 i_pkt.data := nil;

 if av_read_frame(i_fmt_ctx, @i_pkt) < 0 then
 break;
 (*
 pts and dts should increase monotonically
 pts should be >= dts
 *)
 i_pkt.flags := i_pkt.flags or AV_PKT_FLAG_KEY;
 pts := i_pkt.pts;
 Inc(i_pkt.pts, last_pts);
 dts := i_pkt.dts;
 Inc(i_pkt.dts, last_dts);
 i_pkt.stream_index := 0;

 // Write
 av_interleaved_write_frame(o_fmt_ctx, @i_pkt);
 end;

 Inc(last_dts, dts);
 Inc(last_pts, pts); 
 
 avformat_close_input(@i_fmt_ctx)
 end;

 av_write_trailer(o_fmt_ctx);

 avcodec_close(o_fmt_ctx.streams^.codec);
 av_freep(&o_fmt_ctx.streams^.codec);
 av_freep(&o_fmt_ctx.streams);

 avio_close(o_fmt_ctx.pb);
 av_free(o_fmt_ctx);
</pansichar>


Which is a translation of
Михаил Чеботарев
's answer.

Even if the code worked, I see no handling of the
AVMEDIA_TYPE_AUDIO
stream, which means this answer is 1/2 of the problem, since It only combines the video stream.

Another approach I tried was using the UBitmaps2Video FFMPEG implementation, which is successfully able to merge the video files, but only the video stream, no audio.


I tried manually converting the audio stream with the Bass Audio Library. It was able to read the audio and write It in a single WAV file, which then I converted to MP3. Finally muxing the combined video file and the MP3 file with
MuxStreams2
. Unfortunately, the audio and video do not align properly. I was unable to pinpoint the issue.

Currently, the only functional option is using the precompiled FFMPEG Executables and using ShellExecute with the according parameters to combine the videos.
This more exactly :


ffmpeg -f concat -safe 0 -i video-list.txt -c copy output.mp4



But I would still rather use the FFMPEG headers in Delphi to combine the videos that way, as that gives the option for Progress indicatiors, more control of the playback and the ability to pause the thread at any point.


So, why does my implementation to merge video files not work. And what is a good method to include the audio stream as well ?


-
TS video copied to MP4, missing 3 first frames when programmatically read (ffmpeg bug)
3 septembre 2023, par Vasilis LemonidisRunning :


ffmpeg -i test.ts -fflags +genpts -c copy -y test.mp4



for this test.ts, which has 30 frames, readable by opencv, I end up with 28 frames, out of which 27 are readable by opencv. More specifically :


ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 tmp.ts 



returns 30.


ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 tmp.mp4



returns 28.


Using OpenCV in that manner


cap = cv2.VideoCapture(tmp_path)
readMat = []
while cap.isOpened():
 ret, frame = cap.read()
 if not ret:
 break
 readMat.append(frame)



I get for the ts file 30 frames, while for the mp4 27 frames.


Could someone explain why the discrepancies ? I get no error during the transformation from ts to mp4 :


ffmpeg version N-111746-gd53acf452f Copyright (c) 2000-2023 the FFmpeg developers
 built with gcc 11.3.0 (GCC)
 configuration: --ld=g++ --bindir=/bin --extra-libs='-lpthread -lm' --pkg-config-flags=--static --enable-static --enable-gpl --enable-libaom --enable-libass --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libsvtav1 --enable-libdav1d --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree --enable-cuda-nvcc --enable-cuvid --enable-nvenc --enable-libnpp 
 libavutil 58. 16.101 / 58. 16.101
 libavcodec 60. 23.100 / 60. 23.100
 libavformat 60. 10.100 / 60. 10.100
 libavdevice 60. 2.101 / 60. 2.101
 libavfilter 9. 10.100 / 9. 10.100
 libswscale 7. 3.100 / 7. 3.100
 libswresample 4. 11.100 / 4. 11.100
 libpostproc 57. 2.100 / 57. 2.100
[mpegts @ 0x4237240] DTS discontinuity in stream 0: packet 5 with DTS 306003, packet 6 with DTS 396001
Input #0, mpegts, from 'tmp.ts':
 Duration: 00:00:21.33, start: 3.400000, bitrate: 15 kb/s
 Program 1 
 Metadata:
 service_name : Service01
 service_provider: FFmpeg
 Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(progressive), 300x300, 1 fps, 3 tbr, 90k tbn
Output #0, mp4, to 'test.mp4':
 Metadata:
 encoder : Lavf60.10.100
 Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 300x300, q=2-31, 1 fps, 3 tbr, 90k tbn
Stream mapping:
 Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[out#0/mp4 @ 0x423e280] video:25kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 4.192123%
frame= 30 fps=0.0 q=-1.0 Lsize= 26kB time=00:00:21.00 bitrate= 10.3kbits/s speed=1e+04x 



Additional information


The origin of the video I am processing comes from a continuous stitching operation of still images ts videos, produced by this class
update
method :

import cv2
import os
import subprocess
from tempfile import NamedTemporaryFile
class VideoUpdater:
 def __init__(
 self, video_path: str, framerate: int, timePerFrame: Optional[int] = None
 ):
 """
 Video updater takes in a video path, and updates it using a supplied frame, based on a given framerate.
 Args:
 video_path: str: Specify the path to the video file
 framerate: int: Set the frame rate of the video
 """
 if not video_path.endswith(".mp4"):
 LOGGER.warning(
 f"File type {os.path.splitext(video_path)[1]} not supported for streaming, switching to ts"
 )
 video_path = os.path.splitext(video_path)[0] + ".mp4"

 self._ps = None
 self.env = {
 
 }
 self.ffmpeg = "/usr/bin/ffmpeg "

 self.video_path = video_path
 self.ts_path = video_path.replace(".mp4", ".ts")
 self.tfile = None
 self.framerate = framerate
 self._video = None
 self.last_frame = None
 self.curr_frame = None


 def update(self, frame: np.ndarray):
 if len(frame.shape) == 2:
 frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
 else:
 frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
 self.writeFrame(frame)

 def writeFrame(self, frame: np.ndarray):
 """
 The writeFrame function takes a frame and writes it to the video file.
 Args:
 frame: np.ndarray: Write the frame to a temporary file
 """


 tImLFrame = NamedTemporaryFile(suffix=".png")
 tVidLFrame = NamedTemporaryFile(suffix=".ts")

 cv2.imwrite(tImLFrame.name, frame)
 ps = subprocess.Popen(
 self.ffmpeg
 + rf"-loop 1 -r {self.framerate} -i {tImLFrame.name} -t {self.framerate} -vcodec libx264 -pix_fmt yuv420p -y {tVidLFrame.name}",
 env=self.env,
 shell=True,
 stdout=subprocess.PIPE,
 stderr=subprocess.PIPE,
 )
 ps.communicate()
 if os.path.isfile(self.ts_path):
 # this does not work to watch, as timestamps are not updated
 ps = subprocess.Popen(
 self.ffmpeg
 + rf'-i "concat:{self.ts_path}|{tVidLFrame.name}" -c copy -y {self.ts_path.replace(".ts", ".bak.ts")}',
 env=self.env,
 shell=True,
 stdout=subprocess.PIPE,
 stderr=subprocess.PIPE,
 )
 ps.communicate()
 shutil.move(self.ts_path.replace(".ts", ".bak.ts"), self.ts_path)

 else:
 shutil.copyfile(tVidLFrame.name, self.ts_path)
 # fixing timestamps, we dont have to wait for this operation
 ps = subprocess.Popen(
 self.ffmpeg
 + rf"-i {self.ts_path} -fflags +genpts -c copy -y {self.video_path}",
 env=self.env,
 shell=True,
 # stdout=subprocess.PIPE,
 # stderr=subprocess.PIPE,
 )
 tImLFrame.close()
 tVidLFrame.close()