
Recherche avancée
Médias (1)
-
Revolution of Open-source and film making towards open film making
6 octobre 2011, par
Mis à jour : Juillet 2013
Langue : English
Type : Texte
Autres articles (105)
-
Encoding and processing into web-friendly formats
13 avril 2011, parMediaSPIP automatically converts uploaded files to internet-compatible formats.
Video files are encoded in MP4, Ogv and WebM (supported by HTML5) and MP4 (supported by Flash).
Audio files are encoded in MP3 and Ogg (supported by HTML5) and MP3 (supported by Flash).
Where possible, text is analyzed in order to retrieve the data needed for search engine detection, and then exported as a series of image files.
All uploaded files are stored online in their original format, so you can (...) -
Formulaire personnalisable
21 juin 2013, parCette page présente les champs disponibles dans le formulaire de publication d’un média et il indique les différents champs qu’on peut ajouter. Formulaire de création d’un Media
Dans le cas d’un document de type média, les champs proposés par défaut sont : Texte Activer/Désactiver le forum ( on peut désactiver l’invite au commentaire pour chaque article ) Licence Ajout/suppression d’auteurs Tags
On peut modifier ce formulaire dans la partie :
Administration > Configuration des masques de formulaire. (...) -
Qu’est ce qu’un masque de formulaire
13 juin 2013, parUn masque de formulaire consiste en la personnalisation du formulaire de mise en ligne des médias, rubriques, actualités, éditoriaux et liens vers des sites.
Chaque formulaire de publication d’objet peut donc être personnalisé.
Pour accéder à la personnalisation des champs de formulaires, il est nécessaire d’aller dans l’administration de votre MediaSPIP puis de sélectionner "Configuration des masques de formulaires".
Sélectionnez ensuite le formulaire à modifier en cliquant sur sont type d’objet. (...)
Sur d’autres sites (6282)
-
missing audio of second video after combining video
27 mai 2021, par JohnTerryI am trying to add
xfade
filter and the command is working but audio of second video is missing in output video.

command is -


ffmpeg -i 1.mp4 -i 1.mp4 -filter_complex "[0:v]scale=480:360:force_original_aspect_ratio=decrease,pad=480:360:-1:-1,setsar=1,fps=30,format=yuv420p[v0];[1:v]scale=480:360:force_original_aspect_ratio=decrease,pad=480:360:-1:-1,setsar=1,fps=30,format=yuv420p[v1];
[v0][v1]xfade=transition=pixelize:duration=1:offset=7,format=yuv420p" -c:v libvpx-vp9 -crf 30 -b:v 0 -b:a 128k -c:a libopus -deadline good -cpu-used 8 output.webm



the full log is below-


ffmpeg version n4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
configuration: --prefix= --prefix=/usr --disable-debug --disable-doc - 
-disable-static --enable-cuda --enable-cuda-sdk --enable-cuvid -- 
enable-libdrm --enable-ffplay --enable-gnutls --enable-gpl --enable- 
libass --enable-libfdk-aac --enable-libfontconfig --enable-libfreetype 
--enable-libmp3lame --enable-libnpp --enable-libopencore_amrnb -- 
enable-libopencore_amrwb --enable-libopus --enable-libpulse --enable-
sdl2 --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libv4l2 --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxvid --enable-nonfree --enable-nvenc --enable-omx --enable-openal --enable-opencl --enable-runtime-cpudetect --enable-shared --enable-vaapi --enable-vdpau --enable-version3 --enable-xlib
 libavutil 56. 51.100 / 56. 51.100
 libavcodec 58. 91.100 / 58. 91.100
 libavformat 58. 45.100 / 58. 45.100
 libavdevice 58. 10.100 / 58. 10.100
 libavfilter 7. 85.100 / 7. 85.100
 libswscale 5. 7.100 / 5. 7.100
 libswresample 3. 7.100 / 3. 7.100
 libpostproc 55. 7.100 / 55. 7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1.mp4':
 Metadata:
 major_brand : isom
 minor_version : 512
 compatible_brands: isomiso2avc1mp41
 encoder : Lavf58.29.100
 Duration: 00:00:09.12, start: 0.000000, bitrate: 702 kb/s
 Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 474x360, 568 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
 Metadata:
 handler_name : VideoHandler
 Stream #0:1(und): Audio: mp3 (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 127 kb/s (default)
 Metadata:
 handler_name : SoundHandler
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '2.mp4':
 Metadata:
 major_brand : isom
 minor_version : 512
 compatible_brands: isomiso2avc1mp41
 encoder : Lavf58.29.100
 Duration: 00:00:08.75, start: 0.000000, bitrate: 2546 kb/s
 Stream #1:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 640x360, 2416 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
 Metadata:
 handler_name : VideoHandler
 Stream #1:1(und): Audio: mp3 (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 127 kb/s (default)
 Metadata:
 handler_name : SoundHandler
