Results 1 to 3 of 3

Thread: Qprocess error with script using ffmpeg

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2020
    Qt products

    Default Qprocess error with script using ffmpeg

    Hi, I'm working on a project using a QProcess to call a script to segment video files using ffmpeg. The idea is to call within the script the command to segment a video file into many chunks of a few seconds, selected by the user. The script also converts the video file from mkv to mp4, but that does not seem to have any issues. I have tested the script from the command line and it works just fine, however when I call it with a QProcess in my code the video segments have a wrong length (should be 30 seconds but are of less than a second in duration).

    The script has as arguments the directory of the video file and the duration of the segments, and is called from within a method like so:

    Qt Code:
    1. QProcess *process = new QProcess();
    2. connect(process, &QProcess::readyReadStandardError,
    3. this, &GStreamerManager::readyReadStandardError);
    4. connect(process, &QProcess::readyReadStandardOutput,
    5. this, &GStreamerManager::readyReadStandardOutput);
    7. QTime t;
    8. t.restart();
    9. QLOG_INFO() << "Segmenting video file into mp4 files...";
    10. QStringList arguments;
    11. arguments << video_finfo.absoluteFilePath()
    12. << QString::number(_params.video_split_interval_s);
    13. QLOG_INFO() << arguments;
    14. process->start("mkv2mp4s", arguments);
    15. process->waitForFinished();
    16. QLOG_INFO() << QString("Command %1 exited with code %2")
    17. .arg(process->program()).arg(process->exitCode());
    18. QLOG_INFO() << QString("MP4 converted! Delay %1 mseconds").arg(t.elapsed());
    To copy to clipboard, switch view to plain text mode 

    The script, mkv2mp4s goes like so:

    Qt Code:
    1. #!/bin/bash
    3. # Author: Leandro Candau Sánchez de Ybargüen
    4. # Description: Split a mkv to many mp4 videos using ffmpeg
    5. # Exit values: 0: No error | 1: Wrong arguments | 2: ffmpeg not installed | 3: Error mp4 split |
    6. # 4: Error mp4 creation | 5: Error mkv split
    8. function echo_error {
    9. echo $1 1>&2
    10. }
    12. if [ $# -ne 2 ]; then
    13. echo "Usage: $0 <path_to_video> <split_time_s>"
    14. exit 1
    15. fi
    17. if ! command -v ffmpeg > /dev/null; then
    18. echo_error "ffmpeg is not installed. Please install FFmpeg package."
    19. exit 2
    20. fi
    22. input_mkv_path=$1
    23. split_time_s=$2
    24. exit_value=0
    25. video_basename="$(dirname "$input_mkv_path")/$(basename "$input_mkv_path" .mkv)"
    26. output_mp4_path="$(dirname "$input_mkv_path")/$(basename "$input_mkv_path" .mkv).mp4"
    27. output_mp4_split="$(dirname "$input_mkv_path")/$(basename "$input_mkv_path" .mkv)_%d.mp4"
    28. input_mkv_split="$(dirname "$input_mkv_path")/$(basename "$input_mkv_path" .mkv)_%d.mkv"
    30. echo "Path to mkv: "$input_mkv_path""
    31. echo "Video split seconds: "$split_time_s""
    32. echo "Output mp4: "$output_mp4_path""
    33. echo "Output mp4 split: "$output_mp4_split""
    35. echo "Creating mp4 video $output_mp4_path from mkv $input_mkv_path..."
    36. {
    37. ffmpeg -i $input_mkv_path -codec copy $output_mp4_path
    38. } &> /dev/null
    40. if test -f "$output_mp4_path"; then
    41. echo "New video file "$output_mp4_path" has been created"
    42. echo "Removing old mkv file..."
    43. rm $input_mkv_path
    44. echo "Spliting mp4 file in "$split_time_s" seconds chunks"
    45. {
    46. ffmpeg -i $output_mp4_path -c copy -segment_time $split_time_s -f segment $output_mp4_split
    47. } &> /dev/null
    48. if ls ""$video_basename"_0.mp4" 1> /dev/null 2>&1; then
    49. echo "Removing old mp4 file..."
    50. rm $output_mp4_path
    51. else
    52. echo_error "Split files do not exist!"
    53. exit_value=3
    54. fi
    55. else
    56. echo_error "mp4 creation failed!"
    57. exit_value=4
    58. echo "Spliting mkv file in "$split_time_s" seconds chunks"
    59. {
    60. ffmpeg -i $input_mkv_path -c copy -segment_time $split_time_s -f segment $input_mkv_split
    61. } &> /dev/null
    62. if ls ""$video_basename"_0.mkv" 1> /dev/null 2>&1; then
    63. echo "Removing old mkv file..."
    64. rm $input_mkv_path
    65. else
    66. echo_error "Split mkv files do not exist!"
    67. exit_value=5
    68. fi
    69. fi
    71. # Format the file name from basefile_<0|1|2...>.mp4|mkv to basefile_<offset_seconds>.mp4|mkv
    72. for filename in $(dirname "$input_mkv_path")/$(basename "$input_mkv_path" .mkv)_*.m*; do
    73. substring=$(echo $filename | grep -oP "_\d+\.") # substring con el patron _#.
    74. value=$(echo $substring | grep -oP "\d+") # valor de # del string anterior
    75. value=_$(( $value*$split_time_s )). # valor # * split_time_s
    76. new_filename=${filename/$substring/$value}
    77. if [ $new_filename != $filename ]; then # evita warning en el _0 pq se llama igual
    78. mv $filename ${filename/$substring/$value} # el string original reemplazando substring por _value.
    79. fi
    80. done
    82. exit $exit_value
    To copy to clipboard, switch view to plain text mode 

    I would appreciate any insight on how to deal with this. Thanks.

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 753 Times in 743 Posts
    Qt products
    Windows Android

    Default Re: Qprocess error with script using ffmpeg

    Qt Code:
    1. QString::number(_params.video_split_interval_s);
    To copy to clipboard, switch view to plain text mode 

    Create a temporary variable to hold the value of this expression then examine it in the debugger as well as the value of the input (_params.video_split_interval).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    candau (5th August 2020)

  4. #3
    Join Date
    Jul 2020
    Qt products

    Default Re: Qprocess error with script using ffmpeg

    Hi! Thanks for the reply! I tried debugging as you suggested and saw naught was wrong with the script, as I saw when trying to run it manually.
    Eventually I traced the error onto the next processing point of the video, witch was another script to add metadata into it renaming the video. The problem was in this second script for it renames the video before it was done copying it, thus resulting in an incomplete video. Thanks for the help!

Similar Threads

  1. Killing QProcess shell script
    By mister_m in forum Qt Programming
    Replies: 6
    Last Post: 19th August 2016, 18:08
  2. Qprocess error with ffmpeg
    By davidlamhauge in forum Qt Programming
    Replies: 2
    Last Post: 3rd February 2014, 23:02
  3. Qprocess that call ffmpeg in mac
    By polin in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2013, 22:11
  4. Qt Mac g++ Linking Error on while compiling FFMPEG
    By polin in forum Qt Programming
    Replies: 0
    Last Post: 17th January 2013, 11:08
  5. Running an script using QProcess
    By DiegoTc in forum Newbie
    Replies: 1
    Last Post: 31st December 2010, 19:02


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.