
Recherche avancée
Médias (1)
-
Bug de détection d’ogg
22 mars 2013, par
Mis à jour : Avril 2013
Langue : français
Type : Video
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 (...) -
MediaSPIP v0.2
21 juin 2013, parMediaSPIP 0.2 est la première version de MediaSPIP stable.
Sa date de sortie officielle est le 21 juin 2013 et est annoncée ici.
Le fichier zip ici présent contient uniquement les sources de MediaSPIP en version standalone.
Comme pour la version précédente, il est nécessaire d’installer manuellement l’ensemble des dépendances logicielles sur le serveur.
Si vous souhaitez utiliser cette archive pour une installation en mode ferme, il vous faudra également procéder à d’autres modifications (...) -
MediaSPIP version 0.1 Beta
16 avril 2011, parMediaSPIP 0.1 beta est la première version de MediaSPIP décrétée comme "utilisable".
Le fichier zip ici présent contient uniquement les sources de MediaSPIP en version standalone.
Pour avoir une installation fonctionnelle, il est nécessaire d’installer manuellement l’ensemble des dépendances logicielles sur le serveur.
Si vous souhaitez utiliser cette archive pour une installation en mode ferme, il vous faudra également procéder à d’autres modifications (...)
Sur d’autres sites (8703)
-
Apostrophe issue with FFmpeg
1er mars 2024, par Rohan MolinilloI'm working on a company's project which is vue.js.
There are also code parts in php.
It uses FFmpeg to create a video from multiple videos.
On each video, there is a text type subtitle.
Each text is retrieved from a .txt file
But I have a problem with apostrophes.