Stream mapping:
 Stream #0:0 (h264) -> scale (graph 0)
 Stream #1:0 (h264) -> scale (graph 0)
 format (graph 0) -> Stream #0:0 (libvpx-vp9)
 Stream #0:1 -> #0:1 (mp3 (mp3float) -> opus (libopus))
Press [q] to stop, [?] for help
[libvpx-vp9 @ 0x561afcf3a280] v1.7.0
Output #0, webm, to 'output.webm':
 Metadata:
 major_brand : isom
 minor_version : 512
 compatible_brands: isomiso2avc1mp41
 encoder : Lavf58.45.100
 Stream #0:0: Video: vp9 (libvpx-vp9), yuv420p, 480x360 [SAR 1:1 DAR 4:3], q=-1--1, 30 fps, 1k tbn, 30 tbc (default)
 Metadata:
 encoder : Lavc58.91.100 libvpx-vp9
 Side data:
 cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
 Stream #0:1(und): Audio: opus (libopus), 48000 Hz, stereo, flt, 128 kb/s (default)
 Metadata:
 handler_name : SoundHandler
 encoder : Lavc58.91.100 libopus
frame= 87 fps=0.0 q=0.0 size= 1kB time=00:00:03.19 bitrate= 2.1kbits/sframe= 157 fps=156 q=0.0 size= 171kB time=00:00:05.51 bitrate= 254.3kbits/sframe= 227 fps=150 q=0.0 size= 171kB time=00:00:07.85 bitrate= 178.5kbits/sframe= 265 fps=131 q=0.0 size= 171kB time=00:00:08.31 bitrate= 168.7kbits/sframe= 303 fps=120 q=0.0 size= 256kB time=00:00:09.30 bitrate= 225.5kbits/sframe= 341 fps=113 q=0.0 size= 256kB time=00:00:10.56 bitrate= 198.4kbits/sframe= 379 fps=107 q=0.0 size= 256kB time=00:00:11.83 bitrate= 177.2kbits/sframe= 416 fps=103 q=0.0 size= 256kB time=00:00:13.06 bitrate= 160.5kbits/sframe= 454 fps=100 q=0.0 size= 256kB time=00:00:14.33 bitrate= 146.3kbits/sframe= 472 fps= 93 q=0.0 Lsize= 1168kB time=00:00:15.73 bitrate= 608.2kbits/s speed=3.09x 
video:1036kB audio:125kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.629681%



when using online url which has no audio getting this error-
getting this error-


ffmpeg version n4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
 built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
 configuration: --prefix= --prefix=/usr --disable-debug --disable-doc --disable-static --enable-cuda --enable-cuda-sdk --enable-cuvid --enable-libdrm --enable-ffplay --enable-gnutls --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfontconfig --enable-libfreetype --enable-libmp3lame --enable-libnpp --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopus --enable-libpulse --enable-sdl2 --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libv4l2 --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxvid --enable-nonfree --enable-nvenc --enable-omx --enable-openal --enable-opencl --enable-runtime-cpudetect --enable-shared --enable-vaapi --enable-vdpau --enable-version3 --enable-xlib
 libavutil 56. 51.100 / 56. 51.100
 libavcodec 58. 91.100 / 58. 91.100
 libavformat 58. 45.100 / 58. 45.100
 libavdevice 58. 10.100 / 58. 10.100
 libavfilter 7. 85.100 / 7. 85.100
 libswscale 5. 7.100 / 5. 7.100
 libswresample 3. 7.100 / 3. 7.100
 libpostproc 55. 7.100 / 55. 7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'https://player.vimeo.com/external/183300180.sd.mp4?s=8d9b1561f39df586af49fe5ab105f57f5873854b&profile_id=164':
 Metadata:
 major_brand : mp42
 minor_version : 0
 compatible_brands: mp42mp41isomavc1
 creation_time : 2016-09-19T12:39:02.000000Z
 Duration: 00:00:10.00, start: 0.000000, bitrate: 489 kb/s
 Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt470bg/smpte170m/bt470bg), 640x360 [SAR 1:1 DAR 16:9], 486 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc (default)
 Metadata:
 creation_time : 2016-09-19T12:39:02.000000Z
 handler_name : L-SMASH Video Handler
 encoder : AVC Coding
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'https://player.vimeo.com/external/166335878.sd.mp4?s=828bf6e8594fab383d7ee6bac40d3f247cc40af9&profile_id=164':
 Metadata:
 major_brand : mp42
 minor_version : 0
 compatible_brands: mp42mp41isomavc1
 creation_time : 2016-05-12T09:34:11.000000Z
 Duration: 00:00:10.01, start: 0.000000, bitrate: 479 kb/s
 Stream #1:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt470bg/smpte170m/bt470bg), 474x360 [SAR 1:1 DAR 79:60], 345 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc (default)
 Metadata:
 creation_time : 2016-05-12T09:34:11.000000Z
 handler_name : L-SMASH Video Handler
 encoder : AVC Coding
 Stream #1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
 Metadata:
 creation_time : 2016-05-12T09:34:11.000000Z
 handler_name : L-SMASH Audio Handler
