1

everybody here! So basically this is what I want to achieve:

I have a muted video about 3 minutes long. I have a list of audio tracks in mp3 format (40 songs in a folder with duration 2 to 6 mins each one)

I want this video to play cycled automatically taking songs from playlist and injecting them to the video one by one. Every time a song finishes the next one from the list should start playing at the moment. Video continues playing and doesn't care duration of tracks.

I consider it as the first step on the way to broadcast radio with a video background on youtube in 24/7 mode with ability to put additional tracks to playlist without need to stop translation.

My problem is that I'm new in FFmpeg and I would appreciate any suggestions regarding which FFMpeg topic to start investigate with in order to achieve my goal

Grrzly
  • 43
  • 7

1 Answers1

7

Use the concat demuxer

You can do live updates to the playlist for the concat demuxer, but each audio file must have the same attributes, the same number of streams, and all be the same format.

  1. Create input.txt containing:

    ffconcat version 1.0
    file 'audio1.mp3'
    file 'audio2.mp3'
    file 'audio3.mp3'
    file 'audio40.mp3'
    

    All file names must be "safe" or it will fail with Unsafe file name. Basically no special characters in file names and only use absolute paths. See concat demuxer for more info.

  2. Run ffmpeg to stream to YouTube:

    ffmpeg -re -framerate 10 -loop 1 -i image.jpg -re -f concat -i input.txt -map 0:v -map 1:a -c:v libx264 -tune stillimage -vf format=yuv420p -c:a aac -g 20 -b:v 2000k -maxrate 2000k -bufsize 8000k -f flv rtmp://youtube
    
  3. When you are ready to add new songs make temp.txt containing:

    ffconcat version 1.0
    file 'audio41.mp3'
    file 'audio42.mp3'
    file 'audio43.mp3'
    
  4. Replace input.txt atomically:

    mv temp.txt input.txt
    

See FFmpeg Wiki: Concatenate for lots more info.

If your audio files are not the same

The files listed in input.txt must all have the same:

  • Format (AAC, MP3, etc, but not mixed)
  • Sample rate (48000, 44100, etc)
  • Number of channels (mono, stereo, etc).

If they vary then you will have to pre-process them before adding them to the playlist. Bash example conforming each audio to stereo (-ac 2) with 44100 sample rate (-ar 44100) and save as AAC format in M4A container:

mkdir conformed
for f in *.mp3; do ffmpeg -i "$f" -map 0:a -ac 2 -ar 44100 -c:a aac "conformed/${f%.*}.m4a"; done
  • Outputting to AAC is recommended for streaming to YouTube.

  • If you do this then you can avoid re-encoding the audio in the ffmpeg command to YouTube. Just change -c:a aac to -c:a copy in step #2: Run ffmpeg to stream to YouTube.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • Thank you for the great detailed explanation! I can start trying to deal with it. BTW what do you mean by telling "files are not the same" ? Different duration, frequencies, codec e.t.c? Also is there a way to additionally take the name of the artist/song from audio file metadata/tags and put it as a text on video at the moment an appropriate song starts playing? – Grrzly Dec 05 '20 at 08:50
  • 1) Must be same format (AAC, MP3, etc, but not mixed), same sample rate (48000, 44100, etc), same number of channels (mono, stereo, etc). Duration can be different. 2) Adding text is a completely different topic and should be asked as a new question. – llogan Dec 05 '20 at 17:50
  • Today I've tried everything you suggested and it seemed working. In addition I need to find a balance between broadcast quality and available internet bandwidth as well as the way to continuously adding new tracks and playing them in random order without interrupting translation. Very big thank you for the help! I'd also address you a separate question about text over the video. Wait for it soon.. – Grrzly Dec 05 '20 at 19:21
  • here is a [question regarding adding a text over the video](https://stackoverflow.com/questions/65161366/how-to-take-metadata-from-mp3-file-and-put-it-to-a-video-as-a-text-using-ffmpeg) – Grrzly Dec 05 '20 at 20:01