If in the subtitle is stored like this ( I'm here ) in the txt file, on the video there will be written ( Im ).
The apostrophe is removed and the rest of the text too ( here ) will not be displayed.


I'm new to php and ffmpeg, I've been on this problem for almost 3 weeks.


I share the php code with you.


<?php

declare(strict_types=1);

array_shift($argv); // remove script name in $argv[0]

$parameters = array_reduce($argv, function ($carry, $arg) {
 $tokens = explode('=', $arg);
 $carry[$tokens[0]] = $tokens[1];
 return $carry;
}, []);

$projectPath = $parameters['projectPath'];
$musicPath = $parameters['musicPath'];

$fontPath = getcwd() . "/public/fonts/cobol/Cobol-Bold.ttf";
$logoPath = getcwd() . "/public/images/saintex.jpg";
$carnetLogoPath = getcwd() . "/public/images/CarnetTitre.jpg";

// Adding descriptions for each clip and fade in and fade out filters
$clipsToDescribe = glob("$projectPath/*.webm");
$clipFrameRate = (int) shell_exec("cd $projectPath && ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate $clipsToDescribe[0]");
$clipFrameRate = $clipFrameRate > 60 ? 30 : $clipFrameRate;

foreach ($clipsToDescribe as $key => $clipToDescribe) {
 $clipIndex = $key + 1;
 $clipFrames = (int) shell_exec("cd $projectPath && ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 $clipToDescribe");
 $clipDuration = (float) ($clipFrames / $clipFrameRate) - 0.5;
 file_put_contents("$projectPath/clip{$clipIndex}_desc.txt", addslashes($parameters["clip{$clipIndex}_desc"]));
 shell_exec("cd $projectPath && ffmpeg -i $clipToDescribe -vf 'drawtext=fontfile=$fontPath: textfile=clip{$clipIndex}_desc.txt: fontcolor=white: fontsize=46: box=1: boxcolor=black@0.5: boxborderw=5: x=(w-text_w)/2: y=(h-text_h-50): fix_bounds=true, fade=t=in:st=0:d=0.3,fade=t=out:st=$clipDuration:d=0.3' -b:v 3000K -b:a 192K clip{$clipIndex}.webm");
}
array_map('unlink', glob("$projectPath/*desc.txt"));

shell_exec("cd $projectPath && ffmpeg -t 2 -f lavfi -i color=c=black:s=1280x720 -r 30 blank.webm");
shell_exec("cd $projectPath && ffmpeg -i blank.webm -i $carnetLogoPath -filter_complex '[0:v][1:v] overlay=(main_w/2)-(overlay_w/2):(main_h/2)-(overlay_h/2)' -pix_fmt yuv420p -c:a copy logo.webm");



$workshop = $parameters["workshop_type"];
$title = $parameters["title"];
shell_exec("cd $projectPath && ffmpeg -f lavfi -i color=size=1280x720:duration=3:rate=30:color=black -vf 'drawtext=text=$workshop:fontfile=$fontPath:fontcolor=white:fontsize=46:x=(w-text_w)/2:y=(h-text_h)/2, drawtext=text=$title:fontfile=$fontPath:fontcolor=white:fontsize=46:x=(w-text_w)/2:y=((h-text_h)/2)+lh+5' opening.webm");
unlink("$projectPath/blank.webm");

$videosFile = "file 'logo.webm'\n";
$videosFile .= "file 'opening.webm'\n";{
 file_put_contents("$projectPath/project_desc.txt", $parameters["project_desc"]);
 shell_exec("cd $projectPath && ffmpeg -f lavfi -i color=size=1280x720:duration=3:rate=30:color=black -vf 'drawtext=fontfile=$fontPath:fontsize=46:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:textfile=project_desc.txt' project_desc.webm");
 unlink("$projectPath/project_desc.txt");
 $videosFile .= "file 'project_desc.webm'\n";
} 
if(array_key_exists("participants", $parameters)) {
 file_put_contents("$projectPath/participants.txt", "Participants :\n" . $parameters["participants"]);
 shell_exec("cd $projectPath && ffmpeg -f lavfi -i color=size=1280x720:duration=2:rate=30:color=black -vf 'drawtext=fontfile=$fontPath:fontsize=46:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:textfile=participants.txt' participants.webm");
 unlink("$projectPath/participants.txt");
}

$videos = glob("$projectPath/clip*.webm");
foreach($videos as $video) {
 if($video != "originalVideos") {
 $videosFile .= "file ". "'{$video}'\n";
 }
} 
if(array_key_exists("participants", $parameters)) {
 $videosFile .= "file '$projectPath/participants.webm'";
}
array_map('unlink', glob("$projectPath/*webm.txt"));
file_put_contents("$projectPath/videosFile.txt", $videosFile);
if($musicPath == "/_musics/") {
 echo(shell_exec("cd $projectPath && ffmpeg -f concat -safe 0 -i videosFile.txt -b:v 10000K -crf 20 -b:a 192K output.webm"));
} else {
 echo(shell_exec("cd $projectPath && ffmpeg -f concat -safe 0 -i videosFile.txt -b:v 10000K -crf 20 -b:a 192K assembled.webm && ffmpeg -i assembled.webm -i ../..$musicPath -filter_complex ' [1:0] apad ' -shortest -y -b:v 3000K -b:a 192K output.webm"));
}



I tried many things but each time there were errors.
I think I didn't implement the code properly.


I share you the error


[09:21:02] RECEIVED EVENT: videoRequest
{ Error: Command failed: php ./public/src/generate.php projectPath='/home/rohan/Documents/dodoc2/_projects/its-a-test' musicPath='/_musics/classic.mp3' clip1_name='' clip2_name='' clip3_name='' clip4_name='' clip5_name='' clip1_desc='It's a first test' clip2_desc='It's a second test' clip3_desc='It's a third test' clip4_desc='It's a fourth' clip5_desc='It's a last test' project_desc='' workshop_type='Atelier Robotique' title='It's a test' participants='Molinillo Rohan
'
PHP Warning: Undefined array key 1 in /home/rohan/carnet-numerique/public/src/generate.php on line 9
PHP Warning: Undefined array key 1 in /home/rohan/carnet-numerique/public/src/generate.php on line 9
PHP Warning: Undefined array key 1 in /home/rohan/carnet-numerique/public/src/generate.php on line 9
PHP Warning: Undefined array key 1 in /home/rohan/carnet-numerique/public/src/generate.php on line 9
PHP Warning: Undefined array key 1 in /home/rohan/carnet-numerique/public/src/generate.php on line 9
PHP Warning: Undefined array key 1 in /home/rohan/carnet-numerique/public/src/generate.php on line 9
ffmpeg version 5.1.1-1ubuntu2.1 Copyright (c) 2000-2022 the FFmpeg developers
 built with gcc 12 (Ubuntu 12.2.0-3ubuntu1)
 configuration: --prefix=/usr --extra-version=1ubuntu2.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libglslang --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librist --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --disable-sndio --enable-pocketsphinx --enable-librsvg --enable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-libplacebo --enable-shared
 libavutil 57. 28.100 / 57. 28.100
 libavcodec 59. 37.100 / 59. 37.100
 libavformat 59. 27.100 / 59. 27.100
 libavdevice 59. 7.100 / 59. 7.100
 libavfilter 8. 44.100 / 8. 44.100
 libswscale 6. 7.100 / 6. 7.100
 libswresample 4. 7.100 / 4. 7.100
 libpostproc 56. 6.100 / 56. 6.100
Input #0, matroska,webm, from '/home/rohan/Documents/dodoc2/_projects/its-a-test/video-20230404-091933-682.webm':
 Metadata:
 encoder : QTmuxingAppLibWebM-0.0.1
 Duration: N/A, start: -0.001000, bitrate: N/A
 Stream #0:0(eng): Video: vp8, yuv420p(progressive), 1280x720, SAR 1:1 DAR 16:9, 1k tbr, 1k tbn (default)
 Stream #0:1(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
Stream mapping:
 Stream #0:0 -> #0:0 (vp8 (native) -> vp9 (libvpx-vp9))
 Stream #0:1 -> #0:1 (opus (native) -> opus (libopus))
Press [q] to stop, [?] for help
[libvpx-vp9 @ 0x55952c183000] v1.12.0
Output #0, webm, to 'clip1.webm':
 Metadata:
 encoder : Lavf59.27.100
 Stream #0:0(eng): Video: vp9, yuv420p(tv, bt470bg/unknown/unknown, progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 3000 kb/s, 1k fps, 1k tbn (default)
 Metadata:
 encoder : Lavc59.37.100 libvpx-vp9
 Side data:
 cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
 Stream #0:1(eng): Audio: opus, 48000 Hz, stereo, flt, 192 kb/s (default)
 Metadata:
 encoder : Lavc59.37.100 libopus
frame= 229 fps= 11 q=12.0 Lsize= 2786kB time=00:00:07.52 bitrate=3034.7kbits/s speed=0.377x 
video:2601kB audio:181kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.175036%
PHP Warning: Undefined array key "clip2_desc" in /home/rohan/carnet-numerique/public/src/generate.php on line 29
PHP Fatal error: Uncaught TypeError: addslashes(): Argument #1 ($string) must be of type string, null given in /home/rohan/carnet-numerique/public/src/generate.php:29
Stack trace:
#0 /home/rohan/carnet-numerique/public/src/generate.php(29): addslashes()
#1 {main}
 thrown in /home/rohan/carnet-numerique/public/src/generate.php on line 29

 at ChildProcess.exithandler (child_process.js:275:12)
 at emitTwo (events.js:126:13)
 at ChildProcess.emit (events.js:214:7)
 at maybeClose (internal/child_process.js:925:16)
 at Socket.stream.socket.on (internal/child_process.js:346:11)
 at emitOne (events.js:116:13)
 at Socket.emit (events.js:211:7)
 at Pipe._handle.close [as _onclose] (net.js:554:12)
 killed: false,
 code: 255,
 signal: null,
 cmd: 'php ./public/src/generate.php projectPath=\'/home/rohan/Documents/dodoc2/_projects/its-a-test\' musicPath=\'/_musics/classic.mp3\' clip1_name=\'\' clip2_name=\'\' clip3_name=\'\' clip4_name=\'\' clip5_name=\'\' clip1_desc=\'It\'s a first test\' clip2_desc=\'It\'s a second test\' clip3_desc=\'It\'s a third test\' clip4_desc=\'It\'s a fourth\' clip5_desc=\'It\'s a last test\' project_desc=\'\' workshop_type=\'Atelier Robotique\' title=\'It\'s a test\' participants=\'Molinillo Rohan\n\'' }



-
How can I fix a segmentation fault in a C program ? [duplicate]
31 mars 2023, par ipegasus

Possible Duplicate :

Segmentation fault



Currently I am upgrading an open source program used for HTTP streaming. It needs to support the latest FFmpeg.
The code compiles fine without any warnings, although I am getting a segmentation fault error.


How can I fix the issue ? And / or, what is the best way to debug ? Please find attached a portion of the code due to size. I will try to add the project to GitHub :)