Stream specifier ':a' in filtergraph description [0:v]scale=614:360:force_original_aspect_ratio=decrease,pad=614:360:-1:-1,setsar=1,fps=30,format=yuv420p[v0];[1:v]scale=614:360:force_original_aspect_ratio=decrease,pad=614:360:-1:-1,setsar=1,fps=30,format=yuv420p[v1];[v0][v1]xfade=transition=pixelize:duration=1:offset=8,format=yuv420p;[0:a][1:a]acrossfade=d=0 matches no streams.



-
How to execute an ffmpeg function using ```ffmpeg-static-electron```
4 avril 2021, par RadespyThanks in advance.


I'm creating an electron-react-app using electron-forge and want to crop a video file using
child_process.execFile()
from the main process.

OS : Windows 10 Pro


I've tried the following code in the main process after installing
ffmpeg-static-electron
package :

const ffmpeg = require('ffmpeg-static-electron');
const { execFile, exec } = require("child_process");

exec(`${ffmpeg.path}`, (error, stdout, stderr) => {
 if (error) {
 console.log(error.message)
 }
 if (stderr) {
 console.log(stderr)
 }
 console.log(stdout)
 })



I get the following output :


'ffmpeg' is not recognized as an internal or external command,
operable program or batch file.
[1]
[1] Command failed: C:\Users\xxxx\Desktop\electron-forge-working-branch-1\node_modules\ffmpeg-static-electron\bin\win\x64\ffmpeg.exe


[1] ffmpeg version 3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
[1] built with gcc 5.3.0 (GCC)
[1] configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-decklink --enable-zlib
[1] libavutil 55. 17.103 / 55. 17.103
[1] libavcodec 57. 24.102 / 57. 24.102
[1] libavformat 57. 25.100 / 57. 25.100
[1] libavdevice 57. 0.101 / 57. 0.101
[1] libavfilter 6. 31.100 / 6. 31.100
[1] libswscale 4. 0.100 / 4. 0.100
[1] libswresample 2. 0.101 / 2. 0.101
[1] libpostproc 54. 0.100 / 54. 0.100
[1] Hyper fast Audio and Video encoder
[1] usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
[1]
[1] Use -h to get full help or, even better, run 'man ffmpeg'
[1]
[1] ffmpeg version 3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
[1] built with gcc 5.3.0 (GCC)
[1] configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-decklink --enable-zlib
[1] libavutil 55. 17.103 / 55. 17.103
[1] libavcodec 57. 24.102 / 57. 24.102
[1] libavformat 57. 25.100 / 57. 25.100
[1] libavdevice 57. 0.101 / 57. 0.101
[1] libavfilter 6. 31.100 / 6. 31.100
[1] libswscale 4. 0.100 / 4. 0.100
[1] libswresample 2. 0.101 / 2. 0.101
[1] libpostproc 54. 0.100 / 54. 0.100
[1] Hyper fast Audio and Video encoder
[1] usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
[1]
[1] Use -h to get full help or, even better, run 'man ffmpeg'



The command appears to be failing however, the correct output from ffmpeg is subsequently shown.


Can anyone explain whats happening here ?


I've also run a test function :


execFile(`${ffmpeg.path}`, 
 ['ffmpeg -i test_video.mp4 -vf test_video.webm'], 
 {cwd: './temp'}, 
 (error, stdout, stderr) => {
 if (error) {
 console.log(error.message)
 }
 if (stderr) {
 console.log(stderr)
 }
 console.log(stdout)
 })



and am getting the following message :


[1] 'ffmpeg' is not recognized as an internal or external command,
[1] operable program or batch file.
[1] 
[1] Command failed: C:\Users\xxxx\Desktop\electron-forge-working-branch-1\node_modules\ffmpeg-static-electron\bin\win\x64\ffmpeg.exe ffmpeg -i test_video.mp4 -vf test_video.webm

[1] ffmpeg version 3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
[1] built with gcc 5.3.0 (GCC)
[1] configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-decklink --enable-zlib
[1] libavutil 55. 17.103 / 55. 17.103
[1] libavcodec 57. 24.102 / 57. 24.102
[1] libavformat 57. 25.100 / 57. 25.100
[1] libavdevice 57. 0.101 / 57. 0.101
[1] libavfilter 6. 31.100 / 6. 31.100
[1] libswscale 4. 0.100 / 4. 0.100
[1] libswresample 2. 0.101 / 2. 0.101
[1] libpostproc 54. 0.100 / 54. 0.100


