Newest 'ffmpeg' Questions - Stack Overflow
Les articles publiés sur le site
-
ffmpeg convert to mp3 with same duration
25 novembre 2015, par VishnuI am using ffmpeg to convert a flv with speex audio codec generated from red5 server to mp3 file. I am using the following command to convert flv to mp3
ffmpeg -i input.flv -acodec libmp3lame output.mp3
input file metadata
Duration: 01:52:46.27, start: 0.000000, bitrate: 15 kb/s Stream #0:0: Audio: speex, 16000 Hz, mono, s16, 0 kb/s
output file metadata
Duration: 01:01:46.42, start: 0.069063, bitrate: 24 kb/s Stream #0:0: Audio: mp3, 16000 Hz, mono, s16p, 24 kb/s
Input file duration is different from output file duration. How to convert the flv to mp3 without changing the duration of the file?
-
FFmpeg recording h264 to a circular buffer "Packet header not contained in global..."
25 novembre 2015, par user15941I've made a class which captures a RTSP h264 stream and stores its Packets in a circular buffer. On demand the content of the buffer is dumped to a file with header and trailer. I based the code mainly on: this question example, but by trial and error I came up with three modifications:
- I use wrapping object for AVPacket not to bother with memory deallocation.
- I convert the data with a filter: "h264_mp4toannexb", so the result file is in .ts format.
- I wait in a loop to get a key-frame before any push_back() to circular buffer.
However something's wrong with the framerate. The only player, that opens my videos correctly is mplayer. VLC and other players I tested show all the frames in one second.
ffmpeg lib also reports a lot of error messages: "Packet header is not contained in global metadata", but I don't know whether the information is held in that header or in some other place?
With this code I assign consecutive pts and dts. Then every packet is stored in the circular buffer.
packet.pts = idx++; packet.dts = packet.pts; if (cbuf_mutex.try_lock()) { cbuf.push_back(PACKET(&packet)); captured_frames++; if (captured_frames > CBUF_SIZE) captured_frames = CBUF_SIZE; cbuf_mutex.unlock(); } else { syslog(LOG_ERR, "Buffer mutex was locked - packet skipped"); }
PACKET class implements copying of the data. Is there something missing, which may cause the errors?
PACKET::PACKET(const PACKET& rhs) { av_new_packet(&packet, rhs.packet.size); av_copy_packet(&packet, &rhs.packet); } PACKET& PACKET::operator=(const PACKET& rhs) { if (&rhs == this) return *this; av_free_packet(&packet); av_new_packet(&packet, rhs.packet.size); av_copy_packet(&packet, &rhs.packet); return *this; }
-
Redux : how to add subtitles to mp4 using ffmpeg ?
25 novembre 2015, par stachyraI have a tiny little example mp4 video (which unfortunately stack overflow won't allow me to embed directly inside my question). Following the advice previously offered in this answer, I'd like to add a subtitle to the video, which will flash on the screen for 2 seconds, using an .srt file that looks like this:
1 00:00:02,000 --> 00:00:04,000 Test subtitle
As provided in the link above, I issue a command that looks like this:
ffmpeg -i small.mp4 -f srt -i small.srt -c:v copy -c:a copy -c:s mov_text small_subtitles.mp4 >& err.log
And the
err.log
file looks like this:ffmpeg version N-76950-g401c93d-tessus Copyright (c) 2000-2015 the FFmpeg developers built with Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn) configuration: --cc=/usr/bin/clang --prefix=/opt/ffmpeg --as=yasm --extra-version=tessus --enable-avisynth --enable-fontconfig --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzmq --enable-version3 --disable-ffplay --disable-indev=qtkit --disable-indev=x11grab_xcb libavutil 55. 7.100 / 55. 7.100 libavcodec 57. 15.100 / 57. 15.100 libavformat 57. 17.100 / 57. 17.100 libavdevice 57. 0.100 / 57. 0.100 libavfilter 6. 15.100 / 6. 15.100 libswscale 4. 0.100 / 4. 0.100 libswresample 2. 0.101 / 2. 0.101 libpostproc 54. 0.100 / 54. 0.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'small.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf56.40.101 Duration: 00:00:10.02, start: 0.000000, bitrate: 23 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 188x112 [SAR 1:1 DAR 47:28], 17 kb/s, 60 fps, 60 tbr, 15360 tbn, 120 tbc (default) Metadata: handler_name : VideoHandler Input #1, srt, from 'small.srt': Duration: N/A, bitrate: N/A Stream #1:0: Subtitle: subrip [mp4 @ 0x7f8492848000] Codec for stream 0 does not use global headers but container format requires global headers Output #0, mp4, to 'small_subtitles.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf57.17.100 Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 188x112 [SAR 1:1 DAR 47:28], q=2-31, 17 kb/s, 60 fps, 60 tbr, 15360 tbn, 15360 tbc (default) Metadata: handler_name : VideoHandler Stream #0:1: Subtitle: mov_text ([8][0][0][0] / 0x0008) (default) Metadata: encoder : Lavc57.15.100 mov_text Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #1:0 -> #0:1 (subrip (srt) -> mov_text (native)) Press [q] to stop, [?] for help frame= 601 fps=0.0 q=-1.0 Lsize= 29kB time=00:00:09.96 bitrate= 24.0kbits/s video:21kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 39.561928%
The resulting output,
small_subtitles.mp4
, does not seem to have the expected subtitle present.Furthermore, the error message,
[mp4 @ 0x7f8492848000] Codec for stream 0 does not use global headers but container format requires global headers
is highlighted in bold colors when I run this at the command line without capturing the
err.log
file (which makes me think it must be a significant message), although I am unable to reproduce that special print formatting easily here.I've also tried this method, but with the .avi replaced by .mp4, and it seems to just fail silently--no obvious error message, but also no subtitle in the resulting video either.
Anybody have an idea how to fix this? In particular, what does the business about global headers mean?
I'm working on OS X and am open to hearing answers that would use other open source tools as well.
-
how to use ffmpeg programmatically ?
24 novembre 2015, par yggI am new to ffmpeg, and I have found many solutions about it are given by command lines,but not codes or functions,so my question is :
Why does people prefer to use command lines in ffmpeg, and how can I transform the command line to codes?
For example, the docs suggest resize your video to a specified size by:
ffmpeg -i input.avi -vf scale=320:240 output.avi
But how can I implement it in my app programmatically?
Btw, there are two libraries named libswresample and libswscale in ffmpeg, what does the "sw" stand for?
-
Saving frames from a network camera (RTSP) to a mp4 file
24 novembre 2015, par Dídac PérezI am quite confused about how to save a video stream into a mp4 file. I am using ffmpeg. Let me explain the problem:
- I connect to a network camera via RTSP (H.264 stream) with avformat_open_input(), avformat_find_stream_info(), av_read_play(), and I get frames with av_read_frame().
- Each time I get a frame with av_read_frame(), I store the corresponding AVPacket in a circular buffer.
- In some points in my application, a range of this circular buffer is selected. I find a key frame used to start from.
- Once I have a list of AVPacket's starting from a key frame, I write header, frames, and tail, as I described below in the code.
The problem is that the resulting mp4 video has artifacts if I try to watch it using VLC, Windows Media Player, or another one.
I have also realized that the pts of that packets are not continuous, while dts are continuous. I know about B frames, but is this a problem in my case?
// Prepare the output AVFormatContext* oc = avformat_alloc_context(); oc->oformat = av_guess_format(NULL, "video.mp4", NULL); // Must write header, packets, and trailing avio_open2(&oc->pb, "video.mp4", AVIO_FLAG_WRITE, NULL, NULL); // Write header AVStream* stream = avformat_new_stream(oc, (AVCodec*) context->streams[video_stream_index]->codec->codec); avcodec_copy_context(stream->codec, context->streams[video_stream_index]->codec); stream->sample_aspect_ratio = context->streams[video_stream_index]->codec->sample_aspect_ratio; avformat_write_header(oc, NULL); // FOR EACH FRAME... ... av_write_frame(oc, circular[k]); ... // Write trailer and close the file av_write_trailer(oc); avcodec_close(stream->codec); avio_close(oc->pb); avformat_free_context(oc);
Thank you so much,