Convert a video to MP4 (H.264/AAC) with ffmpeg - linux

If I don't make a mistake, Safari currently need MP4 (H.264/AAC) video encoded for the HTML5 <video> element.
So I tried to convert a video to this format with ffmpeg. However when I enter the shell command ffmpeg -i video.flv video.mp4, the returned error is :
Seems stream 0 codec frame rate
differs from container frame rate:
2000.00 (2000/1) -> 29.92 (359/12) Input #0, flv, from 'video.flv':
Duration: 00:05:01.20, start:
0.000000, bitrate: 66 kb/s
Stream #0.0: Video: h264, yuv420p, 320x240 [PAR 1:1 DAR 4:3], 66 kb/s,
29.92 tbr, 1k tbn, 2k tbc
Stream #0.1: Audio: aac, 22050 Hz, stereo, s16 Output #0, mp4, to
'video.mp4':
Stream #0.0: Video: mpeg4, yuv420p, 320x240 [PAR 1:1 DAR 4:3],
q=2-31, 200 kb/s, 90k tbn, 29.92 tbc
Stream #0.1: Audio: 0x0000, 22050 Hz, stereo, s16, 64 kb/s Stream
mapping: Stream #0.0 -> #0.0
Stream #0.1 -> #0.1 Unsupported codec
for output stream #0.1
An AAC codec is required but I'm quite newbie with ubuntu and I dont really now how to fix this problem. I'm using Ubuntu 9.10 Karmik Koala (for amd64).
Thank you very much. :)

http://handbrake.fr is a nice high level tool with a lot of useful presets for mp4 for iPod, PS3, ... with both GUI and CLI interfaces for Linux, Windows and Mac OS X.
It comes with its own dependencies as a single statically linked fat binary so you have all the x264 / aac codecs included.
$ HandBrakeCLI -Z Universal -i myinputfile.mov -o myoutputfile.mp4
To list all the available presets:
$ HandBrakeCLI -z

Software patents led Debian/Ubuntu to disable the H.264 and AAC encoders in ffmpeg. See /usr/share/doc/ffmpeg/README.Debian.gz.
So go install x264, mplayer/mencoder, and Nero's AAC encoder. (Or, if you want to use all Free software, and don't care so much about audio quality, then sudo aptitude install faac.)
I don't remember if the medibuntu package of mencoder includes x264 vid encoding, since I build my own from git x264 and svn mplayer sources. (x264 is very actively developed, with significant quality and speed improvements frequently added.)
http://git.videolan.org/?p=x264.git;a=summary
x264 is also packaged, but you should check that it's up to date enough to include weightp with recent bugfixes, and even more recent speed improvements...
Or if you're already willing to convert from .flv, instead of going from the high-quality source the flv was made from, then probably whatever recent version of x264 you can find will be fine.

You're trying to convert a (rather rare) .flv file that (already) contains H.264 video and AAC audio.
Formatting your console's output as FFmpeg brings out these details.
Input #0, flv, from 'video.flv':
Duration: 00:05:01.20, start: 0.000000, bitrate: 66 kb/s
Stream #0.0: Video: h264, yuv420p, 320x240 [PAR 1:1 DAR 4:3], 66 kb/s, 29.92 tbr, 1k tbn, 2k tbc
Stream #0.1: Audio: aac, 22050 Hz, stereo, s16
The original flv is converted to an .mp4 file with H.264 video and AAC audio (just like the original .flv):
Output #0, mp4, to 'video.mp4':
Stream #0.0: Video: mpeg4, yuv420p, 320x240 [PAR 1:1 DAR 4:3], q=2-31, 200 kb/s, 90k tbn, 29.92 tbc
Stream #0.1: Audio: 0x0000, 22050 Hz, stereo, s16, 64 kb/s
Because the audio and video data in the .flv are already in the format/codecs you need for the .mp4, you can just copy everything to the new .mp4 container. This process will be massively faster than decoding and reencoding everything:
ffmpeg -i video.flv -vcodec copy -acodec copy video.mp4
or more simply:
ffmpeg -i video.flv -codec copy video.mp4
##The real error you're getting is:##
Unsupported codec for output stream #0.1
Which means FFmpeg can't convert audio (stream #0.1) to AAC.
You can skip the error by:
copying the audio data since it's already AAC encoded (use the copy command above)
or you can solve the error by:
using a FFmpeg build with AAC decode/encode support. FFmpeg currently supports 4 AAC libraries (see FFmpeg and AAC Encoding Guide).
For more details you should also read Converting FLV to MP4 With FFmpeg The Ultimate Guide