[1] Output #0, webm, to 'ffmpeg -i test_video.mp4 -vf test_video.webm':
[1] 

Output file #0 does not contain any stream
[1]
[1] ffmpeg version 3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
[1] built with gcc 5.3.0 (GCC)
[1] configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-decklink --enable-zlib
[1] libavutil 55. 17.103 / 55. 17.103
[1] libavcodec 57. 24.102 / 57. 24.102
[1] libavformat 57. 25.100 / 57. 25.100
[1] libavdevice 57. 0.101 / 57. 0.101
[1] libavfilter 6. 31.100 / 6. 31.100
[1] libswscale 4. 0.100 / 4. 0.100
[1] libswresample 2. 0.101 / 2. 0.101
[1] libpostproc 54. 0.100 / 54. 0.100
[1] Output #0, webm, to 'ffmpeg -i test_video.mp4 -vf test_video.webm':
[1] Output file #0 does not contain any stream



Again, not sure what I'm doing wrong.


Any help with being able to execute ffmpeg functions from within the
Main process
would be very much appreciated.

Thanks.


-
Why does adding audio stream to ffmpeg's libavcodec output container cause a crash ?
29 mars 2021, par SniggerfardimungusAs it stands, my project correctly uses libavcodec to decode a video, where each frame is manipulated (it doesn't matter how) and output to a new video. I've cobbled this together from examples found online, and it works. The result is a perfect .mp4 of the manipulated frames, minus the audio.


My problem is, when I try to add an audio stream to the output container, I get a crash in mux.c that I can't explain. It's in
static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt)
. Wherest->internal->priv_pts->val = pkt->dts;
is attempted,priv_pts
is nullptr.

I don't recall the version number, but this is from a November 4, 2020 ffmpeg build from git.


My
MediaContentMgr
is much bigger than what I have here. I'm stripping out everything to do with the frame manipulation, so if I'm missing anything, please let me know and I'll edit.

The code that, when added, triggers the nullptr exception, is called out inline


The .h :


#ifndef _API_EXAMPLE_H
#define _API_EXAMPLE_H

#include <glad></glad>glad.h>
#include <glfw></glfw>glfw3.h>
#include "glm/glm.hpp"

extern "C" {
#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libavutil></libavutil>avutil.h>
#include <libavutil></libavutil>opt.h>
#include <libswscale></libswscale>swscale.h>
}

#include "shader_s.h"

class MediaContainerMgr {
public:
 MediaContainerMgr(const std::string& infile, const std::string& vert, const std::string& frag, 
 const glm::vec3* extents);
 ~MediaContainerMgr();
 void render();
 bool recording() { return m_recording; }

 // Major thanks to "shi-yan" who helped make this possible:
 // https://github.com/shi-yan/videosamples/blob/master/libavmp4encoding/main.cpp
 bool init_video_output(const std::string& video_file_name, unsigned int width, unsigned int height);
 bool output_video_frame(uint8_t* buf);
 bool finalize_output();

private:
 AVFormatContext* m_format_context;
 AVCodec* m_video_codec;
 AVCodec* m_audio_codec;
 AVCodecParameters* m_video_codec_parameters;
 AVCodecParameters* m_audio_codec_parameters;
 AVCodecContext* m_codec_context;
 AVFrame* m_frame;
 AVPacket* m_packet;
 uint32_t m_video_stream_index;
 uint32_t m_audio_stream_index;
 
 void init_rendering(const glm::vec3* extents);
 int decode_packet();

 // For writing the output video:
 void free_output_assets();
 bool m_recording;
 AVOutputFormat* m_output_format;
 AVFormatContext* m_output_format_context;
 AVCodec* m_output_video_codec;
 AVCodecContext* m_output_video_codec_context;
 AVFrame* m_output_video_frame;
 SwsContext* m_output_scale_context;
 AVStream* m_output_video_stream;
 
 AVCodec* m_output_audio_codec;
 AVStream* m_output_audio_stream;
 AVCodecContext* m_output_audio_codec_context;
};

#endif



And, the hellish .cpp :


#include 
#include 
#include 
#include 
#include 

#include "media_container_manager.h"

