3

ffmpeg

ffmpeg version 3.1.1 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-4)
  configuration: --enable-libmfx --enable-nonfree
  libavutil      55. 28.100 / 55. 28.100
  libavcodec     57. 48.101 / 57. 48.101
  libavformat    57. 41.100 / 57. 41.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 47.100 /  6. 47.100
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  1.100 /  2.  1.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

I know to convert from one format to other format using the commands like,

ffmpeg -i M.avi -c:v h264_qsv -look_ahead 0 -strict -2 -preset:v fast M.mp4

Now I m in need of doing conversions like M.avi to M.h264 and then from M.h264 to M.mp4

ffmpeg -i M.avi -c:v h264_qsv -look_ahead 0 -strict -2 -preset:v fast M.h264

ffmpeg -i M.h264 -c:v h264_qsv -look_ahead 0 -strict -2 -preset:v fast M.mp4

While doing this everything worked fine, but M.mp4 is playing without audio.

As per the link,

https://stackoverflow.com/questions/5725958/ffmpeg-conversion-skips-audio

I even tried with -acodec copy as

ffmpeg -i M.avi -c:v h264_qsv -look_ahead 0 -strict -2 -preset:v fast -acodec copy M.h264

ffmpeg -i M.h264 -c:v h264_qsv -look_ahead 0 -strict -2 -preset:v fast -acodec copy M.mp4

while using the command ffmpeg -i M.mp4 -c:v h264_qsv -look_ahead 0 -strict -2 -preset:v fast -acodec copy n.h264, the outcome is

ffmpeg version 3.1.1 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-4)
  configuration: --enable-libmfx --enable-nonfree
  libavutil      55. 28.100 / 55. 28.100
  libavcodec     57. 48.101 / 57. 48.101
  libavformat    57. 41.100 / 57. 41.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 47.100 /  6. 47.100
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  1.100 /  2.  1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'M.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2016-01-31 09:44:22
  Duration: 00:03:28.38, start: 0.000000, bitrate: 2120 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 1925 kb/s, 24 fps, 24 tbr, 24 tbn, 48 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 191 kb/s (default)
    Metadata:
      creation_time   : 2016-01-31 09:44:23
      handler_name    : IsoMedia File Produced by Google, 5-11-2011
File 'n.h264' already exists. Overwrite ? [y/N] y
libva info: VA-API version 0.35.0
libva info: va_getDriverName() returns 0
libva info: User requested driver 'iHD'
libva info: Trying to open /opt/intel/mediasdk/lib64/iHD_drv_video.so
libva info: Found init function __vaDriverInit_0_32
libva info: va_openDriver() returns 0
[h264 @ 0x2fd4940] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead.
Output #0, h264, to 'n.h264':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    encoder         : Lavf57.41.100
    Stream #0:0(und): Video: h264 (h264_qsv), nv12, 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 1000 kb/s, 24 fps, 24 tbn, 24 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc57.48.101 h264_qsv
    Side data:
      cpb: bitrate max/min/avg: 0/0/1000000 buffer size: 0 vbv_delay: -1
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (h264_qsv))
Press [q] to stop, [?] for help
frame= 5001 fps=284 q=-0.0 Lsize=   23221kB time=00:03:28.33 bitrate= 913.1kbits/s speed=11.8x    
video:23221kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%

Still no audio. Can someone help me with this?

Vanns
  • 351

3 Answers3

0

As others have stated, there is no reason to do this in multiple steps, and h264 format cannot hold audio, so that extra step is where you are losing the audio.

You should also note that you are performing full re-encoding of the video on your second step, which is a waste of time and is a lossy operation (you will lose video quality).

To convert to h264+aac all in one step, do something like this:

ffmpeg -i M.avi -c:v h264_qsv -c:a aac out.mp4

(The commands I'm giving are in their most basic form. You should still add in your additional flags for quality, etc.)

To convert in multiple steps, you will need to create separate video and audio files, then combine them into a container. To find out what format the avi is using, just run ffmpeg without any flags or output

ffmpeg -i M.avi

and it will show you the details of all of the channels in the avi container.

If the formats in the avi container are supported by mp4, you could just pull the video and audio into an mp4 without re-encoding (this would avoid losing quality)

ffmpeg -i M.avi -c copy M.mp4

Or you could re-encode them into separate video and audio, then combine

ffmpeg -i M.avi -c:v h264_qsv M.h264

ffmpeg -i M.avi -c:a aac M.aac

ffmpeg -i M.h264 -i M.aac -map 0:v -map 1:a -c copy M.mp4

Notice that you should copy codecs in the final step instead of re-encoding.

blendenzo
  • 952
0

I used the following code to convert my .avi file to .mp4 file. The output as in your requirement contains both audio and video.

ffmpeg -i input_video_with_audio.avi -vcodec libx264 -crf 35 -preset ultrafast -c:a copy output_video_with_audio.mp4

The command is specifying H.264/MPEG-4 (libx264) encoder with -vcodec, -crf for video quality (range is 0 to 63, higher numbers indicating lower quality and smaller output size), -c:a copy the audio encoder is set to copy.

rusty
  • 16,917
0

I'm not aware of a way to extract the Audio and Video in a single step, but you can easily extract both Audio and Video with a short script.

Assuming the audio is AAC as in your post:

#! /bin/bash
###################################################################
i="" # edit with the input avi file name including extension
###################################################################
  ffmpeg -i "$i" -c:v h264_qsv -look_ahead 0 -strict -2 -preset:v fast -an "${i%.*}.".h264
  ffmpeg -i "$i" -vn -acodec copy "${i%.*}.".aac
done

To recombine them in in an MP4 container, you can do:

#! /bin/bash
###################################################################
i="" # edit with the input h264 file name including extension
###################################################################
  ffmpeg -i "$i" -i "${i%.*}.".aac -vcodec copy -acodec copy "${i%.*}.".mp4
done

This is not an ideal solution, as it creates an additional step (as noted in the comments). But at least it would give you a starting point :)

Hope that helps! :)

AnotherKiwiGuy
  • 4,482
  • 1
  • 22
  • 39