You need to recompile ffmpeg (from source) so that it supports x264. If you follow the instructions in this page, then you will be able to peform any kind of conversion you want.

You can also try adding the Motumedia PPA to your apt sources and update your ffmpeg packages.

Had this problem recently with converting nasty WMV into Final Cut Pro X for editing. Flow player can do it but it leaves a water mark, so I fiddled a bit with ffmpeg till I got something going.
First install ffmpeg - I used
brew install ffmpeg
Obviously you need brew installed first, google that bit.
Next I wrote a simple command line script with the following content - you can substitute the $1 for an input / output file or just create a shell script file...
vi convert.sh
Paste.
echo "Pass one"
ffmpeg -y -i "$1" -c:v libx264 -preset medium -b:v 1555k -pass 1 -c:a libfaac -b:a 256k -f mp4 /dev/null &&
echo "Pass two"
ffmpeg -i "$1" -c:v libx264 -preset medium -b:v 1555k -pass 2 -c:a libfaac -b:a 256k "$1.mp4"
Then to convert your video...
sh convert.sh myvideofile.wmv
If all went well you should see a new file called myvideofile.wmv.mp4.
Hope that works for you.

You need to compile ffmpeg with an AAC encoder. You can find one at AudioCoding.

Try This one:: Libav in Linux
Installation: run command
sudo apt-get install libav-tools
Video conversion command::Go to folder contains the video and run in terminal
avconv -i oldvideo.flv -ar 22050 convertedvideo.mp4

Related

Use ffmpeg to downscale, tonemap, downmix and re encode mkv