MediaContainerMgr::MediaContainerMgr(const std::string& infile, const std::string& vert, const std::string& frag,
 const glm::vec3* extents) :
 m_video_stream_index(-1),
 m_audio_stream_index(-1),
 m_recording(false),
 m_output_format(nullptr),
 m_output_format_context(nullptr),
 m_output_video_codec(nullptr),
 m_output_video_codec_context(nullptr),
 m_output_video_frame(nullptr),
 m_output_scale_context(nullptr),
 m_output_video_stream(nullptr)
{
 // AVFormatContext holds header info from the format specified in the container:
 m_format_context = avformat_alloc_context();
 if (!m_format_context) {
 throw "ERROR could not allocate memory for Format Context";
 }
 
 // open the file and read its header. Codecs are not opened here.
 if (avformat_open_input(&m_format_context, infile.c_str(), NULL, NULL) != 0) {
 throw "ERROR could not open input file for reading";
 }

 printf("format %s, duration %lldus, bit_rate %lld\n", m_format_context->iformat->name, m_format_context->duration, m_format_context->bit_rate);
 //read avPackets (?) from the avFormat (?) to get stream info. This populates format_context->streams.
 if (avformat_find_stream_info(m_format_context, NULL) < 0) {
 throw "ERROR could not get stream info";
 }

 for (unsigned int i = 0; i < m_format_context->nb_streams; i++) {
 AVCodecParameters* local_codec_parameters = NULL;
 local_codec_parameters = m_format_context->streams[i]->codecpar;
 printf("AVStream->time base before open coded %d/%d\n", m_format_context->streams[i]->time_base.num, m_format_context->streams[i]->time_base.den);
 printf("AVStream->r_frame_rate before open coded %d/%d\n", m_format_context->streams[i]->r_frame_rate.num, m_format_context->streams[i]->r_frame_rate.den);
 printf("AVStream->start_time %" PRId64 "\n", m_format_context->streams[i]->start_time);
 printf("AVStream->duration %" PRId64 "\n", m_format_context->streams[i]->duration);
 printf("duration(s): %lf\n", (float)m_format_context->streams[i]->duration / m_format_context->streams[i]->time_base.den * m_format_context->streams[i]->time_base.num);
 AVCodec* local_codec = NULL;
 local_codec = avcodec_find_decoder(local_codec_parameters->codec_id);
 if (local_codec == NULL) {
 throw "ERROR unsupported codec!";
 }

 if (local_codec_parameters->codec_type == AVMEDIA_TYPE_VIDEO) {
 if (m_video_stream_index == -1) {
 m_video_stream_index = i;
 m_video_codec = local_codec;
 m_video_codec_parameters = local_codec_parameters;
 }
 m_height = local_codec_parameters->height;
 m_width = local_codec_parameters->width;
 printf("Video Codec: resolution %dx%d\n", m_width, m_height);
 }
 else if (local_codec_parameters->codec_type == AVMEDIA_TYPE_AUDIO) {
 if (m_audio_stream_index == -1) {
 m_audio_stream_index = i;
 m_audio_codec = local_codec;
 m_audio_codec_parameters = local_codec_parameters;
 }
 printf("Audio Codec: %d channels, sample rate %d\n", local_codec_parameters->channels, local_codec_parameters->sample_rate);
 }

 printf("\tCodec %s ID %d bit_rate %lld\n", local_codec->name, local_codec->id, local_codec_parameters->bit_rate);
 }

 m_codec_context = avcodec_alloc_context3(m_video_codec);
 if (!m_codec_context) {
 throw "ERROR failed to allocate memory for AVCodecContext";
 }

 if (avcodec_parameters_to_context(m_codec_context, m_video_codec_parameters) < 0) {
 throw "ERROR failed to copy codec params to codec context";
 }

 if (avcodec_open2(m_codec_context, m_video_codec, NULL) < 0) {
 throw "ERROR avcodec_open2 failed to open codec";
 }

 m_frame = av_frame_alloc();
 if (!m_frame) {
 throw "ERROR failed to allocate AVFrame memory";
 }

 m_packet = av_packet_alloc();
 if (!m_packet) {
 throw "ERROR failed to allocate AVPacket memory";
 }
}

MediaContainerMgr::~MediaContainerMgr() {
 avformat_close_input(&m_format_context);
 av_packet_free(&m_packet);
 av_frame_free(&m_frame);
 avcodec_free_context(&m_codec_context);


 glDeleteVertexArrays(1, &m_VAO);
 glDeleteBuffers(1, &m_VBO);
}


