2

I'm not familiar with ffmpeg, but came across this script that takes in the file and creates an output with eac3 audio.

#!/bin/sh

echo "Dolby Digital Plus Muxer"
echo "Developed by @kdcloudy, not affiliated with Dolby Laboratories"
echo "Enter the file name to be converted: "
read filepath
if [! -d $filepath]
then
exit $err
fi

ffmpeg -i $filepath -vn ddp.eac3
ffmpeg -i $filepath -i ddp.eac3 -vcodec copy -c:a eac3 -map 0:s -map 0:v:0 -map 1:a:0 output.mp4
rm ddp.eac3

I'd like to know what to modify in this code to ensure all the subtitles are copied from the original file and all the available audio tracks are converted to eac3 and added to the output.mp4 file. For the subtitles copying I tried -map but couldn't get it to work. Thanks for the help!

1 Answers1

5

You only need one ffmpeg command:

ffmpeg -i input.mkv -map 0 -c:v copy -c:a eac3 -c:s copy output.mkv
  • -map 0 Selects all streams. Otherwise only one stream per stream type will be selected. See FFmpeg Wiki: Map for more into on -map.
  • -c:v copy Stream copy all video.
  • -c:a eac3 Encodes all audio to E-AC-3.
  • -c:s copy Stream copy all subtitles.

For compatibility this assumes that the input and output are both Matroska (.mkv).

That script is not great. Here's a cleaner, simpler version (not that I think a script is necessary for this):

#!/bin/bash
# Usage: ./eac3 input.mkv output.mkv

ffmpeg -i "$1" -map 0 -c:v copy -c:a eac3 -c:s copy "$2"

If you want to convert a whole directory see How do you convert an entire directory with ffmpeg?

llogan
  • 121,796
  • 28
  • 232
  • 243
  • it doesn't seem to be working for me. I'm trying to convert mkv files to mp4 w/ ac3. When I ran the script I got `Could not find tag for codec ass in stream #2, codec not currently supported in container Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument Error initializing output stream 0:1 -- ` And when I removed `-c:s copy` I got `Automatic encoder selection failed for output stream #0:2. Default encoder for format mp4 (codec none) is probably disabled. Please choose an encoder manually. Error selecting an encoder for stream 0:2` – Kushal Sai Gunturi Oct 04 '20 at 04:30
  • @KushalSaiGunturi I made two assumptions: 1) you wanted MP4 output format. 2) You wanted to, *"ensure all the subtitles are copied from the original file"*, so I assumed input and output are the same format. MP4 only supports "mov_text" subtitles, so you can't stream copy ASS to MP4. So if you want MP4 change `-c:s copy` to `-c:s mov_text`. Note that support for subtitles in MP4 is not widely supported so your player may not play them. Or output to .mkv instead of .mp4 and the original command will work. – llogan Oct 04 '20 at 20:07
  • I used this command on a MKV file with 1 Video, 4 Audio (all EAC3) and 1 Subtitle Stream :- ffmpeg -i input.mkv -map 0 -c:v copy -c:a mp3 -c:s copy output.mkv I get following error :- [matroska @ 0000011ce929e600] dimensions not set Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument Error initializing output stream 0:4 -- [libmp3lame @ 0000011ce99b4600] 4 frames left in the queue on closing [libmp3lame @ 0000011ce93dc980] 4 frames left in the queue on closing Conversion failed! – maverick Dec 05 '21 at 03:27