Sample Usage


# segmenter --i out.ts --l 10 --o stream.m3u8 --d segments --f stream



Makefile


FFLIBS=`pkg-config --libs libavformat libavcodec libavutil`
FFFLAGS=`pkg-config --cflags libavformat libavcodec libavutil`

all:
 gcc -Wall -g segmenter.c -o segmenter ${FFFLAGS} ${FFLIBS}



segmenter.c


/*
 * Copyright (c) 2009 Chase Douglas
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include 
#include 
#include 
#include 
#include 
#include "libavformat/avformat.h"

#include "libavformat/avio.h"

#include <sys></sys>stat.h>

#include "segmenter.h"
#include "libavformat/avformat.h"

#define IMAGE_ID3_SIZE 9171

void printUsage() {
 fprintf(stderr, "\nExample: segmenter --i infile --d baseDir --f baseFileName --o playListFile.m3u8 --l 10 \n");
 fprintf(stderr, "\nOptions: \n");
 fprintf(stderr, "--i <infile>.\n");
 fprintf(stderr, "--o <outfile>.\n");
 fprintf(stderr, "--d basedir, the base directory for files.\n");
 fprintf(stderr, "--f baseFileName, output files will be baseFileName-#.\n");
 fprintf(stderr, "--l segment length, the length of each segment.\n");
 fprintf(stderr, "--a, audio only decode for < 64k streams.\n");
 fprintf(stderr, "--v, video only decode for < 64k streams.\n");
 fprintf(stderr, "--version, print version details and exit.\n");
 fprintf(stderr, "\n\n");
}

void ffmpeg_version() {
 // output build and version numbers
 fprintf(stderr, " libavutil version: %s\n", AV_STRINGIFY(LIBAVUTIL_VERSION));
 fprintf(stderr, " libavutil build: %d\n", LIBAVUTIL_BUILD);
 fprintf(stderr, " libavcodec version: %s\n", AV_STRINGIFY(LIBAVCODEC_VERSION));
 fprintf(stdout, " libavcodec build: %d\n", LIBAVCODEC_BUILD);
 fprintf(stderr, " libavformat version: %s\n", AV_STRINGIFY(LIBAVFORMAT_VERSION));
 fprintf(stderr, " libavformat build: %d\n", LIBAVFORMAT_BUILD);
 fprintf(stderr, " built on " __DATE__ " " __TIME__);
#ifdef __GNUC__
 fprintf(stderr, ", gcc: " __VERSION__ "\n");
#else
 fprintf(stderr, ", using a non-gcc compiler\n");
#endif
}


static AVStream *add_output_stream(AVFormatContext *output_format_context, AVStream *input_stream) {
 AVCodecContext *input_codec_context;
 AVCodecContext *output_codec_context;
 AVStream *output_stream;

 output_stream = avformat_new_stream(output_format_context, 0);
 if (!output_stream) {
 fprintf(stderr, "Segmenter error: Could not allocate stream\n");
 exit(1);
 }

 input_codec_context = input_stream->codec;
 output_codec_context = output_stream->codec;

 output_codec_context->codec_id = input_codec_context->codec_id;
 output_codec_context->codec_type = input_codec_context->codec_type;
 output_codec_context->codec_tag = input_codec_context->codec_tag;
 output_codec_context->bit_rate = input_codec_context->bit_rate;
 output_codec_context->extradata = input_codec_context->extradata;
 output_codec_context->extradata_size = input_codec_context->extradata_size;

 if (av_q2d(input_codec_context->time_base) * input_codec_context->ticks_per_frame > av_q2d(input_stream->time_base) && av_q2d(input_stream->time_base) < 1.0 / 1000) {
 output_codec_context->time_base = input_codec_context->time_base;
 output_codec_context->time_base.num *= input_codec_context->ticks_per_frame;
 } else {
 output_codec_context->time_base = input_stream->time_base;
 }

 switch (input_codec_context->codec_type) {
#ifdef USE_OLD_FFMPEG
 case CODEC_TYPE_AUDIO:
#else
 case AVMEDIA_TYPE_AUDIO:
#endif
 output_codec_context->channel_layout = input_codec_context->channel_layout;
 output_codec_context->sample_rate = input_codec_context->sample_rate;
 output_codec_context->channels = input_codec_context->channels;
 output_codec_context->frame_size = input_codec_context->frame_size;
 if ((input_codec_context->block_align == 1 && input_codec_context->codec_id == CODEC_ID_MP3) || input_codec_context->codec_id == CODEC_ID_AC3) {
 output_codec_context->block_align = 0;
 } else {
 output_codec_context->block_align = input_codec_context->block_align;
 }
 break;
#ifdef USE_OLD_FFMPEG
 case CODEC_TYPE_VIDEO:
#else
 case AVMEDIA_TYPE_VIDEO:
#endif
 output_codec_context->pix_fmt = input_codec_context->pix_fmt;
 output_codec_context->width = input_codec_context->width;
 output_codec_context->height = input_codec_context->height;
 output_codec_context->has_b_frames = input_codec_context->has_b_frames;

 if (output_format_context->oformat->flags & AVFMT_GLOBALHEADER) {
 output_codec_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
 }
 break;
 default:
 break;
 }

 return output_stream;
}

int write_index_file(const char index[], const char tmp_index[], const unsigned int planned_segment_duration, const unsigned int actual_segment_duration[],
 const char output_directory[], const char output_prefix[], const char output_file_extension[],
 const unsigned int first_segment, const unsigned int last_segment) {
 FILE *index_fp;
 char *write_buf;
 unsigned int i;

 index_fp = fopen(tmp_index, "w");
 if (!index_fp) {
 fprintf(stderr, "Could not open temporary m3u8 index file (%s), no index file will be created\n", tmp_index);
 return -1;
 }

 write_buf = malloc(sizeof (char) * 1024);
 if (!write_buf) {
 fprintf(stderr, "Could not allocate write buffer for index file, index file will be invalid\n");
 fclose(index_fp);
 return -1;
 }

 unsigned int maxDuration = planned_segment_duration;

 for (i = first_segment; i <= last_segment; i++)
 if (actual_segment_duration[i] > maxDuration)
 maxDuration = actual_segment_duration[i];



 snprintf(write_buf, 1024, "#EXTM3U\n#EXT-X-TARGETDURATION:%u\n", maxDuration);

 if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {
 fprintf(stderr, "Could not write to m3u8 index file, will not continue writing to index file\n");
 free(write_buf);
 fclose(index_fp);
 return -1;
 }

 for (i = first_segment; i <= last_segment; i++) {
 snprintf(write_buf, 1024, "#EXTINF:%u,\n%s-%u%s\n", actual_segment_duration[i], output_prefix, i, output_file_extension);
 if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {
 fprintf(stderr, "Could not write to m3u8 index file, will not continue writing to index file\n");
 free(write_buf);
 fclose(index_fp);
 return -1;
 }
 }

 snprintf(write_buf, 1024, "#EXT-X-ENDLIST\n");
 if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {
 fprintf(stderr, "Could not write last file and endlist tag to m3u8 index file\n");
 free(write_buf);
 fclose(index_fp);
 return -1;
 }

 free(write_buf);
 fclose(index_fp);

 return rename(tmp_index, index);
}

int main(int argc, const char *argv[]) {
 //input parameters
 char inputFilename[MAX_FILENAME_LENGTH], playlistFilename[MAX_FILENAME_LENGTH], baseDirName[MAX_FILENAME_LENGTH], baseFileName[MAX_FILENAME_LENGTH];
 char baseFileExtension[5]; //either "ts", "aac" or "mp3"
 int segmentLength, outputStreams, verbosity, version;



 char currentOutputFileName[MAX_FILENAME_LENGTH];
 char tempPlaylistName[MAX_FILENAME_LENGTH];


 //these are used to determine the exact length of the current segment
 double prev_segment_time = 0;
 double segment_time;
 unsigned int actual_segment_durations[2048];
 double packet_time = 0;

 //new variables to keep track of output size
 double output_bytes = 0;

 unsigned int output_index = 1;
 AVOutputFormat *ofmt;
 AVFormatContext *ic = NULL;
 AVFormatContext *oc;
 AVStream *video_st = NULL;
 AVStream *audio_st = NULL;
 AVCodec *codec;
 int video_index;
 int audio_index;
 unsigned int first_segment = 1;
 unsigned int last_segment = 0;
 int write_index = 1;
 int decode_done;
 int ret;
 int i;

 unsigned char id3_tag[128];
 unsigned char * image_id3_tag;

 size_t id3_tag_size = 73;
 int newFile = 1; //a boolean value to flag when a new file needs id3 tag info in it

 if (parseCommandLine(inputFilename, playlistFilename, baseDirName, baseFileName, baseFileExtension, &outputStreams, &segmentLength, &verbosity, &version, argc, argv) != 0)
 return 0;

 if (version) {
 ffmpeg_version();
 return 0;
 }


 fprintf(stderr, "%s %s\n", playlistFilename, tempPlaylistName);


 image_id3_tag = malloc(IMAGE_ID3_SIZE);
 if (outputStreams == OUTPUT_STREAM_AUDIO)
 build_image_id3_tag(image_id3_tag);
 build_id3_tag((char *) id3_tag, id3_tag_size);

 snprintf(tempPlaylistName, strlen(playlistFilename) + strlen(baseDirName) + 1, "%s%s", baseDirName, playlistFilename);
 strncpy(playlistFilename, tempPlaylistName, strlen(tempPlaylistName));
 strncpy(tempPlaylistName, playlistFilename, MAX_FILENAME_LENGTH);
 strncat(tempPlaylistName, ".", 1);

 //decide if this is an aac file or a mpegts file.
 //postpone deciding format until later
 /* ifmt = av_find_input_format("mpegts");
 if (!ifmt)
 {
 fprintf(stderr, "Could not find MPEG-TS demuxer.\n");
 exit(1);
 } */

 av_log_set_level(AV_LOG_DEBUG);

 av_register_all();
 ret = avformat_open_input(&ic, inputFilename, NULL, NULL);
 if (ret != 0) {
 fprintf(stderr, "Could not open input file %s. Error %d.\n", inputFilename, ret);
 exit(1);
 }

 if (avformat_find_stream_info(ic, NULL) < 0) {
 fprintf(stderr, "Could not read stream information.\n");
 exit(1);
 }

 oc = avformat_alloc_context();
 if (!oc) {
 fprintf(stderr, "Could not allocate output context.");
 exit(1);
 }

 video_index = -1;
 audio_index = -1;

 for (i = 0; i < ic->nb_streams && (video_index < 0 || audio_index < 0); i++) {
 switch (ic->streams[i]->codec->codec_type) {

#ifdef USE_OLD_FFMPEG
 case CODEC_TYPE_VIDEO:
#else
 case AVMEDIA_TYPE_VIDEO:
#endif
 video_index = i;
 ic->streams[i]->discard = AVDISCARD_NONE;
 if (outputStreams & OUTPUT_STREAM_VIDEO)
 video_st = add_output_stream(oc, ic->streams[i]);
 break;
#ifdef USE_OLD_FFMPEG
 case CODEC_TYPE_AUDIO:
#else
 case AVMEDIA_TYPE_AUDIO:
#endif
 audio_index = i;
 ic->streams[i]->discard = AVDISCARD_NONE;
 if (outputStreams & OUTPUT_STREAM_AUDIO)
 audio_st = add_output_stream(oc, ic->streams[i]);
 break;
 default:
 ic->streams[i]->discard = AVDISCARD_ALL;
 break;
 }
 }

 if (video_index == -1) {
 fprintf(stderr, "Stream must have video component.\n");
 exit(1);
 }

 //now that we know the audio and video output streams
 //we can decide on an output format.
 if (outputStreams == OUTPUT_STREAM_AUDIO) {
 //the audio output format should be the same as the audio input format
 switch (ic->streams[audio_index]->codec->codec_id) {
 case CODEC_ID_MP3:
 fprintf(stderr, "Setting output audio to mp3.");
 strncpy(baseFileExtension, ".mp3", strlen(".mp3"));
 ofmt = av_guess_format("mp3", NULL, NULL);
 break;
 case CODEC_ID_AAC:
 fprintf(stderr, "Setting output audio to aac.");
 ofmt = av_guess_format("adts", NULL, NULL);
 break;
 default:
 fprintf(stderr, "Codec id %d not supported.\n", ic->streams[audio_index]->id);
 }
 if (!ofmt) {
 fprintf(stderr, "Could not find audio muxer.\n");
 exit(1);
 }
 } else {
 ofmt = av_guess_format("mpegts", NULL, NULL);
 if (!ofmt) {
 fprintf(stderr, "Could not find MPEG-TS muxer.\n");
 exit(1);
 }
 }
 oc->oformat = ofmt;

 if (outputStreams & OUTPUT_STREAM_VIDEO && oc->oformat->flags & AVFMT_GLOBALHEADER) {
 oc->flags |= CODEC_FLAG_GLOBAL_HEADER;
 }


 /* Deprecated: pass the options to avformat_write_header directly.
 if (av_set_parameters(oc, NULL) < 0) {
 fprintf(stderr, "Invalid output format parameters.\n");
 exit(1);
 }
 */

 av_dump_format(oc, 0, baseFileName, 1);


 //open the video codec only if there is video data
 if (video_index != -1) {
 if (outputStreams & OUTPUT_STREAM_VIDEO)
 codec = avcodec_find_decoder(video_st->codec->codec_id);
 else
 codec = avcodec_find_decoder(ic->streams[video_index]->codec->codec_id);
 if (!codec) {
 fprintf(stderr, "Could not find video decoder, key frames will not be honored.\n");
 }

 if (outputStreams & OUTPUT_STREAM_VIDEO)
 ret = avcodec_open2(video_st->codec, codec, NULL);
 else
 avcodec_open2(ic->streams[video_index]->codec, codec, NULL);
 if (ret < 0) {
 fprintf(stderr, "Could not open video decoder, key frames will not be honored.\n");
 }
 }

 snprintf(currentOutputFileName, strlen(baseDirName) + strlen(baseFileName) + strlen(baseFileExtension) + 10, "%s%s-%u%s", baseDirName, baseFileName, output_index++, baseFileExtension);

 if (avio_open(&oc->pb, currentOutputFileName, URL_WRONLY) < 0) {
 fprintf(stderr, "Could not open '%s'.\n", currentOutputFileName);
 exit(1);
 }
 newFile = 1;

 int r = avformat_write_header(oc,NULL);
 if (r) {
 fprintf(stderr, "Could not write mpegts header to first output file.\n");
 debugReturnCode(r);
 exit(1);
 }

 //no segment info is written here. This just creates the shell of the playlist file
 write_index = !write_index_file(playlistFilename, tempPlaylistName, segmentLength, actual_segment_durations, baseDirName, baseFileName, baseFileExtension, first_segment, last_segment);

 do {
 AVPacket packet;

 decode_done = av_read_frame(ic, &packet);

 if (decode_done < 0) {
 break;
 }

 if (av_dup_packet(&packet) < 0) {
 fprintf(stderr, "Could not duplicate packet.");
 av_free_packet(&packet);
 break;
 }

 //this time is used to check for a break in the segments
 // if (packet.stream_index == video_index && (packet.flags & PKT_FLAG_KEY))
 // {
 // segment_time = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
 // }
#if USE_OLD_FFMPEG
 if (packet.stream_index == video_index && (packet.flags & PKT_FLAG_KEY))
#else
 if (packet.stream_index == video_index && (packet.flags & AV_PKT_FLAG_KEY))
#endif
 {
 segment_time = (double) packet.pts * ic->streams[video_index]->time_base.num / ic->streams[video_index]->time_base.den;
 }
 // else if (video_index < 0)
 // {
 // segment_time = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
 // }

 //get the most recent packet time
 //this time is used when the time for the final segment is printed. It may not be on the edge of
 //of a keyframe!
 if (packet.stream_index == video_index)
 packet_time = (double) packet.pts * ic->streams[video_index]->time_base.num / ic->streams[video_index]->time_base.den; //(double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
 else if (outputStreams & OUTPUT_STREAM_AUDIO)
 packet_time = (double) audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
 else
 continue;
 //start looking for segment splits for videos one half second before segment duration expires. This is because the
 //segments are split on key frames so we cannot expect all segments to be split exactly equally.
 if (segment_time - prev_segment_time >= segmentLength - 0.5) {
 fprintf(stderr, "looking to print index file at time %lf\n", segment_time);
 avio_flush(oc->pb);
 avio_close(oc->pb);

 if (write_index) {
 actual_segment_durations[++last_segment] = (unsigned int) rint(segment_time - prev_segment_time);
 write_index = !write_index_file(playlistFilename, tempPlaylistName, segmentLength, actual_segment_durations, baseDirName, baseFileName, baseFileExtension, first_segment, last_segment);
 fprintf(stderr, "Writing index file at time %lf\n", packet_time);
 }

 struct stat st;
 stat(currentOutputFileName, &st);
 output_bytes += st.st_size;

 snprintf(currentOutputFileName, strlen(baseDirName) + strlen(baseFileName) + strlen(baseFileExtension) + 10, "%s%s-%u%s", baseDirName, baseFileName, output_index++, baseFileExtension);
 if (avio_open(&oc->pb, currentOutputFileName, URL_WRONLY) < 0) {
 fprintf(stderr, "Could not open '%s'\n", currentOutputFileName);
 break;
 }

 newFile = 1;
 prev_segment_time = segment_time;
 }

 if (outputStreams == OUTPUT_STREAM_AUDIO && packet.stream_index == audio_index) {
 if (newFile && outputStreams == OUTPUT_STREAM_AUDIO) {
 //add id3 tag info
 //fprintf(stderr, "adding id3tag to file %s\n", currentOutputFileName);
 //printf("%lf %lld %lld %lld %lld %lld %lf\n", segment_time, audio_st->pts.val, audio_st->cur_dts, audio_st->cur_pkt.pts, packet.pts, packet.dts, packet.dts * av_q2d(ic->streams[audio_index]->time_base) );
 fill_id3_tag((char*) id3_tag, id3_tag_size, packet.dts);
 avio_write(oc->pb, id3_tag, id3_tag_size);
 avio_write(oc->pb, image_id3_tag, IMAGE_ID3_SIZE);
 avio_flush(oc->pb);
 newFile = 0;
 }

 packet.stream_index = 0; //only one stream in audio only segments
 ret = av_interleaved_write_frame(oc, &packet);
 } else if (outputStreams & OUTPUT_STREAM_VIDEO) {
 if (newFile) {
 //fprintf(stderr, "New File: %lld %lld %lld\n", packet.pts, video_st->pts.val, audio_st->pts.val);
 //printf("%lf %lld %lld %lld %lld %lld %lf\n", segment_time, audio_st->pts.val, audio_st->cur_dts, audio_st->cur_pkt.pts, packet.pts, packet.dts, packet.dts * av_q2d(ic->streams[audio_index]->time_base) );
 newFile = 0;
 }
 if (outputStreams == OUTPUT_STREAM_VIDEO)
 ret = av_write_frame(oc, &packet);
 else
 ret = av_interleaved_write_frame(oc, &packet);
 }

 if (ret < 0) {
 fprintf(stderr, "Warning: Could not write frame of stream.\n");
 } else if (ret > 0) {
 fprintf(stderr, "End of stream requested.\n");
 av_free_packet(&packet);
 break;
 }

 av_free_packet(&packet);
 } while (!decode_done);

 //make sure all packets are written and then close the last file.
 avio_flush(oc->pb);
 av_write_trailer(oc);

 if (video_st && video_st->codec)
 avcodec_close(video_st->codec);

 if (audio_st && audio_st->codec)
 avcodec_close(audio_st->codec);

 for (i = 0; i < oc->nb_streams; i++) {
 av_freep(&oc->streams[i]->codec);
 av_freep(&oc->streams[i]);
 }

 avio_close(oc->pb);
 av_free(oc);

 struct stat st;
 stat(currentOutputFileName, &st);
 output_bytes += st.st_size;


 if (write_index) {
 actual_segment_durations[++last_segment] = (unsigned int) rint(packet_time - prev_segment_time);

 //make sure that the last segment length is not zero
 if (actual_segment_durations[last_segment] == 0)
 actual_segment_durations[last_segment] = 1;

 write_index_file(playlistFilename, tempPlaylistName, segmentLength, actual_segment_durations, baseDirName, baseFileName, baseFileExtension, first_segment, last_segment);

 }

 write_stream_size_file(baseDirName, baseFileName, output_bytes * 8 / segment_time);

 return 0;
}
</outfile></infile>


-
Getting the length of video using FFMPEG/FFProble is not working
20 juillet 2016, par Jitender KumarI am struggling to the get the length/duration of a video using FFMPEG. Below is the code which I got from Google, but when I run the method it returns empty string. Did whatever tweaks I could but no success. Can anybody please guide me what is going wrong here ?
private static void GetVideoDuration()
{
string basePath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
string filePath = basePath + @"1.mp4";
string cmd = string.Format("-v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 {0}", filePath);
Process proc = new Process();
proc.StartInfo.FileName = Path.Combine(basePath, @"ffprobe.exe");
proc.StartInfo.Arguments = cmd;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
if (!proc.Start())
{
Console.WriteLine("Error starting");
return;
}
StreamReader reader = proc.StandardError;
string line;
while ((line = reader.ReadToEnd()) != null)
{
Console.WriteLine(line);
}
proc.Close();
}