bool MediaContainerMgr::advance_frame() {
 while (true) {
 if (av_read_frame(m_format_context, m_packet) < 0) {
 // Do we actually need to unref the packet if it failed?
 av_packet_unref(m_packet);
 continue;
 //return false;
 }
 else {
 if (m_packet->stream_index == m_video_stream_index) {
 //printf("AVPacket->pts %" PRId64 "\n", m_packet->pts);
 int response = decode_packet();
 av_packet_unref(m_packet);
 if (response != 0) {
 continue;
 //return false;
 }
 return true;
 }
 else {
 printf("m_packet->stream_index: %d\n", m_packet->stream_index);
 printf(" m_packet->pts: %lld\n", m_packet->pts);
 printf(" mpacket->size: %d\n", m_packet->size);
 if (m_recording) {
 int err = 0;
 //err = avcodec_send_packet(m_output_video_codec_context, m_packet);
 printf(" encoding error: %d\n", err);
 }
 }
 }

 // We're done with the packet (it's been unpacked to a frame), so deallocate & reset to defaults:
/*
 if (m_frame == NULL)
 return false;

 if (m_frame->data[0] == NULL || m_frame->data[1] == NULL || m_frame->data[2] == NULL) {
 printf("WARNING: null frame data");
 continue;
 }
*/
 }
}

int MediaContainerMgr::decode_packet() {
 // Supply raw packet data as input to a decoder
 // https://ffmpeg.org/doxygen/trunk/group__lavc__decoding.html#ga58bc4bf1e0ac59e27362597e467efff3
 int response = avcodec_send_packet(m_codec_context, m_packet);

 if (response < 0) {
 char buf[256];
 av_strerror(response, buf, 256);
 printf("Error while receiving a frame from the decoder: %s\n", buf);
 return response;
 }

 // Return decoded output data (into a frame) from a decoder
 // https://ffmpeg.org/doxygen/trunk/group__lavc__decoding.html#ga11e6542c4e66d3028668788a1a74217c
 response = avcodec_receive_frame(m_codec_context, m_frame);
 if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
 return response;
 } else if (response < 0) {
 char buf[256];
 av_strerror(response, buf, 256);
 printf("Error while receiving a frame from the decoder: %s\n", buf);
 return response;
 } else {
 printf(
 "Frame %d (type=%c, size=%d bytes) pts %lld key_frame %d [DTS %d]\n",
 m_codec_context->frame_number,
 av_get_picture_type_char(m_frame->pict_type),
 m_frame->pkt_size,
 m_frame->pts,
 m_frame->key_frame,
 m_frame->coded_picture_number
 );
 }
 return 0;
}