I try to use ffmpeg to downscale a 4k and tonemap a 4k HDR mkv to a 1080p SDR mkv with this code:
ffmpeg -i "Input.mkv" -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p -c:v hevc_nvenc -b:v 12M -preset slow "Output.mkv"
The problem is, that only the first audio track (of four) is copied to the new mkv and the subtitle tracks are missing:
Input:
Stream #0:0: Video: hevc (Main 10), yuv420p10le(tv, bt2020nc/bt2020/smpte2084), 3840x2160 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 23.98 tbc (default)
Stream #0:1(ger): Audio: eac3, 48000 Hz, 7.1, fltp (default)
Stream #0:2(ger): Audio: dts (DTS-HD MA), 48000 Hz, 7.1, s16p
Stream #0:3(eng): Audio: truehd, 48000 Hz, 7.1, s32 (24 bit)
Stream #0:4(eng): Audio: ac3, 48000 Hz, 5.1(side), fltp, 640 kb/s
Stream #0:5(ger): Subtitle: subrip (default) (forced)
Stream #0:6(ger): Subtitle: dvd_subtitle, 1920x1080 (forced)
Stream #0:7(ger): Subtitle: hdmv_pgs_subtitle, 1920x1080 (forced)
Stream #0:8(ger): Subtitle: dvd_subtitle, 1920x1080
Stream #0:9(ger): Subtitle: hdmv_pgs_subtitle
Stream #0:10(eng): Subtitle: dvd_subtitle, 1920x1080
Stream #0:11(eng): Subtitle: hdmv_pgs_subtitle
Output:
Stream #0:0: Video: hevc (Main), yuv420p(tv), 3840x2160 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 23.98 tbc (default)
Stream #0:1(ger): Audio: vorbis, 48000 Hz, 7.1, fltp (default)
Stream #0:2(ger): Subtitle: ass (default) (forced)
I would like to have an mkv as output converts all the audio tracks to aac Stereo (but keeps all four of them), copies the subtitle tracks and tonemaps/downscales the video track.
I have tried to use the -map 0 or -map 0:a:0 -map 0:a:1 ... commands (and some similiar to those, however I seemingly end up with either one audio track, no video track or a video track that is just copied.
I possible, I would also like to use nvenc with a high quality preset, which is the reason for -c:v hevc_nvenc -b:v 12M -preset slow in my command, however I have no idea, if this is done right, since the ouput mkv also as a 4k video track instead of a 1080p. Maybe this is caused by the -c:v because it overrides -vf? Sorry, I am feeling dumb and am just getting started with ffmpeg.
There is so much information about ffmpeg out there, but it is either too complicated for me, or not answering my questions.
Thanks for your help!
ffmpeg -i "Input.mkv" -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p -map 0 -c:v hevc_nvenc -b:v 12M -preset slow -c:s copy -ac 2 "Output.mkv"
Add -map 0 to include all streams (default behavior only chooses 1 stream per type). See FFmpeg Wiki: Map.
-ac 2 for stereo audio.
-c:s copy to stream copy subtitles. Video and audio are being filtered, so they can't be stream copied and must be re-encoded.
I'm not a user of NVENC so I can't comment on that.

Extract audio with ffmpeg, linux

I'm trying to extract audio tracks from some Avi videos and save them to their own files, ideally without re-encoding.
I've had a look through here https://www.ffmpeg.org/ffmpeg.html#Audio-Options and here ffmpeg to extract audio from video though I'm getting errors regardless of the approach I try.
My latest command string is:
ffmpeg -i /home/d/Pictures/Test/input-video.AVI -map 0:a -vn -acodec copy /home/d/Pictures/Test/output-audio.m4a
The key part of the output is:
Guessed Channel Layout for Input Stream #0.1 : mono
Input #0, avi, from '/home/d/Pictures/Test/input-video.AVI':
Duration: 00:00:05.94, start: 0.000000, bitrate: 18131 kb/s
Stream #0:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj422p(pc, bt470bg/unknown/unknown), 1280x720, 17995 kb/s, 30.28 fps, 30.28 tbr, 30.28 tbn, 30.28 tbc
Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 11025 Hz, 1 channels, s16, 176 kb/s
File '/home/d/Pictures/Test/output-audio.m4a' already exists. Overwrite ? [y/N] y
[ipod # 0x1d89520] Codec for stream 0 does not use global headers but container format requires global headers
[ipod # 0x1d89520] Could not find tag for codec pcm_s16le in stream #0, codec not currently supported in container
Output #0, ipod, to '/home/d/Pictures/Test/output-audio.m4a':
Metadata:
encoder : Lavf56.40.101
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 11025 Hz, mono, 176 kb/s
Stream mapping:
Stream #0:1 -> #0:0 (copy)
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
I'm believe I have got the right audio stream number from this output and thus am assuming the "-map 0:a" part isn't the problem.
I'm running on Linux Mint 18.1
MP4 family of formats don't store PCM audio, so you either have to re-encode or save to another format, like Matroska.
ffmpeg -i video.AVI -map 0:a -vn -acodec copy audio.mka

FFmpeg not copying all audio streams

I'm having trouble getting ffmpeg to copy all audio streams from a .mp4 file. After hours of searching online, it appears this should copy all streams (as shown in example 4 here):
ffmpeg -i in.mp4 -map 0 -c copy out.mp4
in.mp4 contains 3 streams:
Video
Audio track 1
Audio track 2
out.mp4 (which should be identical to in.mp4) contains only 2 streams:
Video
Audio track 1
FFmpeg does appear to correctly identify all 3 streams, but doesn't copy all of them over. Output from FFmpeg:
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Stream #0:2 -> #0:2 (copy)
Edit: Output from ffmpeg -v 9 -loglevel 99 -i in.mp4:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from in.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.36.100
Duration: 00:00:06.03, start: 0.000000, bitrate: 5582 kb/s
Stream #0:0(und), 1, 1/15360: Video: h264 (Main), 1 reference frame (avc1 /
0x31637661), yuv420p(tv, bt470bg/unknown/unknown, left), 1920x1080 (0x0) [SAR 1:
1 DAR 16:9], 0/1, 5317 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und), 1, 1/48000: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz,
stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream #0:2(und), 1, 1/48000: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz,
stereo, fltp, 128 kb/s
Metadata:
handler_name : SoundHandler
Successfully opened the file.
At least one output file must be specified
[AVIOContext # 0000000001c2b9e0] Statistics: 153350 bytes read, 2 seeks
Edit 2 (solved): I managed to find the correct syntax from this ticket. For any others that are interested, the correct syntax is:
ffmpeg -i in.mp4 -vcodec copy -c:a copy -map 0 out.mp4
This will copy all streams.
FFmpeg have option to map all streams to output, you have to use option -map 0 to map all streams from input to output.
In full line it might look like:
ffmpeg -i in.mp4 -c copy -map 0 out.mp4
For more info see the documentation on stream selection and the -map option.
Apparently this is a popular question, so I'm posting my solution as an answer (was previously a comment reply) so that others can see.
I managed to find the correct syntax from this ticket. The correct syntax is:
ffmpeg -i in.mp4 -vcodec copy -c:a copy -map 0:0 -map 0:1 -map 0:2 out.mp4
This will copy all 3 streams.
OK, I read pretty deep into the ffmpeg man page and found this which should be useful:
Note that currently each output stream can only contain channels from
a single input stream; you can't for example use "-map_channel" to
pick multiple input audio channels contained in different streams
(from the same or different files) and merge them into a single output
stream. It is therefore not currently possible, for example, to turn
two separate mono streams into a single stereo stream. However
splitting a stereo stream into two single channel mono streams is
possible.
If you need this feature, a possible workaround is to use the amerge
filter. For example, if you need to merge a media (here input.mkv)
with 2 mono audio streams into one single stereo channel audio stream
(and keep the video stream), you can use the following command:
ffmpeg -i input.mkv -filter_complex "[0:1] [0:2] amerge" -c:a pcm_s16le -c:v copy output.mkv
You may want to read through and experiment with the man page instructions on man ffmpeg-filters to understand just what level of complexity you're getting into for naming channels and expected output.
[Edit: As Mulvya noted, this answers a question, but it was not quite the original poster's question.]
First I tried this broader answer here: https://stackoverflow.com/a/54616353/1422630
But I had trouble with a not supported subtitle track so I ended having to use this command:
avconv -i INFILE -c copy -map 0:a -map 0:v OUTFILE
I understand that, after I asked to copy, it basically copied only what I mapped (and it mapped all audio of course), as I don't care for the subtitles being embedded at all. If you want to map the subtitles, just add this -map 0:s.
It seems that specific ffmpeg versions ignore -c copy option and skip audio stream copy, thus resulting in final file with no audio, e.g. does not copy audio tracks and produce video with no sound.
The ffmpeg affected is for example used on Synology Disk Station devices:
ffmpeg version 2.7.7 Copyright (c) 2000-2015 the FFmpeg developers
built with gcc 4.9.3 (crosstool-NG 1.20.0) 20150311 (prerelease)
To resolve that, without analyzing file structure and manually mapping all audio streams with -map 0:1 -map 0:2 etc, I found very simple command to process it automatically:
ffmpeg -i INFILE -map 0 -c copy -c:a copy OUTFILE
This is different from -c:v -c:a as preserves chapters and subtitles together with video and all audio tracks with different languages, like english, spanish, french, russian or chineese.
Also in case you have more hardly broken file, which simple copy does not fix, please try this command, which potentially fix more errors, which could crash video player, or stuck video or audio:
ffmpeg -err_detect ignore_err -i INFILE -map 0 -c copy -c:a copy OUTFILE

FFMPEG convert FLV to MP4 and reduce filesize

i have a system that is recording live stream via Wowza. I get from Wowza a .flv file with the record. The problem is, 5 minutes file is near to 50mb big. But when i look at some TV-Series that you can download from the net, they are 20 minutes, in mp4 and just like 150mb big. Whatever... Look, the stream input is like:
Input #0, flv, from 'rtmp://127.0.0.1/stream/test.stream':
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0.0: Video: h264 (Constrained Baseline), yuv420p, 720x404 [PAR 1:1 DAR 180:101], 25 tbr, 1k tbn, 50 tbc
Stream #0.1: Audio: aac, 48000 Hz, stereo, s16
FFmpeg shows me something like this at the "session":
frame= 2721 fps= 27 q=-1.0 Lsize= 17188kB time=111.21 bitrate=1266.1kbits/s
Have you any idea how to use FFmpeg to convert the .FLV file in MP4 in same near to Quality but smaller filesize?
btw. i'm operating in linux ^^
Thanks
This is based on a linux mashine using the latest ffmpeg
using the following code and changing only the -crf value you are able to create various video clip sizes.lower number is higher quality.
The final video is compatible with all modern devices/services like native web players, ios devices , android devices, microsoft devices, ps3, xbox and more.
ffmpeg
-y //overwrite the file if it exists
-i INPUTFILE // replace with the input file
-metadata title=THETITLE // set a nice title, visible on modern devices
-metadata date=THEDATE // set a nice title, visible on modern devices
-c:v libx264 // use the h264 codec
-crf 21 // try different numbers between 18-26
-preset veryslow // placebo,slow,fast,ultrafast==big file
-tune film // tune it a little
-pix_fmt yuv420p // preferred on most modern devices
-profile:v main // preferred on most modern devices
-level 3.1 // preferred on most modern devices
-refs 4 // preferred on most modern devices
-c:a libfdk_aac // use aac
-metadata:s:a language=eng // set a language, visible on modern devices
-b:a 128k // audio bitrate 128k is like mp3 192k
-ar 48000 // 44100 ... whatever
-ac 2 // audiochannels
-movflags +faststart //move the metadata in the front of the video so it loads faster
OUTPUTFILE

RTMP: Is there such a linux command line tool?

I have looked everywhere to find a linux utility that will allow me to download rtmp streams. Not flv video but MP3 streams. The location of the streams I want to download are in this format.
rtmp://live.site.com/loc/45/std_fc74a6b7f79c70a5f60.mp3
Anyone know of such a command line tool? Or even anything close to what I am asking for?
I do not want full software applications and it would be great if it worked on Linux via Shell or something.
Thanks all
One of the following should do, if you have mplayer or vlc compiled with RTMP access.
mplayer -dumpstream rtmp://live.site.com/loc/45/std_fc74a6b7f79c70a5f60.mp3
This will generate a ./stream.dump.
vlc -I dummy rtmp://live.site.com/loc/45/std_fc74a6b7f79c70a5f60.mp3 \
--sout file/ts:output.mpg vlc://quit
This will generate a ./output.mpg. You'll have to demux it to extract just the audio stream out.
This question is old but this can help to another users with this doubt.
To download directly, without any conversion, there is two options (the author of both programs is the same and the behavior is the same):
RTMPDump. Example: rtmpdump -r "rtmp://host.com/dir/file.flv" -o filename.flv
flvstreamer. Example: flvstreamer -r "rtmp://od.flash.plus.es/ondemand/14314/plus/plustv/PO770632.flv" -o salida.flv
And if you want download and convert the video at same time, the best way is use ffmpeg:
ffmpeg -i rtmp://server/live/streamName -acodec copy -vcodec copy dump.mp4
I think the landscape has changed a bit since the time of some of the previous answers. At least according to the rtmp wikipedia page. It would appear that the rtmp protocol specification is open for public use. To that end you can use 2 tools to accomplish what the original poster was asking, rtmpdump and ffmpeg. Here's what I did to download a rtmp stream that was sending an audio podcast.
step #1 - download the stream
I used the tool rtmpdump to accomplish this. Like so:
% rtmpdump -r rtmp://url/to/some/file.mp3 -o /path/to/file.flv
RTMPDump v2.3
(c) 2010 Andrej Stepanchuk, Howard Chu, The Flvstreamer Team; license: GPL
Connecting ...
INFO: Connected...
Starting download at: 0.000 kB
28358.553 kB / 3561.61 sec
Download complete
step #2 - convert the flv file to mp3
OK, so now you've got a local copy of the stream, file.flv. You can use ffmpeg to interrogate the file further and also to extract just the audio portion.
% ffmpeg -i file.flv
....
[flv # 0x25f6670]max_analyze_duration reached
[flv # 0x25f6670]Estimating duration from bitrate, this may be inaccurate
Input #0, flv, from 'file.flv':
Duration: 00:59:21.61, start: 0.000000, bitrate: 64 kb/s
Stream #0.0: Audio: mp3, 44100 Hz, 1 channels, s16, 64 kb/s
From the above output we can see that the file.flv contains a single stream, just audio, and it's in mp3 format, and it's a single channel. To extract it to a proper mp3 file you can use ffmpeg again:
% ffmpeg -i file.flv -vn -acodec copy file.mp3
....
[flv # 0x22a6670]max_analyze_duration reached
[flv # 0x22a6670]Estimating duration from bitrate, this may be inaccurate
Input #0, flv, from 'file.flv':
Duration: 00:59:21.61, start: 0.000000, bitrate: 64 kb/s
Stream #0.0: Audio: mp3, 44100 Hz, 1 channels, s16, 64 kb/s
Output #0, mp3, to 'file.mp3':
Metadata:
TSSE : Lavf52.64.2
Stream #0.0: Audio: libmp3lame, 44100 Hz, 1 channels, 64 kb/s
Stream mapping:
Stream #0.0 -> #0.0
Press [q] to stop encoding
size= 27826kB time=3561.66 bitrate= 64.0kbits/s
video:0kB audio:27826kB global headers:0kB muxing overhead 0.000116%
The above command will copy the audio stream into a file, file.mp3. You could also have extracted it to a wav file like so:
ffmpeg -i file.flv -vn -acodec pcm_s16le -ar 44100 -ac 2 file.wav
This page was useful in determining how to convert the flv file to other formats.

Resources