i have a .mp4 video, that is recorded in iphone4s.This video file contains 'Rotate - 180' metadata.
When i am converting the .mp4 file to .ts using ffmpeg. I lost the 'Rotate' meta tag.
The ffmpeg command that i have used is given below.
ffmpeg -i input_file.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb output_file.ts
is there any one know how to set 'Rotate' meta data to a .ts file ?
or
any other way to copy all meta datas in the input .mp4 file to output .ts file
Thank you
Nothing seems to work to add the rotation metadata to .ts files with ffmpeg. Only re-encoding works.
My input file has a rotation value of 90.
Terminal command:
./ffmpeg -i input_file.mp4
Metadata:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input_file.mp4':
Metadata:
major_brand : isom
minor_version : 0
compatible_brands: isom3gp4
creation_time : 2013-08-27 21:25:13
Duration: 00:00:21.33, start: 0.000000, bitrate: 16820 kb/s
Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 17011 kb/s, 30 fps, 30 tbr, 90k tbn, 180k tbc
Metadata:
rotate : 90
creation_time : 2013-08-27 21:25:13
handler_name : VideoHandle
Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 121 kb/s
Metadata:
creation_time : 2013-08-27 21:25:13
handler_name : SoundHandle
The following attempts did not get the rotation value to copy over:
./ffmpeg -y -i input_file.mp4 -vcodec copy -acodec copy -bsf h264_mp4toannexb output_file.ts
./ffmpeg -y -i input_file.mp4 -vcodec copy -acodec copy -bsf h264_mp4toannexb -metadata rotate=90 output_file.ts
//------ even manually specifying the metadata as the first video stream does not work: ------//
./ffmpeg -y -i input_file.mp4 -vcodec copy -acodec copy -bsf h264_mp4toannexb -metadata:s:v:0 rotate=90 output_file.ts
Even though you can manually inject metadata like:
./ffmpeg -y -i input_file.mp4 -vcodec copy -acodec copy -bsf h264_mp4toannexb -metadata TITLE='Test Title' output_file.ts
This does work, but requires re-encoding, which is really inefficient compared to video and audio codec copying above.
(1 == 90 degree CW rotation)
./ffmpeg -y -i input_file.mp4 -vf "transpose=1" output_file.ts
Related
I want to trim a transparent webm video using ffmpeg. Here's the ffprobe result for that video:
Input #0, matroska,webm, from 'template.webm':
Metadata:
ENCODER : Lavf58.29.100
Duration: 00:00:05.24, start: -0.002000, bitrate: 2856 kb/s
Stream #0:0: Video: vp8, yuv420p(progressive), 1573x900, SAR 1:1 DAR 1573:900, 30 fps, 30 tbr, 1k tbn, 1k tbc (default)
Metadata:
ALPHA_MODE : 1
ENCODER : Lavc58.54.100 libvpx
DURATION : 00:00:05.240000000
Stream #0:1: Audio: opus, 48000 Hz, mono, fltp
Metadata:
ENCODER : Lavc58.54.100 libopus
DURATION : 00:00:05.241000000
I tried
ffmpeg -i template.webm -ss 1 -to 3 -c copy trimmed.webm
but the trimmed video doesn't start (or sometimes end) at the exact times defined in the command so I tried re-encoding the video using libvpx
ffmpeg -i template.webm -ss 1 -to 3 -c:v libvpx -c:a copy -crf 30 -b:v 0 trimmed.webm
It solved the timing issue but this results in loss of transparency of output video. Here's the ffprobe:
Input #0, matroska,webm, from 'trimmed.webm':
Metadata:
ENCODER : Lavf57.83.100
Duration: 00:00:02.00, start: -0.001000, bitrate: 1395 kb/s
Stream #0:0: Video: vp8, yuv420p(progressive), 1573x900, SAR 1:1 DAR 1573:900, 30 fps, 30 tbr, 1k tbn, 1k tbc (default)
Metadata:
ALPHA_MODE : 1
ENCODER : Lavc57.107.100 libvpx
DURATION : 00:00:02.000000000
Stream #0:1: Audio: opus, 48000 Hz, mono, fltp
Metadata:
ENCODER : Lavc58.54.100 libopus
DURATION : 00:00:02.001000000
How should I trim the video while preserving the transparency? Moreover, a fast solution will be extremely helpful.
The native, built-in FFmpeg VP8 decoder does not yet support alpha/transparency. Use libvpx to decode:
ffmpeg -c:v libvpx -i template.webm -ss 1 -to 3 -c:v libvpx -c:a copy -crf 30 -b:v 0 trimmed.webm
If you get Transparency encoding with auto_alt_ref does not work error then add the -auto-alt-ref 0 output option or change -c:v libvpx output option to -c:v libvpx-vp9.
i have .ts file looks like the following
Input #0, mpegts, from 'i.ts':
Duration: 00:00:36.32, start: 28752.398067, bitrate: 57694 kb/s
Program 50
Metadata:
service_name : aaa HD
service_provider:
Stream #0:51[0x1f5]: Video: h264 ([27][0][0][0] / 0x001B), none, 90k tbr, 90k tbn, 180k tbc
Stream #0:52[0x1f6]: Audio: mp3 ([3][0][0][0] / 0x0003), 0 channels, fltp
Program 51
Metadata:
service_name : b Music HD
service_provider:
Stream #0:16[0x1ff]: Video: h264 ([27][0][0][0] / 0x001B), none, 90k tbr, 90k tbn, 180k tbc
Stream #0:17[0x200]: Audio: mp3 ([3][0][0][0] / 0x0003), 0 channels, fltp
Program 52
Metadata:
service_name : c ch HD
service_provider:
Stream #0:14[0x209]: Video: h264 ([27][0][0][0] / 0x001B), none, 90k tbr, 90k tbn, 180k tbc
Stream #0:15[0x20a]: Audio: mp3 ([3][0][0][0] / 0x0003), 0 channels, fltp
Program 1510
Metadata:
service_name : asd
service_provider: xyz
Stream #0:18[0x5e7]: Video: h264 ([27][0][0][0] / 0x001B), none, 90k tbr, 90k tbn, 180k tbc
Stream #0:19[0x5e8]: Audio: mp3 ([3][0][0][0] / 0x0003), 0 channels, fltp
i need to extract one video stream and its audio stream from this file for example related to program 50 i tried
ffmpeg -i i.ts -map 0:51 output.mp4
but i got this error
Stream mapping:
Stream #0:51 -> #0:0 (h264 (native) -> h264 (libx264))
Press [q] to stop, [?] for help
Cannot determine format of input stream 0:51 after EOF
Error marking filters as finished
Conversion failed!
I found a solution here
For mapping the whole programs, the syntax is:
ffmpeg -i i.ts -c:v copy -c:a copy -map 0:p:51 output.mp4
I can't verify that solution is actually working with your .ts file.
I created the following sample, that builds a .ts file with two programs, and then extracts each program to .mp4 file:
ffmpeg -y -r 10 -f lavfi -i testsrc=rate=10:size=160x120 -f lavfi -i sine=frequency=1000 -t 5 -c:v libx264 -c:a aac in1.mp4
ffmpeg -y -r 10 -f lavfi -i mandelbrot=rate=10:size=160x120 -f lavfi -i sine=frequency=300 -t 5 -c:v libx264 -c:a aac in2.mp4
ffmpeg -y -i in1.mp4 -i in2.mp4 -map 0:0 -map 0:1 -map 1:0 -map 1:1 -program title=ProgOne:st=0:st=1 -program title=ProgTwo:st=2:st=3 -c:v copy -c:a copy in.ts
ffmpeg -y -i in.ts -c:v copy -c:a copy -map 0:p:1 output1.mp4
ffmpeg -y -i in.ts -c:v copy -c:a copy -map 0:p:2 output2.mp4
1st command builds video test pattern with high frequency beep (output: in1.mp4).
2nd command builds video Mandelbrot pattern with low frequency beep (output: in2.mp4).
3rd command builds transport stream with two programs (output: in.ts).
4th command extracts first program (output: output1.mp4).
5th command extracts second program (output: output2.mp4).
I need to concatenate mp4 files from different sources, this means some of the variables are out of my control such as timebase, aspect ratio and encoding. So to get around this I re-encode and attempt to standardise the files before concatenating them. Unfortunately, despite this I get Non-monotonous DTS in output stream warnings during the concatenation stage, and the output video seems to always have broken audio/video syncing by the last segment.
I know there are a lot of other questions out there about resolving the warning above, but I've been through them all and reviewed the documentation.. but unfortunately I've been still been unable to solve it..
I think the thing which I don't understand is: if I have mp4s from different sources, what exactly do I need to do to ensure that the files will always neatly concatenate together?
What I've tried so far
The script I'm using to standardise the mp4 files before concantenation is the following (amends resolution, frame rate, timebase, bitrate for audio, bitrate for video, audio encoding and video encoding):
ffmpeg -y -i $1 -vf 'scale=1280:720:force_original_aspect_ratio=1,pad=1280:720:(ow-iw)/2:(oh-ih)/2' -r 30 -video_track_timescale 90000 -b:a 128K -b:v 1200K -c:a aac -c:v libx264 $2
Here's the ffprobe output on two of the files, there are some differences but I'm not sure if they are significant?
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'intro.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.12.100
Duration: 00:00:08.98, start: 0.000000, bitrate: 1210 kb/s
Stream #0:0(eng): Video: h264 (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 1069 kb/s, 30 fps, 30 tbr, 90k tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 132 kb/s (default)
Metadata:
handler_name : SoundHandler
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'middle.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.12.100
Duration: 00:00:59.72, start: 0.000000, bitrate: 1200 kb/s
Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 1063 kb/s, 30 fps, 30 tbr, 90k tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
They all have normal video and audio at this point.
After that I concatenate them and add a watermark using the following (it sucks that I need to re-encode here):
ffmpeg -y \
-f concat \
-safe 0 \
-i $INFILES \
-c:v libx264 \
-c:a copy \
-preset fast \
-vf drawtext=enable="'between(t, $DRAW_TEXT_DELAY, $DRAW_TEXT_DURATION)': fontfile=$FONT_DIR/$FONT: text='$TEXT': fontcolor=$FONTCOLOR: fontsize=$FONTSIZE: $POSITION" \
$OUTFILE
INFILES is a path to a text file formatted like:
file /usr/src/app/data/test/out/intro.mp4
file /usr/src/app/data/test/out/middle.mp4
file /usr/src/app/data/test/out/outro.mp4
What am I missing here? Is there a way to debug this further?
Your audio streams have distinct sampling rates, and may have distinct channel count as well. Also, compressed MPEG audio streams will introduce slight async upon concat.
Use
ffmpeg -y -i $1 -vf 'scale=1280:720:force_original_aspect_ratio=1,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,format=yuv420p' -r 30 -c:v libx264 -b:v 1200K -ac 2 -ar 48000 -c:a pcm_s16le -video_track_timescale 90000 $2
to standardize, but save to MOV.
Then during concat, change -c:a copy to -c:a aac.
There are three methods to concatenate files in FFmpeg.
Demuxer (You are using this)
This method can be used to concat files with the same paramters, like codecs, size, PAR, etc.
$ ffmpeg -concat -i files.txt [...] output.mp4
Protocol
Same as the first one, but on top of that, this method is useful for files that can be copied together bitwise - it doesn't involves re-encoding (some formats support this, like MpegTS or some lossless formats).
$ ffmpeg -i "concat:FILE_0| ... |FILE_N" [...] output.mp4
Filter
If you have videos with different codecs, you have to use this method:
$ ffmpeg -i <FILE_0> ... -i <FILE_N> [...] -filter_complex "[0:0][0:1]...[<N>:0][<N>:1] concat=n=<N>:v=1:a=1[v_out][a_out]" -map [v_out] -map [a_out] output.mp4
The concat filter decodes the video and reencodes it with the same parameters. It also takes care of the audio streams. I'm not entirely sure what does it do if the resolutions are different, but this should be a good start.
I have a mp4 video that I need to convert to mpg (for windows PowerPoint2010)
I have been trying to get best quality. But I keep getting error:
[mpeg # 0x2523620] buffer underflow st=0 bufi=420177 size=445860
[mpeg # 0x2523620] packet too large, ignoring buffer limits to mux it
[mpeg # 0x2523620] buffer underflow st=0 bufi=420177 size=445860
[mpeg # 0x2523620] buffer underflow st=0 bufi=422218 size=445860
Could someone help me with the syntax for best quality ouput to mpg. Here is the output of the mp4 file:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp41isom
creation_time : 2016-06-10 11:15:06
Duration: 00:04:20.86, start: 0.000000, bitrate: 18677 kb/s
Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1920x1080, 18541 kb/s, 29.97 fps, 29.97 tbr, 29970 tbn, 59.94 tbc (default)
Metadata:
creation_time : 2016-06-10 11:15:06
handler_name : VideoHandler
encoder : AVC Coding
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 132 kb/s (default)
Metadata:
creation_time : 2016-06-10 11:15:06
handler_name : SoundHandler
I have tried the following but keep getting that error:
ffmpeg -i video.mp4 -c:v libx264 -c:a copy -qp 5 video.mpg
ffmpeg -i video.mp4 -c:v libx264 -c:a copy -qscale:v 1 video.mpg
ffmpeg -i video.mp4 -c:v libx264 -crf 0 -c:a copy -bf 2 -flags qprd -flags mv0 video.mpg
ffmpeg -i video.mp4 -c:v libx264 -crf 0 -c:a copy video.mpg
ffmpeg -i video.mp4 -c:v libx264 -preset slow -crf 5 -c:a copy video.mpg
ffmpeg -i video.mp4 -c:v libx264 -preset slow -crf 5 -c:a copy -maxrate 11000k video.mpg
ffmpeg -i video.mp4 -c:v libx264 -preset slow -crf 5 -c:a copy -maxrate 5000 -bufsize 11000 video.mpg
Thanks,
Doesn't look like PP2010 supports H.264. (If it did, you could skip re-encoding altogether).
Try
ffmpeg -i video.mp4 -c:v mpeg2video -q:v 5 -c:a mp2 -f vob video.mpg
This will produce a MPEG-2 Program Stream container with MPEG-2 video and MP2 audio.
I have some videos either in mp4 or webm format, and I'd like to use ffmpeg to add 4 seconds to the start of each video to display some text in the center with no sound.
Some other requirements:
try to avoid re-encoding the video
need to maintain the quality (resolution, bitrate, etc)
(optional) to make the text fade in/out
I am new to ffmpeg and any help will be appreciated.
thanks in advance
Example ffprobe information for mp4 below:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf55.33.100
Duration: 00:00:03.84, start: 0.042667, bitrate: 1117 kb/s
Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 1021 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 140 kb/s (default)
Metadata:
handler_name : SoundHandler
Example webm
Input #0, matroska,webm, from 'input.webm':
Metadata:
encoder : Lavf55.33.100
Duration: 00:00:03.80, start: 0.000000, bitrate: 1060 kb/s
Stream #0:0(eng): Video: vp8, yuv420p, 1280x720, SAR 1:1 DAR 16:9, 30 fps, 30 tbr, 1k tbn, 1k tbc (default)
Stream #0:1(eng): Audio: vorbis, 48000 Hz, stereo, fltp (default)
Screenshot from joined.mp4
Screenshot for step 3 console
You'll have to generate a 4 second video with dummy audio matching the parameters of the existing video, including timebase, and then use the concat demuxer with streamcopy.
For the sample files shown in Q:
Step 1 Generate text video
ffmpeg -f lavfi -r 30 -i color=black:1280x720 -f lavfi -i anullsrc -vf "drawtext=fontfile='/path/to/font.ttf':fontcolor=FFFFFF:fontsize=50:text='Your text':x='(main_w-text_w)/2':y='(main_h-text_h)/2',fade=t=in:st=0:d=1,fade=t=out:st=3:d=1" -c:v libx264 -b:v 1000k -pix_fmt yuv420p -video_track_timescale 15360 -c:a aac -ar 48000 -ac 2 -sample_fmt fltp -t 4 intro.mp4
For WebM, replace -c:v libx264 with -c:v libvpx, -c:a aac with -c:a libvorbis and intro.mp4 with intro.webm. You may remove the -video_track_timescale 15360 since WebMs tend to use a single timescale, that I've seen.
Step 2 Prepare concat file, say, list.txt
file 'intro.mp4'
file 'input.mp4'
Step 3 Concat
ffmpeg -f concat -i list.txt -c copy -fflags +genpts joined.mp4
The variables important here are video size 1280x720, frame rate -r 30, -pix_fmt yuv420p, sample rate -ar 48000, format -sample_fmt fltp, channel layout -ac 2 and of course, codecs.
Short answer is that you cannot encode new data as mp4 or webm and insert it at the front of the video stream. Those formats simply do not work like that. Both of these encoding formats are lossy, so if you decode and encode them again then additional information will be lost/changed by the second encoding. You could do something else, but what you are trying to do will not work.