bool MediaContainerMgr::init_video_output(const std::string& video_file_name, unsigned int width, unsigned int height) {
 if (m_recording)
 return true;
 m_recording = true;

 advance_to(0L); // I've deleted the implmentation. Just seeks to beginning of vid. Works fine.

 if (!(m_output_format = av_guess_format(nullptr, video_file_name.c_str(), nullptr))) {
 printf("Cannot guess output format.\n");
 return false;
 }

 int err = avformat_alloc_output_context2(&m_output_format_context, m_output_format, nullptr, video_file_name.c_str());
 if (err < 0) {
 printf("Failed to allocate output context.\n");
 return false;
 }

 //TODO(P0): Break out the video and audio inits into their own methods.
 m_output_video_codec = avcodec_find_encoder(m_output_format->video_codec);
 if (!m_output_video_codec) {
 printf("Failed to create video codec.\n");
 return false;
 }
 m_output_video_stream = avformat_new_stream(m_output_format_context, m_output_video_codec);
 if (!m_output_video_stream) {
 printf("Failed to find video format.\n");
 return false;
 } 
 m_output_video_codec_context = avcodec_alloc_context3(m_output_video_codec);
 if (!m_output_video_codec_context) {
 printf("Failed to create video codec context.\n");
 return(false);
 }
 m_output_video_stream->codecpar->codec_id = m_output_format->video_codec;
 m_output_video_stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
 m_output_video_stream->codecpar->width = width;
 m_output_video_stream->codecpar->height = height;
 m_output_video_stream->codecpar->format = AV_PIX_FMT_YUV420P;
 // Use the same bit rate as the input stream.
 m_output_video_stream->codecpar->bit_rate = m_format_context->streams[m_video_stream_index]->codecpar->bit_rate;
 m_output_video_stream->avg_frame_rate = m_format_context->streams[m_video_stream_index]->avg_frame_rate;
 avcodec_parameters_to_context(m_output_video_codec_context, m_output_video_stream->codecpar);
 m_output_video_codec_context->time_base = m_format_context->streams[m_video_stream_index]->time_base;
 
 //TODO(P1): Set these to match the input stream?
 m_output_video_codec_context->max_b_frames = 2;
 m_output_video_codec_context->gop_size = 12;
 m_output_video_codec_context->framerate = m_format_context->streams[m_video_stream_index]->r_frame_rate;
 //m_output_codec_context->refcounted_frames = 0;
 if (m_output_video_stream->codecpar->codec_id == AV_CODEC_ID_H264) {
 av_opt_set(m_output_video_codec_context, "preset", "ultrafast", 0);
 } else if (m_output_video_stream->codecpar->codec_id == AV_CODEC_ID_H265) {
 av_opt_set(m_output_video_codec_context, "preset", "ultrafast", 0);
 } else {
 av_opt_set_int(m_output_video_codec_context, "lossless", 1, 0);
 }
 avcodec_parameters_from_context(m_output_video_stream->codecpar, m_output_video_codec_context);

 m_output_audio_codec = avcodec_find_encoder(m_output_format->audio_codec);
 if (!m_output_audio_codec) {
 printf("Failed to create audio codec.\n");
 return false;
 }



I've commented out all of the audio stream init beyond this next line, because this is where
the trouble begins. Creating this output stream causes the null reference I mentioned. If I
uncomment everything below here, I still get the null deref. If I comment out this line, the
deref exception vanishes. (IOW, I commented out more and more code until I found that this
was the trigger that caused the problem.)


I assume that there's something I'm doing wrong in the rest of the commented out code, that,
when fixed, will fix the nullptr and give me a working audio stream.


m_output_audio_stream = avformat_new_stream(m_output_format_context, m_output_audio_codec);
 if (!m_output_audio_stream) {
 printf("Failed to find audio format.\n");
 return false;
 }
 /*
 m_output_audio_codec_context = avcodec_alloc_context3(m_output_audio_codec);
 if (!m_output_audio_codec_context) {
 printf("Failed to create audio codec context.\n");
 return(false);
 }
 m_output_audio_stream->codecpar->codec_id = m_output_format->audio_codec;
 m_output_audio_stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
 m_output_audio_stream->codecpar->format = m_format_context->streams[m_audio_stream_index]->codecpar->format;
 m_output_audio_stream->codecpar->bit_rate = m_format_context->streams[m_audio_stream_index]->codecpar->bit_rate;
 m_output_audio_stream->avg_frame_rate = m_format_context->streams[m_audio_stream_index]->avg_frame_rate;
 avcodec_parameters_to_context(m_output_audio_codec_context, m_output_audio_stream->codecpar);
 m_output_audio_codec_context->time_base = m_format_context->streams[m_audio_stream_index]->time_base;
 */

 //TODO(P2): Free assets that have been allocated.
 err = avcodec_open2(m_output_video_codec_context, m_output_video_codec, nullptr);
 if (err < 0) {
 printf("Failed to open codec.\n");
 return false;
 }

 if (!(m_output_format->flags & AVFMT_NOFILE)) {
 err = avio_open(&m_output_format_context->pb, video_file_name.c_str(), AVIO_FLAG_WRITE);
 if (err < 0) {
 printf("Failed to open output file.");
 return false;
 }
 }

 err = avformat_write_header(m_output_format_context, NULL);
 if (err < 0) {
 printf("Failed to write header.\n");
 return false;
 }

 av_dump_format(m_output_format_context, 0, video_file_name.c_str(), 1);

 return true;
}


//TODO(P2): make this a member. (Thanks to https://emvlo.wordpress.com/2016/03/10/sws_scale/)
void PrepareFlipFrameJ420(AVFrame* pFrame) {
 for (int i = 0; i < 4; i++) {
 if (i)
 pFrame->data[i] += pFrame->linesize[i] * ((pFrame->height >> 1) - 1);
 else
 pFrame->data[i] += pFrame->linesize[i] * (pFrame->height - 1);
 pFrame->linesize[i] = -pFrame->linesize[i];
 }
}



This is where we take an altered frame and write it to the output container. This works fine
as long as we haven't set up an audio stream in the output container.


bool MediaContainerMgr::output_video_frame(uint8_t* buf) {
 int err;

 if (!m_output_video_frame) {
 m_output_video_frame = av_frame_alloc();
 m_output_video_frame->format = AV_PIX_FMT_YUV420P;
 m_output_video_frame->width = m_output_video_codec_context->width;
 m_output_video_frame->height = m_output_video_codec_context->height;
 err = av_frame_get_buffer(m_output_video_frame, 32);
 if (err < 0) {
 printf("Failed to allocate output frame.\n");
 return false;
 }
 }

 if (!m_output_scale_context) {
 m_output_scale_context = sws_getContext(m_output_video_codec_context->width, m_output_video_codec_context->height, 
 AV_PIX_FMT_RGB24,
 m_output_video_codec_context->width, m_output_video_codec_context->height, 
 AV_PIX_FMT_YUV420P, SWS_BICUBIC, nullptr, nullptr, nullptr);
 }

 int inLinesize[1] = { 3 * m_output_video_codec_context->width };
 sws_scale(m_output_scale_context, (const uint8_t* const*)&buf, inLinesize, 0, m_output_video_codec_context->height,
 m_output_video_frame->data, m_output_video_frame->linesize);
 PrepareFlipFrameJ420(m_output_video_frame);
 //TODO(P0): Switch m_frame to be m_input_video_frame so I don't end up using the presentation timestamp from
 // an audio frame if I threadify the frame reading.
 m_output_video_frame->pts = m_frame->pts;
 printf("Output PTS: %d, time_base: %d/%d\n", m_output_video_frame->pts,
 m_output_video_codec_context->time_base.num, m_output_video_codec_context->time_base.den);
 err = avcodec_send_frame(m_output_video_codec_context, m_output_video_frame);
 if (err < 0) {
 printf(" ERROR sending new video frame output: ");
 switch (err) {
 case AVERROR(EAGAIN):
 printf("AVERROR(EAGAIN): %d\n", err);
 break;
 case AVERROR_EOF:
 printf("AVERROR_EOF: %d\n", err);
 break;
 case AVERROR(EINVAL):
 printf("AVERROR(EINVAL): %d\n", err);
 break;
 case AVERROR(ENOMEM):
 printf("AVERROR(ENOMEM): %d\n", err);
 break;
 }

 return false;
 }

 AVPacket pkt;
 av_init_packet(&pkt);
 pkt.data = nullptr;
 pkt.size = 0;
 pkt.flags |= AV_PKT_FLAG_KEY;
 int ret = 0;
 if ((ret = avcodec_receive_packet(m_output_video_codec_context, &pkt)) == 0) {
 static int counter = 0;
 printf("pkt.key: 0x%08x, pkt.size: %d, counter:\n", pkt.flags & AV_PKT_FLAG_KEY, pkt.size, counter++);
 uint8_t* size = ((uint8_t*)pkt.data);
 printf("sizes: %d %d %d %d %d %d %d %d %d\n", size[0], size[1], size[2], size[2], size[3], size[4], size[5], size[6], size[7]);
 av_interleaved_write_frame(m_output_format_context, &pkt);
 }
 printf("push: %d\n", ret);
 av_packet_unref(&pkt);

 return true;
}

bool MediaContainerMgr::finalize_output() {
 if (!m_recording)
 return true;

 AVPacket pkt;
 av_init_packet(&pkt);
 pkt.data = nullptr;
 pkt.size = 0;

 for (;;) {
 avcodec_send_frame(m_output_video_codec_context, nullptr);
 if (avcodec_receive_packet(m_output_video_codec_context, &pkt) == 0) {
 av_interleaved_write_frame(m_output_format_context, &pkt);
 printf("final push:\n");
 } else {
 break;
 }
 }

 av_packet_unref(&pkt);

 av_write_trailer(m_output_format_context);
 if (!(m_output_format->flags & AVFMT_NOFILE)) {
 int err = avio_close(m_output_format_context->pb);
 if (err < 0) {
 printf("Failed to close file. err: %d\n", err);
 return false;
 }
 }

 return true;
}



EDIT
The call stack on the crash (which I should have included in the original question) :


avformat-58.dll!compute_muxer_pkt_fields(AVFormatContext * s, AVStream * st, AVPacket * pkt) Line 630 C
avformat-58.dll!write_packet_common(AVFormatContext * s, AVStream * st, AVPacket * pkt, int interleaved) Line 1122 C
avformat-58.dll!write_packets_common(AVFormatContext * s, AVPacket * pkt, int interleaved) Line 1186 C
avformat-58.dll!av_interleaved_write_frame(AVFormatContext * s, AVPacket * pkt) Line 1241 C
CamBot.exe!MediaContainerMgr::output_video_frame(unsigned char * buf) Line 553 C++
CamBot.exe!main() Line 240 C++



If I move the call to avformat_write_header so it's immediately before the audio stream initialization, I still get a crash, but in a different place. The crash happens on line 6459 of movenc.c, where we have :


/* Non-seekable output is ok if using fragmentation. If ism_lookahead
 * is enabled, we don't support non-seekable output at all. */
if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) && // CRASH IS HERE
 (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
 return AVERROR(EINVAL);
}



The exception is a nullptr exception, where s->pb is NULL. The call stack is :


avformat-58.dll!mov_init(AVFormatContext * s) Line 6459 C
avformat-58.dll!init_muxer(AVFormatContext * s, AVDictionary * * options) Line 407 C
[Inline Frame] avformat-58.dll!avformat_init_output(AVFormatContext *) Line 489 C
avformat-58.dll!avformat_write_header(AVFormatContext * s, AVDictionary * * options) Line 512 C
CamBot.exe!MediaContainerMgr::init_video_output(const std::string & video_file_name, unsigned int width, unsigned int height) Line 424 C++
CamBot.exe!main() Line 183 C++